In the previous article Install Magento 2 on Docker – Part 1, I have guided you to successfully install Magento 2 on Docker. But after checking out the result, I felt a little lacking because of two things:
- There are no sample data
- I have no idea how to access the database
So this article will solve that two things.
1. Install sample data using composer.
Notice: Do not install sample data if your Magento application is set for production mode. Switch to developer mode first.
Notice: The following actions require the auth.json file I guided you to create in part 1.
To install sample data using the command line, enter the following command as the Magento file system owner in the <magento_root> directory:
docker-compose run --rm deploy magento-command sampledata:deploy
The above script will take some time to finish because of a lot of sample data. After it finished, run:
docker-compose run --rm deploy magento-command setup:upgrade
Now open your magento template and you will see the sample data
2. Access Database using phpmyadmin
In <magento_root> directory, edit file docker-compose.yml and add phpmyadmin configuration for docker-compose. Example:
This is my docker-compose.yml file for Magento version 2.4.2
# ./vendor/bin/ece-docker 'build:compose' --mode=developer version: '2.1' services: db: hostname: db.magento2.docker image: 'mariadb:10.4' shm_size: 2gb environment: - MYSQL_ROOT_PASSWORD=magento2 - MYSQL_DATABASE=magento2 - MYSQL_USER=magento2 - MYSQL_PASSWORD=magento2 ports: - '3306' volumes: - '.:/app:delegated' - 'magento-magento-db:/var/lib/mysql' healthcheck: test: 'mysqladmin ping -h localhost -pmagento2' interval: 30s timeout: 30s retries: 3 networks: magento: aliases: - db.magento2.docker redis: hostname: redis.magento2.docker image: 'redis:6.0' volumes: - '.:/app:delegated' ports: - 6379 sysctls: net.core.somaxconn: 1024 ulimits: nproc: 65535 nofile: soft: 20000 hard: 40000 healthcheck: test: 'redis-cli ping || exit 1' interval: 30s timeout: 30s retries: 3 networks: magento: aliases: - redis.magento2.docker elasticsearch: hostname: elasticsearch.magento2.docker image: 'magento/magento-cloud-docker-elasticsearch:7.9-1.2.3' ulimits: memlock: soft: -1 hard: -1 environment: - cluster.name=docker-cluster - bootstrap.memory_lock=true networks: magento: aliases: - elasticsearch.magento2.docker fpm: hostname: fpm.magento2.docker image: 'magento/magento-cloud-docker-php:7.4-fpm-1.2.3' extends: generic volumes: - '.:/app:delegated' networks: magento: aliases: - fpm.magento2.docker depends_on: db: condition: service_healthy web: hostname: web.magento2.docker image: 'magento/magento-cloud-docker-nginx:1.19-1.2.3' extends: generic volumes: - '.:/app:delegated' environment: - WITH_XDEBUG=0 - NGINX_WORKER_PROCESSES=1 - NGINX_WORKER_CONNECTIONS=1024 networks: magento: aliases: - web.magento2.docker depends_on: fpm: condition: service_started varnish: hostname: varnish.magento2.docker image: 'magento/magento-cloud-docker-varnish:6.2-1.2.3' networks: magento: aliases: - varnish.magento2.docker depends_on: web: condition: service_started phpmyadmin: hostname: phpmyadmin.magento2.docker restart: always image: phpmyadmin/phpmyadmin:latest networks: magento: aliases: - magento2.docker environment: - MYSQL_ROOT_PASSWORD=magento2 - PMA_USER=magento2 - PMA_PASSWORD=magento2 ports: - "8080:80" depends_on: - db tls: hostname: tls.magento2.docker image: 'magento/magento-cloud-docker-nginx:1.19-1.2.3' extends: generic networks: magento: aliases: - magento2.docker environment: - NGINX_WORKER_PROCESSES=1 - NGINX_WORKER_CONNECTIONS=1024 - UPSTREAM_HOST=varnish - UPSTREAM_PORT=80 ports: - '80:80' - '443:443' depends_on: varnish: condition: service_started generic: hostname: generic.magento2.docker image: 'magento/magento-cloud-docker-php:7.4-cli-1.2.3' env_file: ./.docker/config.env environment: - MAGENTO_RUN_MODE=developer - 'PHP_EXTENSIONS=bcmath bz2 calendar exif gd gettext intl mysqli pcntl pdo_mysql soap sockets sysvmsg sysvsem sysvshm opcache zip xsl sodium redis' build: hostname: build.magento2.docker image: 'magento/magento-cloud-docker-php:7.4-cli-1.2.3' extends: generic volumes: - '.:/app:delegated' networks: magento-build: aliases: - build.magento2.docker depends_on: db: condition: service_healthy redis: condition: service_healthy elasticsearch: condition: service_healthy deploy: hostname: deploy.magento2.docker image: 'magento/magento-cloud-docker-php:7.4-cli-1.2.3' extends: generic volumes: - '.:/app:delegated' networks: magento: aliases: - deploy.magento2.docker depends_on: db: condition: service_healthy redis: condition: service_healthy elasticsearch: condition: service_healthy volumes: magento-magento-db: { } networks: magento: driver: bridge magento-build: driver: bridge
You can find some lines like this in my file, I configured it to match with the db configuration above:
phpmyadmin: hostname: phpmyadmin.magento2.docker restart: always image: phpmyadmin/phpmyadmin:latest networks: magento: aliases: - magento2.docker environment: - MYSQL_ROOT_PASSWORD=magento2 - PMA_USER=magento2 - PMA_PASSWORD=magento2 ports: - "8080:80" depends_on: - db
When we have these lines, run:
docker-compose up -d
And all done. Now you can open http://localhost:8080/ to access the database
Leave a Reply