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?
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 shut the container down, the data is persisted.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.