5

I have an application that I want to run in a container, and a database that I want to run in a container. I have implemented the database part like so.

  db:
    image: mysql:5.7
    ports:
      - "3306:3306"
    command: --init-file /SCHEMA.sql
    restart: always
    environment:
      MYSQL_DATABASE: shapeshop
      MYSQL_USER: admin
      MYSQL_PASSWORD: admin
      MYSQL_ROOT_PASSWORD: root
    networks:
      - backend

My initial schema for my database is in a file called SCHEMA.sql in the same directory as my docker-compose file.

I am using the

command --init-file

configuration to setup up the databse.

When I run docker-compose up it fails with this error :

2021-05-14T08:20:59.320203Z 0 [ERROR] mysqld: File '/SCHEMA.sql' not found (Errcode: 2 - No such file or directory)

Is my command file specified correctly?

3
  • Without volumes: to map the host file into the container, the database startup can't see it. You'd typically mount the SQL file into /docker-entrypoint-initdb.d/ and the standard mysql image startup will execute it for you (only the first time the container starts). Commented May 14, 2021 at 11:09
  • 2
    so does this command --init work only on the file system within the container? I'm a bit confused by the documentation. Commented May 16, 2021 at 10:56
  • I was planning on going through the documentation today, to get an answer on these kind of questions..... Commented May 16, 2021 at 13:39

1 Answer 1

6
+300

As David Maze mentioned in the comments, you have to mount the schema file into the container.

so does this command --init work only on the file system within the container?

yes.

You can also omit the init command entirely since the image automatically loads .sql dumps into the database. From the docs:

[...] it will execute files with extensions .sh, .sql and .sql.gz that are found in /docker-entrypoint-initdb.d. [...] You can easily populate your mysql services by mounting a SQL dump into that directory [...].

Like this:

services:
  db:
    image: mysql:5.7
    # If you want to use a custom command, do it like this:
    # command: ["mysqld","--innodb-buffer-pool-size=2G"]
    volumes:
      # Persist data
      - "db_data:/var/lib/mysql"
      # Mount the schema file
      - "./SCHEMA.sql:/docker-entrypoint-initdb.d/SCHEMA.sql"
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: shapeshop
      MYSQL_USER: admin
      MYSQL_PASSWORD: admin
      MYSQL_ROOT_PASSWORD: root
    networks:
      - backend
    restart: always

volumes:
  db_data:

I've also added an volume for /var/lib/mysql so that when you stop and remove the container, the data is persisted.

Sign up to request clarification or add additional context in comments.

1 Comment

This answer is perfect. I have done this in a number of environments over the years and it behaves exactly as described. Very thorough answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.