In the previous article, I created a container for the app. This time, we will build a MySQL container on Docker and perform actual data input and access.
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ Vue edition ~
We will create a db container separately from the app container created last time. The final directory structure is as follows.
├─ app
├─ db
│ └─ conf
│ └─ my.cnf //add to
│ └─ init_db
│ └─ test.sql //add to
├─ docker
│ └─ app
│ └─ db
│ └─ Dockerfile //add to
├─ .env //add to
└─ docker-compose.yml //Edit
docker-compose.yml
version: "3"
services:
app:
container_name: app_container
build: ./docker/app
ports:
- 8080:8080
volumes:
- ./app:/app
stdin_open: true
tty: true
environment:
TZ: Asia/Tokyo
command: yarn serve
#Do not edit the previous application container
#Add the following
db:
container_name: db_container
build: ./docker/db
image: mysql:5.7
ports:
- 3306:3306
volumes:
- ./db/conf/my.cnf:/etc/mysql/conf.d/mysql.cnf #Bind MySQL settings
- ./db/init_db:/docker-entrypoint-initdb.d #Bind sql file for initial data input
- test_data:/var/lib/mysql #Bind persisted data
environment:
- MYSQL_DATABASE=${MYSQL_DATABASE} #Read various settings from the environment variables of the container
- MYSQL_USER=${MYSQL_USER}
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
- TZ="Asia/Tokyo"
volumes:
test_data: #Data persistence
The setting to launch the app container and the db container at the same time is completed above. The database name, user name, and password to be accessed are executed by referring to the environment variables of the container from [environment].
/docker/db/Dockerfile
FROM mysql
EXPOSE 3306
CMD ["mysqld"]
Set the port to 3306.
db/conf/my.cnf
[mysqld]
character-set-server=utf8
[mysql]
default-character-set=utf8
[client]
default-character-set=utf8
Set the MySQL character code.
.env
MYSQL_DATABASE=test_db //Set the database name described later
MYSQL_USER={Set an appropriate user name}
MYSQL_PASSWORD={Set password for MySQL access}
MYSQL_ROOT_PASSWORD={Set password for MySQL access}
What is set here is referenced when the container is started and is set in the environment variable of the container. The login password will be the password set above.
* Exclude .env files with .gitignore so that they are not included in Git. Don't push if you make a mistake! </ font>
db/init_db/test.sql
DROP DATABASE IF EXISTS test_db;
CREATE DATABASE test_db;
USE test_db;
DROP TABLE IF EXISTS test;
CREATE TABLE test (
id int NOT NULL AUTO_INCREMENT primary key,
name varchar(30),
description varchar(255)
);
INSERT INTO test (id, name, description) VALUES (1, 'test1', 'Test data 1.');
INSERT INTO test (id, name, description) VALUES (2, 'test2', 'Test data 2.');
INSERT INTO test (id, name, description) VALUES (3, 'test3', 'Test data 3.');
INSERT INTO test (id, name, description) VALUES (4, 'test4', 'Test data 4.');
INSERT INTO test (id, name, description) VALUES (5, 'test5', 'Test data 5.');
INSERT INTO test (id, name, description) VALUES (6, 'test6', 'Test data 6.');
INSERT INTO test (id, name, description) VALUES (7, 'test7', 'Test data 7.');
INSERT INTO test (id, name, description) VALUES (8, 'test8', 'Test data 8.');
INSERT INTO test (id, name, description) VALUES (9, 'test9', 'Test data 9.');
INSERT INTO test (id, name, description) VALUES (10, 'test10', 'Test data 10.');
Create test_db database> test table as data for confirmation. Insert data such as ID, Name, Description in the column as appropriate.
This is the end of preparations. From here, check if you can actually access and get the data.
$ docker-compose build
$ docker-compose up -d
$ docker ps
OK if db_container is running!
$ docker exec -it db_container sh
$ mysql -u root -p -h 127.0.0.1
Enter password: // .Enter the password described in env
mysql> //If this comes out, access to Mysql is complete!
SHOW DATABASES;
Success if the prepared database [test_db] exists!
USE test_db;
SHOW TABLES;
If the created [test] table exists, it's OK!
SELECT * FROM test;
If the list of data saved in the test table is displayed, it is completed without any problem!
If you can confirm so far, you can access MySQL without any problem! Thank you for your hard work!
mysql> exit
Bye
# //If this happens, you are logged out of MySQL and accessing the container.
exit
docker-compose stop
Thank you for your hard work! At this point, you have completed the environment construction that allows you to launch MySQL on the Docker container and insert and access data! Now that we have an app container and a database to actually operate, we will create an API server container to operate data next time!
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ Express edition ~
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ Vue edition ~ How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ Sequelize ~
Recommended Posts