Mysql: Restoring db from backup using docker-compose

Created on 2 Apr 2017  路  3Comments  路  Source: docker-library/mysql

I'd like to use docker-compose to set up my container such that I can restore my sql-dump. However docker-compose isn't working. It just creates the container but the database is never created.

I've tried two approaches:

  1. Testing if database will be created by using plain old docker run without docker compose:

docker run --env MYSQL_ROOT_PASSWORD=root-pass MYSQL_DATABASE=mydb -v $PWD/db-dump:/docker-entrypoint-initdb.d --name testsql mysql

I have a copy of schema.sql in db-dump.

This works great. The mydb database is created and the script contained in db-dump is restored into mydb. However this is not what I want - I want to use docker-compose.

  1. Using docker compose.

I've tried 2 approaches with this
The first attempt - using volumes to mount the schema's directory:

version: '2.1'
services:
  mysql:
    image: mysql
    environment:
      - MYSQL_ROOT_PASSWORD="root-pass"
      - MYSQL_DATABASE="mydb"
    volumes:
      - ./db-dump:/docker-entrypoint-initdb.d
      - mysql_data:/var/lib/mysql
volumes:
  mysql_data:

Second attempt using own Dockerfile and using ADD command to add the schema:
2 files in compose/mysql (Dockerfile and a copy of schema.sql)

My Dockerfile (compose/mysql/Dockerfile)

FROM mysql
ADD schema.sql /docker-entrypoint-initdb.d/

docker-compose.yml

version: '2.1'
services:
  mysql:
    build: ./compose/mysql
    environment:
      - MYSQL_ROOT_PASSWORD="root-pass"
      - MYSQL_DATABASE="mydb"
      # tried this as well
      # MYSQL_ROOT_PASSWORD: "root-pass"
      # MYSQL_DATABASE: "mydb"
    volumes:
      - mysql_data:/var/lib/mysql
volumes:
  mysql_data:

Both attempts above using docker-compose do not work. mydb is never created and the restore is never attempted.

Any direction as to what I might be missing?

Most helpful comment

Is there already a database in your mysql_data directory? The execution of scripts in docker-entrypoint-initdb.d only happens when a database is freshly created, not if the image is connected to an existing one.

All 3 comments

Is there already a database in your mysql_data directory? The execution of scripts in docker-entrypoint-initdb.d only happens when a database is freshly created, not if the image is connected to an existing one.

I figured it out eventually. I had to delete the data volume to get it to work.

i am trying to do the same, but docker-compose always is not working
I have build the image of my application and whenever i am trying to run docker-compose for my application and mysql database , always getting
"pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([Errno 99] Cannot assign requested address)")" Error.

Please provide the steps which you have followed or any other heads up...

Was this page helpful?
0 / 5 - 0 ratings