How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ MySQL edition ~

Contents

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.

Contents up to the last time

How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ Vue edition ~

Work procedure

  1. Creating a DB container
  2. Input test data
  3. Access to MySQL

1. Creating a DB container

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>

2. Input test data

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.

3. Access MySQL

This is the end of preparations. From here, check if you can actually access and get the data.

DB container build

$ docker-compose build

DB container startup

$ docker-compose up -d

Container start confirmation

$ docker ps

OK if db_container is running!

Access the container

$ docker exec -it db_container sh

Login to MySQL

$ 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!

Database check

SHOW DATABASES;

Success if the prepared database [test_db] exists!

スクリーンショット 2020-09-20 17.21.39.png

Specify the database to use

USE test_db;

Check the table

SHOW TABLES;

If the created [test] table exists, it's OK! スクリーンショット 2020-09-20 17.22.55.png

Check the data in the test table

SELECT * FROM test;

If the list of data saved in the test table is displayed, it is completed without any problem!

スクリーンショット 2020-09-20 17.24.06.png

If you can confirm so far, you can access MySQL without any problem! Thank you for your hard work!

Exit from mysql

mysql> exit
Bye 
# //If this happens, you are logged out of MySQL and accessing the container.

Get out of the container

exit

Stop container

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!

next time

How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ Express edition ~

reference

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

How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ MySQL edition ~
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ Express ~
How to build an environment of [TypeScript + Vue + Express + MySQL] with Docker ~ Vue edition ~
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ Sequelize ~
How to build Rails + Vue + MySQL environment with Docker [2020/09 latest version]
How to build Rails 6 environment with Docker
[Rails] How to build an environment with Docker
[Road _node.js_1-1] Road to build Node.js Express MySQL environment using Docker
How to build docker environment with Gradle for intelliJ
How to build Rails, Postgres, ElasticSearch development environment with Docker
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
[Node.js express Docker] How to define Docker environment variables and load them with node.js
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
Easily build a Vue.js environment with Docker + Vue CLI
[Docker environment] How to deal with ActiveSupport :: MessageEncryptor :: InvalidMessage
Build docker environment with WSL
How to build Java development environment with VS Code
Build Rails (API) x MySQL x Nuxt.js environment with Docker
[Rails] [Docker] Copy and paste is OK! How to build a Rails development environment with Docker
How to quit Docker for Mac and build a Docker development environment with Ubuntu + Vagrant
How to get started with Gatsby (TypeScript) x Netlify x Docker
How to use mysql with M1 mac Docker preview version
Build a Node-RED environment with Docker to move and understand
Rails + MySQL environment construction with Docker
Build Couchbase local environment with Docker
Build a Node.js environment with Docker
Build PlantUML environment with VSCode + Docker
Build Rails environment with Docker Compose
[Environment construction with Docker] Rails 6 & MySQL 8
Update MySQL from 5.7 to 8.0 with Docker
Build docker + laravel environment with laradock
How to build CloudStack using Docker
How to start Camunda with Docker
Build apache7.4 + mysql8 environment with Docker (with initial data) (your own memo)
I tried to build the environment of PlantUML Server with Docker
How to install Pry after building Rails development environment with Docker
Build a development environment for Django + MySQL + nginx with Docker Compose
How to share files with Docker Toolbox
Build a PureScript development environment with Docker
Create Rails 6 + MySQL environment with Docker compose
[Rails] How to use rails console with docker
Deploy to heroku with Docker (Rails 6, MySQL)
Edit Mysql with commands in Docker environment
Create a MySQL environment with Docker from 0-> 1
[Docker] Create Node.js + express + webpack environment with Docker
How to build an environment with Docker, which is the minimum required to start a Rails application
Laravel + MySQL + phpMyadmin environment construction with Docker
Build a Wordpress development environment with Docker
Build Nuxt TypeScript + Vuetify environment with docker-compose
[Docker] Build Jupyter Lab execution environment with Docker
Build an environment with Docker on AWS
Build TensorFlow operation check environment with Docker
How to run Blazor (C #) with Docker
How to build a Ruby on Rails environment using Docker (for Docker beginners)
How to execute with commands of normal development language in Docker development environment
I tried to build a Firebase application development environment with Docker in 2020
01. I tried to build an environment with SpringBoot + IntelliJ + MySQL (MyBatis) (Windows10)
When I tried to build an environment of PHP7.4 + Apache + MySQL with Docker, I got stuck [Windows & Mac]
Build a Laravel / Docker environment with VSCode devcontainer
Build a WordPress development environment quickly with Docker
How to give your image to someone with docker