Categorieën
Algemeen nieuws Magento Nieuws

Local development with the Hypernode Docker container (Linux)

Docker + Hypernode + Linux

“It worked on my machine…”, how often did you hear a colleague say that phrase?

You obviously want to mimic the production environment as close as possible when developing on your local machine. Since most of our customers are on the Hypernode platform, we switched our development machines from Mac to Linux last year. Some preferred their own LEMP stack (Linux, Nginx, MySQL and PHP) and some others preferred the Linux port of Laravel Valet.

We tried the Hypernode Vagrant but the speed was not good enough to allow fast development.

Next to that, Vagrant has a very large overhead because it runs an entire virtual machine including the kernel.It is acceptable if you are have just one project, but if you need to work on multiple projects at the same day it has quite an impact on your computer.

The team of Byte announced their docker container in June 2018. It is as close as possible to our production environments. Although it is not best practice to stash all these services into one container, the ease and stability were the decisive factors in continuing research into this container.

The Hypernode docker container contains all the services you need to run a Magento (2) shop.

  • SSH
  • Nginx
  • MySQL
  • Redis
  • Mailhog
  • Varnish

Although these services are enough in production environments, this does not mean that this meets all your developing needs.. You want to edit the project in either PHPStorm, Sublime or another IDE. Next to that you want to have Xdebug at your disposal. Please note that this tutorial has been written with PhpStorm in mind.

Another thing that quickly came to mind was sharing your local composer cache with the containers, since running composer install / update without cache equals more inefficiency.

The docker file:

Save the following in a ‘Dockerfile’ file. Please note that the name of the file has to be “”Dockerfile”” with “”D”” as capital.

Explanation:
FROM tells docker from which base image you want to use
MAINTAINER is the person who maintains the image
RUN is used to execute a command during build

The example above adds the ‘app’ user to the sudoers list, which means you can use sudo while using ssh.
Normally you can’t execute sudo on a hypernode, but sometimes you need to install a package for debugging purposes, change the PHP version or reboot services.
Next to that it installs Xdebug using the package manager.

Build the container
A note in advance: we use ‘acme‘ as the project name. Replace ‘acme’ with your own project name! 

You can build the container by executing:

docker build -t devhypernode .

Run the container:

docker run -v ~/.cache/composer:/data/web/.composer/cache -v ~/.composer/auth.json:/data/web/.composer/auth.json -v ~/development/acme:/data/web/magento2 —name acme -d devhypernode

-v adds a new volume, which mounts into the container.
-d runs the container in the background

After that you want to assign the directory to the app user.

docker exec acme chown -R app:app /data/web/

Tip: use Dockstation to view all your containers!

All set!

Your container is now up and running, use docker inspect acme | grep IPAddress to view the ip address or use DockStation. You can now SSH into the server to install Magento. The username is app and the default password is insecure_docker_ssh_password, so execute ssh app@your.container.ip in your terminal and enter this password.

Tip: We suggest you to add your public key to the server. Copy the contents of your local ~/.ssh/id_rsa.pub to ~/.ssh/authorized_keys of the container. By using this method you don’t have to type in the password every time you want to connect.

If you wish you can disable the password login afterwards:
docker exec acme sed -i 's/PasswordAuthentication yes/PasswordAuthentication no/g' /etc/ssh/sshd_config

Install Magento 2

Byte has a very nice tutorial which explains the installation step by step. Remember that ~/magento2 already have been made, so you can skip that step.

Speed up Magento2
Waiting is something most of us hate. To speed up your container we suggest you to add Redis to app/etc/env.php. Adding Redis can cut your TTFB times in half.



Setup Xdebug

Create a PHP config file in /data/web/public/.user.ini

Restart the services with the following command:

docker exec acme bash /etc/my_init.d/60_restart_services.sh

Configure PHPstorm to match the following Xdebug settings.

After that you have to set up the path mappings.

You are now ready to use Xdebug!

Commands you will need

Restart hypernode services:

docker exec acme bash /etc/my_init.d/60_restart_services.sh

Get ip address from container:

docker inspect acme | grep IPAddress

Change PHP version from 7.1 to 7.0:

docker exec acme sed -i ’s/7.1/7.0/g’ /etc/my_init.d/60_restart_services.sh
docker exec acme update-alternatives --set php $(which php7.0)
docker exec acme bash /etc/my_init.d/60_restart_services.sh

Ssh into container with your own ssh keys forwarded:

ssh -A 172.17.0.2 (replace with your container ip)