In this blog post, we will install Magento 2 using a Docker file. Before going ahead let’s understand about the docker.
What is Docker?
Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code, you can significantly reduce the delay between writing code and running it in production.
Install Magento 2 using Docker
Step 1:
Determine a location on your local machine where you would like the website files to live.
Step 2:
Open up a command line terminal. Before copying and pasting the code below, replace the placeholder path with the absolute path of where you plan to download/install Magento.
cd /path/to/where/you/will/download/magento && \
composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition .
Step 3:
Increase the PHP memory limit from the default value of 756M to 2048M
find . -name '.htaccess' -exec sed -i '' s/756M/2048M/g {} + && \
find . -name '.htaccess' -exec sed -i '' s/768M/2048M/g {} + && \
find . -name '.user.ini' -exec sed -i '' s/756M/2048M/g {} + && \
find . -name '.user.ini' -exec sed -i '' s/768M/2048M/g {} +
Step 4:
Choose a domain name you would like to use to access the site and add it to your host file.
sudo -- sh -c "echo '127.0.0.1 local.domain.com' >> /etc/hosts"
Here I am using local.domain.com as domain for my local setup
Step 5: Create a docker-compose.yml file
Choose a place on your local machine where you will keep your Docker configuration files. In general, it is the root of the Magento setup.
Copy and past the following code into the new file.
version: '3'
services:
web:
image: webdevops/php-apache-dev:ubuntu-16.04
container_name: web
restart: always
user: application
environment:
- WEB_ALIAS_DOMAIN=local.domain.com
- WEB_DOCUMENT_ROOT=/app/pub
- PHP_DATE_TIMEZONE=EST
- PHP_DISPLAY_ERRORS=1
- PHP_MEMORY_LIMIT=2048M
- PHP_MAX_EXECUTION_TIME=300
- PHP_POST_MAX_SIZE=500M
- PHP_UPLOAD_MAX_FILESIZE=1024M
volumes:
- /path/to/magento:/app:cached
ports:
- "80:80"
- "443:443"
- "32823:22"
links:
- mysql
mysql:
image: mariadb:10
container_name: mysql
restart: always
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=magento
volumes:
- db-data:/var/lib/mysql
phpmyadmin:
container_name: phpmyadmin
restart: always
image: phpmyadmin/phpmyadmin:latest
environment:
- MYSQL_ROOT_PASSWORD=root
- PMA_USER=root
- PMA_PASSWORD=root
ports:
- "8080:80"
links:
- mysql:db
depends_on:
- mysql
volumes:
db-data:
external: false
Step 6:
Replace the domain name on WEB_ALIAS_DOMAIN=local.domain.com with the domain you created earlier in this tutorial.
Replace /path/to/magento with the absolute path to the Magento files you downloaded earlier. Leave everything after the colon just the way it is.
Save the new file as docker-compose.yml
Now We’re Ready to Fire It Up
Fire up your virtual machine! The first time you spin up, Docker needs to download the images, this may take a few minutes. Future spin-ups will only take less time.
docker-compose up -d --build
Let’s make sure that it’s up and running as planned. In a web browser, go to 127.0.0.1:8080 and make sure that you can see phpMyAdmin. If you can, it was a success.
Finally, Let’s Install Magento 2 using Docker!
Access your Docker web container’s command line.
docker exec -it web bash
Navigate to the web document root.
cd /app
This is the root path of Magento within our web container. you can execute all the Magento commands here.
Install Magento 2 using Docker! Before copying and pasting the command shown below into the Docker terminal, replace all the details with yours.
php bin/magento setup:install \
--admin-firstname=John \
--admin-lastname=Doe \
--admin-email=johndoe@example.com \
--admin-user=admin \
--admin-password='SomePassword123' \
--base-url=https://local.domain.com \
--base-url-secure=https://local.domain.com \
--backend-frontname=admin \
--db-host=mysql \
--db-name=magento \
--db-user=root \
--db-password=root \
--use-rewrites=1 \
--language=en_US \
--currency=USD \
--timezone=America/New_York \
--use-secure-admin=1 \
--admin-use-security-key=1 \
--session-save=files \
--use-sample-data
In your web browser, visit your website at https://local.domain.com or whatever domain you choose. The first time you go to access the site, it might take a couple of minutes for the page to load. This is because nothing is cached yet and the Magento system is automatically generating files as the page loads. Subsequent page loads will be faster. Additionally, because the web container uses a self-signed SSL certificate, the browser will likely present you with a security alert the first time you visit the URL. Just follow any prompts to add an exception so that you can proceed to the local website and now you have installed Magento 2 using Docker.
Congratulations! You are now running Magento 2 on Docker.