e-Zest members share technology ideas to foster digital transformation.

How to setup Docker Container for Magento 2

Written by Rajesh Rathod | Oct 15, 2021 7:00:00 AM

Docker is an application that allows us to manage multiple applications in the simplest way in the docker container. Docker container is the bundle of all the resources required to run your application in an isolated process. The container is a simple virtual machine with all the necessary service/application setup for the application.

We can set up the entire technology stack for Magento in the container and create the image; The docker image is portable to create another container immediately as it is required for re-use

Prerequisites

  • Ubuntu Server/Machine
  • Account on docker hub if you want to create your own docker images and push it to Docker Hub for the public uses

Docker Installation

Step 1

  1. Update your existing packages in ubuntu
    sudo apt-get update
  2. Install a few prerequisites package
    sudo apt install apt-transport-https ca-certificates curl software-properties-common
  3. Add GPG key to official Docker repository
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
  4. Add Docker repository to APT sources
    sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"
  5. Update newly added repo – sudo apt-get update
  6. Install Docker
    sudo apt install docker-ce
  7. Check docker status, enable & disable
    sudo systemctl status docker
    sudo systemctl enable docker
    sudo systemctl disable docker
  8. Display other Docker commands
    Use -
    $ docker
    $ docker docker-subcommand –help
    $ docker info

Download/Pull official ubuntu Docker image

Command - docker pull ubuntu

To see available Docker images

Command – docker images

How to create a Docker container

docker container run -it -p 81:80 --name magento2 ubuntu bash

-it: It gives you interactive shell access into the Docker container

-p 81:80: port to execute the container

--name magento: Docker Name

ubuntu: docker image

bash: to get CLI interface for the docker

Check Available Docker

Command:

docker container ls
docker container ls -a

To Commit your own customized Docker Images with all the resources

If you are working with Magento 2, you have to install a technology stack for the Magento 2 for ex- php, php-fpm, nginx, redis, varnish in the container. If you want to re-use this container by your own docker image, we can create/commit shareable docker images that can be re-used to Magento 2 direct project setup. Follow below commands

docker commit f9df6b49a43d image/ubuntu

f9df6b49a43d – Container Id

image/ubuntu - Docker image name

Mount/Map your Magento code directory to docker directory

You can map/mount your code directory (Source code interface) to Docker container directory for easy development; whatever changes have been done in your local directly will be synced immediately with the container directly.

For example: Your local machine directory path is - /var/www/html/m2-docker (Where you have cloned GIT LAB code) where you download Magneto source code

You can mount this project directory to Docker nginx/apache www directory for the development; follow the below command for directory mount

docker container run -it -p 81:80 --name magento2 -v /var/www/html/m2-docker:/var/www/html ubuntu bash

You can get a list of the active container using the below command

docker container ls
docker container ls -a – It will give you all available container

Remove Docker container

We can remove/delete unused container using the below command

docker container rm 57e21b9d5ab7

rm – Remove

57e21b9d5ab7 – Container Id

Start/Stop Docker Container

docker container start magento2

docker container stop magento2

Log in to CLI access with a running container

docker container exec -it magento2 bash

If you want to leave the container running from the CLI, press CTRL+P+Q. With this command, you can exit from the container CLI, but your container will be running in the background.

For More details, you can visit the links below

https://docs.docker.com/

https://docs.docker.com/get-started/

https://docs.docker.com/reference/

https://docs.docker.com/engine/reference/run/

https://docs.docker.com/engine/reference/commandline/create/