I tried to create Node.js Container and MySQL Container by docker-compose, so I wrote below files.
○tree
.
├── docker-compose.yml
├── mysql
│ ├── Dockerfile
│ ├── conf
│ │ ├── default_authentication.cnf
│ │ └── my.cnf
│ ├── data
│ └── init
│ └── create.sql
└── nodejs
├── Dockerfile
└── app
○./docker-compose.yml
version: '3'
services:
webserver:
build: nodejs
image: node-express-dev:1.0
container_name: nodejs
tty: true
volumes:
- ./nodejs/app:/app
ports:
- "8080:3000"
db:
build: mysql
container_name: mysql
ports:
- "3306:3306"
volumes:
- ./mysql/init:/docker-entrypoint-initdb.d
- dbata:/var/lib/mysql
- ./mysql/conf:/etc/mysql/conf.d
environment:
MYSQL_ROOT_PASSWORD: rootpass
MYSQL_USER: user
MYSQL_PASSWORD: mysql
volumes:
dbata:
driver_opts:
type: none
device: /home/.../mysql/data
o: bind
○./mysql/Dockerfile
FROM mysql:latest
RUN mkdir -p /var/log/mysql/
RUN touch /var/log/mysql/mysqld.log
○./mysql/init/create.sql
SET CHARSET UTF8;
DROP DATABASE IF EXISTS test;
CREATE DATABASE test DEFAULT CHARACTER SET utf8;
use test;
SET CHARACTER_SET_CLIENT = utf8;
SET CHARACTER_SET_CONNECTION = utf8;
DROP TABLE IF EXISTS users;
CREATE TABLE users(
id INT,
name VARCHAR(20),
byear INT
);
INSERT INTO users VALUES (1, 'user1', 1995);
INSERT INTO users VALUES (2, 'user2', 1995);
GRANT ALL ON *.* TO 'user';
Then I use sudo docker-compose up --build -d and access to MySQL by mysql -h 127.0.0.1 -u user -p. And in database test I INSERT one tuple to users table.
I thought ./mysql/init/create.sql is executed whenever I run sudo docker-compose up --build -d, so once I remove containers by sudo docker-compose down and recreate containers by sudo docker-compose up --build -d. But when I checked test.users, there are three tuples(create.sql inserts TWO tuples).
My thought (./mysql/init/create.sql is executed whenever I run sudo docker-compose up --build -d) is wrong?
sudo docker-compose down.docker-compose downdoesn't remove containersdocker-compose downoutput isstopping ~andremoving ~, so I thought container is removed. Is it wrong or how should I do to remove container.