3

I'm currently running Magento 2 on Docker using docker-compose.yml here is the file:

version: '3.0'
services:
  web:
    image: alexcheng/magento2
    container_name: magento2
    ports:
      - "80:80"
    env_file:
      - env
    volumes:
      - /dev/mytheme:/var/www/html/app/design/frontend/mytheme/default
      - magento-data:/var/www/html
  db:
    image: mysql:5.6.23
    container_name: mysql
    volumes:
      - db-data:/var/lib/mysql/data
    env_file:
      - env
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: phpmyadmin
    ports:
      - "8580:80"
  smtp: 
    image: namshi/smtp
    container_name: smtp
    restart: always
    ports: 
      - "25:25"
  redis: 
    image: redis 
    container_name: redis
    command: ["redis-server", "--appendonly", "yes"]
    volumes:
      - redis-data:/data
  elasticsearch:
      image: elasticsearch:6.6.2
      container_name: elasticsearch
      ports:
        - "9200:9200"
  rabbitmq:
    image: rabbitmq
    container_name: rabbitmq
    ports:
      - "5672:5672"
volumes:
  db-data:
  magento-data:
  redis-data:
    external: false

From what I understand, the volumes exist so it should work.

Here is the scenario:

  1. docker-compose down then
  2. docker-compose up -- data is all lost need to reinstall everything

The whole database is wiped out. There are no tables at all.

  1. docker container exec -it mysql bash
  2. mysql -h db -u magento -p
  3. Take a look at the tables - nothing is there.

In order to reinstall everything, you need to run:

  1. docker container exec -it magento2 bin/magento setup:uninstall
  2. docker container exec -it magento2 install-magento

Sometimes you need to give the write permission to folders in order to run install-magento properly so:

  • chmod 777 -R var/cache...

I thought db-data and magento-data should do the trick...

I'm not able to explain more than this... Is anyone else running into this problem?

FYI: I'm running docker on Windows 10 Pro with Hyper-V

Thanks in advance!

  • what exactly is your problem? docker-compose down is working as expected... maybe you want to use docker-compose stop instead? – Philipp Sander Mar 20 '19 at 14:10
  • In a scenario where you need to restart your app. Everything is lost... I thought the volumes should keep the data persistent with docker-compose down or am I wrong? Also let's say you're running an AWS ElasticBeanstalk your app has to restart once you do changes. Obviously you would use something like AWS RDS for the database, but your magento container has to go through that whole wizard one more time... Correct me if I'm wrong please. I am trying to recreate that scenario locally but keeping the data persistent. Maybe I'm missing something here and docker-compose stop is the way to go. – Mark Mar 20 '19 at 14:18
  • docker-compose down kills your containers... docker-compose stop STOPPS your containers. whydid you think that? docker-compose --help – Philipp Sander Mar 20 '19 at 14:25
  • Alright it makes sense, something just didn't add up when I was thinking about it... Just a slight confusion! You cleared it up – Mark Mar 20 '19 at 14:48
1

Your data is thrown away when you call docker-compose down because it will stop and remove containers, networks, images, and volumes.

You should use docker-compose stop

0

My docker stack looks a bit like this

version: "2"
services:
  varnish:
    image: meanbee/magento2-varnish:latest
    environment:
      - VIRTUAL_HOST=magento2.docker
      - VIRTUAL_PORT=80
      - HTTPS_METHOD=noredirect
    ports:
      - 80
    links:
      - web

  mail:
    image: mailhog/mailhog
    restart: 'always'
    ports:
      - 1025:1025
      - 8025:8025
    links:
      - fpm
      - db      

  web:
    image: meanbee/magento2-nginx:1.9
    restart: 'always'
    ports:
      - 32785:80
    links:
      - fpm
      - db
    volumes_from:
      - appdata
    env_file:
      - ./global.env
    #environment:
      #- VIRTUAL_HOST=magento2.docker
      #- VIRTUAL_PORT=80
      #- HTTPS_METHOD=noredirect

  fpm:
    hostname: fpm.magento2.docker
    image: meanbee/magento2-php:7.1-fpm
    restart: 'always'
    ports:
      - 9000
    links:
      - db
    volumes_from:
      - appdata
    env_file:
      - ./global.env

  db:
    build: ./mysql
    container_name: '5.7-mysql'
    restart: 'always'
    env_file:
      - ./global.env      
    volumes: 
      - ./data/mysql:/var/lib/mysql
      - ./logs/mysql:/var/log/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=magento2
      - MYSQL_DATABASE=magento2
      - MYSQL_USER=magento2
      - MYSQL_PASSWORD=magento2
      - TERM=meh

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    restart: 'always'
    links: 
      - db:db
    ports:
      - 8000:80
    environment:
      - MYSQL_USER=magento2
      - MYSQL_PASSWORD=magento2
      - MYSQL_ROOT_PASSWORD=magento2 

  cli:
    hostname: cli.magento2.docker
    image: meanbee/magento2-php:7.1-cli
    restart: 'always'
    links:
      - db
    volumes:
      - ~/.composer/cache:/root/.composer/cache
    volumes_from:
      - appdata
    env_file:
      - ./global.env
      - ./composer.env
    environment:
      - M2SETUP_INSTALL_DB=true
      - M2SETUP_DB_HOST=db
      - M2SETUP_DB_NAME=magento2
      - M2SETUP_DB_USER=magento2
      - M2SETUP_DB_PASSWORD=magento2
      - M2SETUP_BASE_URL=http://magento2.docker:32785/
      - M2SETUP_SECURE_BASE_URL=https://magento2.docker:32785/
      - M2SETUP_BACKEND_FRONTNAME=xpanel
      - M2SETUP_ADMIN_FIRSTNAME=Admin
      - M2SETUP_ADMIN_LASTNAME=User
      - M2SETUP_ADMIN_EMAIL=dummy@gmail.com
      - M2SETUP_ADMIN_USER=admin
      - M2SETUP_ADMIN_PASSWORD=test123
      - M2SETUP_VERSION=2.2.*
      - M2SETUP_FORCE_STATIC_DEPLOY=true
      # - M2SETUP_USE_SAMPLE_DATA=true

  cron:
    image: meanbee/magento2-php:7.1-cli
    hostname: magento2-cron.docker
    restart: 'always'
    command: run-cron
    env_file:
      - ./global.env    
    environment:
      - ENABLE_SENDMAIL=false
    volumes_from:
      - appdata
    links:
      - db
    env_file:
      - ./global.env      

  appdata:
    image: tianon/true
    volumes:
      - ./magento:/var/www/magento

It's a bit overkill. Find magento 2 docker meanbee on git for more usage instructions. But data will persist.

You can start, restart and stop containers.

Access via http://magento2.docker:32785

Wrappers

docker-compose run --rm cli magento-command etc

0

Given your exact scenario, @Phillip Sander's answer is correct, but it's important to understand why it's correct.

You stated:

From my understanding the volumes are present so it should work.

That's technically incorrect.

Please consider what Metin Y. said here:

services, networks and named volumes declared in a compose.yml are bound to the lifecycle of docker-compose.

You need to create your volume from the cli (docker volume create ....), and declare it in your compose.yml as external.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.