docker-compose: reinitializing MySQL db every time

牧云@^-^@ 提交于 2021-02-16 09:23:07

问题


We have a Rails app with MySQL as DB. The db part of docker-compose.yml looks like

  db:
    image: mysql
    env_file:
      - ma.env
    volumes:
      - ./dump-db:/docker-entrypoint-initdb.d
      - ./my.cnf:/etc/mysql/my.cnf
    restart: always
    ports:
      - "3306:3306"
    environment:
            - MYSQL_HOST=

and under ./dump-db folder there actually is an sql-dump of our db.

Problem is, we need to have that exact dump loaded each and every time docker-compose up is run. MySQL docker image works in such a way, that it stores it's data on host machine and therefore your db-service is not stateless.

What we need is that db service discarded all changes done within docker-compose up previous run and fresh-started with that exact dump.


回答1:


SOLVED

actually, ideas above gave me some insight to RTM and here goes

  • Get your own MySQL Dockerfile
  • Remove the VOLUME line from it, where it mentions that /var/lib/mysql should be mounted on host.
  • docker-compose up --force-recreate db



回答2:


If you don't want to keep the data each time that you execute the docker-compose up you have to remove the volumes created by the execution of this command.

To achieve that you can execute the following command, that will remove the db service with the volumes declared in the volumes section.

docker-compose down -v db



来源:https://stackoverflow.com/questions/39776713/docker-compose-reinitializing-mysql-db-every-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!