Docker Configuration for Magento 2
Deploy secure and flexible docker infrastructure for Magento 2 in a matter of seconds.
🚀 Deploy your project:
Disclaimer: By default, the latest versions of packages are configured, above those recommended by Magento 2
- Install Docker:
you can use any linux host or Docker Desktop
https://docs.docker.com/
https://docs.docker.com/engine/install/debian/
curl -fsSL https://get.docker.com -o get-docker.sh
bash get-docker.sh
- Install docker compose v2:
mkdir -p ~/.docker/cli-plugins/
curl -SL https://github.com/docker/compose/releases/latest/download/docker-compose-linux-x86_64 \
-o ~/.docker/cli-plugins/docker-compose
chmod +x ~/.docker/cli-plugins/docker-compose
- Add alias or use auto completion feature:
echo "alias doco='docker compose'" >> ~/.bash_profile
. ~/.bash_profile
- Create deployment directory:
mkdir magento && cd magento
- Clone repo:
git clone https://github.com/magenx/Magento-2-docker-configuration.git .
- To avoid copying default passwords and hacking through open ports - generate new passwords:
https://docs.docker.com/compose/compose-file/compose-file-v3/#secrets
bash passgen.sh
When mariadb is started for the first time, a new database with the specified name will be created and initialized with the provided configuration variables. Furthermore, it will execute files with extensions .sh, .sql, .sql.gz, .sql.xz and .sql.zst that are found in ./mariadb/
[ ! ] Check all data, adjust your settings, edit your variables
- Run to pull and build images and start containers:
to enable buildkit:
echo '{ "features": { "buildkit": true } }' > /etc/docker/daemon.json
orexport DOCKER_BUILDKIT=1
doco build --no-cache php
doco up -d
- Watch syslog for errors and issues:
tail -f /var/log/syslog
- To request TLS/SSL certificate with certbot you can run this command [--staging] to test:
doco stop nginx
doco run -p 80:80 --rm certbot certonly \
--email ${ADMIN_EMAIL} --agree-tos --no-eff-email --standalone -d ${DOMAIN} --staging
doco start nginx
change your nginx configuration to uncomment tls/ssl
remove [--staging] flag to reissue live certificate
- To request TLS/SSL certificate with certbot in realtime you can run this command:
doco run --rm certbot certonly \
--email ${ADMIN_EMAIL} --agree-tos --no-eff-email --webroot -w ${WEB_ROOT_PATH} -d ${DOMAIN}
doco restart nginx
- Get random mariadb root password from log:
doco logs mariadb 2>&1 | grep GENERATED
magenx-mariadb | 2021-11-16 08:48:17-05:00 [Note] [Entrypoint]: GENERATED ROOT PASSWORD: m5.QyKl.PS8o<Yx|Jv(~DV&9cY-`i~XZ
- Example how to run composer or magento command from host:
magento entrypoint is n98-magerun2 script, looks like providing more commands and options
doco run --rm composer update
doco run --rm magento module:status --enabled
doco run --rm magento module:disable Magento_TwoFactorAuth
- Source variables and issue magento installation command for example:
doco run --rm magento setup:install --base-url=http://${DOMAIN}/ \
--db-host=mariadb \
--db-name=${MARIADB_NAME} \
--db-user=${MARIADB_USER} \
--db-password='${MARIADB_PASSWORD}' \
--admin-firstname=${ADMIN_FIRSTNAME} \
--admin-lastname=${ADMIN_LASTNAME} \
--admin-email=${ADMIN_EMAIL} \
--admin-user=${ADMIN_LOGIN} \
--admin-password='${ADMIN_PASSWORD}' \
--language=${LOCALE} \
--currency=${CURRENCY} \
--timezone=${TIMEZONE} \
--cleanup-database \
--cache-backend=redis \
--cache-backend-redis-server=redis-cache \
--cache-backend-redis-port=6380 \
--cache-backend-redis-db=0 \
--cache-backend-redis-compress-data=1 \
--cache-backend-redis-compression-lib=gzip \
--cache-backend-redis-password='${REDIS_PASSWORD}' \
--session-save=redis \
--session-save-redis-host=redis-session \
--session-save-redis-port=6379 \
--session-save-redis-log-level=3 \
--session-save-redis-db=0 \
--session-save-redis-compression-lib=gzip \
--session-save-redis-password='${REDIS_PASSWORD}' \
--use-rewrites=1 \
--amqp-host=rabbitmq \
--amqp-port=5672 \
--amqp-user=magento \
--amqp-password='${RABBITMQ_PASSWORD}' \
--amqp-virtualhost='/' \
--consumers-wait-for-messages=0 \
--search-engine=elasticsearch7 \
--elasticsearch-host=elasticsearch \
--elasticsearch-port=9200 \
--elasticsearch-enable-auth=1 \
--elasticsearch-username=elastic \
--elasticsearch-password='${ELASTIC_PASSWORD}'
- Stop all services:
doco down
Stopping magenx-cron ... done
Stopping magenx-nginx ... done
Stopping magenx-php ... done
Stopping magenx-magento ... done
Stopping magenx-elasticsearch ... done
Stopping magenx-rabbitmq ... done
Stopping magenx-varnish ... done
Stopping magenx-certbot ... done
Stopping magenx-nodejs ... done
Stopping magenx-phpmyadmin ... done
Stopping magenx-mariadb ... done
Stopping magenx-redis ... done
🛠️ Stack components in use:
- MariaDB - MariaDB Server is a high performing open source relational database, forked from MySQL.
- Nginx - Official build of Nginx.
- PHP - PHP scripting language.
- Varnish - Varnish is an HTTP accelerator designed for content-heavy dynamic web sites as well as APIs.
- ElasticSearch - Elasticsearch is a powerful open source search and analytics engine that makes data easy to explore.
- Redis x2 - Redis is an open source key-value store that functions as a data structure server.
- RabbitMQ - RabbitMQ is an open source multi-protocol messaging broker.
- NodeJS - Node.js is a JavaScript-based platform for server-side and networking applications.
- PHPMyAdmin - phpMyAdmin - A web interface for MySQL and MariaDB.
- Certbot - Official build of EFF's Certbot tool for obtaining TLS/SSL certificates from Let's Encrypt.
- [Composer]
- [Cron]