Skip to content

Restoring db from backup using docker-compose #276

@odero

Description

@odero

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions