Completed image composition nodoDocker (root directory)
Dockerfile: File for creating an application (node.js) container
app.env: File that describes the connection settings with mysql
docker-compose.yml: yml to manage node.js container and mysql container
src: Application body
First, create a temporary container for the application and create a template for the application. The template of the application is mounted on the host side, and docker-compose is created using the template.
Under nodeDocker Create docker-compose.yml
docker-compose.yml
version: '3'
services:
app:
#Specify the image to boot (here Node.Official image of js v12)
image: node:12
#Set environment variables
environment:
- DEBUG=app:*
tty: true
#Host side port:Container port
ports:
- '3000:3000'
#Mount the folder that stores the source code
#(On the host side./src of the container/Mount on app)
volumes:
- ./src:/app
#Specify the current folder at startup
working_dir: /app
#Specify the command to be executed after startup
command: npm start
host.
#Temporarily start the container (--Delete after stopping with rm. After starting the container, enter bash)
docker-compose run --rm app /bin/bash
container.
# express-Generate application template with generator
npx express-generator
#Install dependent packages
npm install
#Exit the container (this temporary container will be deleted)
exit
docker-compose.yml
volumes:
- ./src:/app
According to this description,. (The directory where docker-compose.yml is located. That is, it is mounted in the src directory under the nodoDocker directory, so the template created in the src directory on the host side remains.
Start the container
host.
docker-compose up
docker-compose.yml
command: npm start
According to the description of, the Express.js application will start automatically after the container is started. http://localhost:3000/ Confirm with.
Stop container
host.
#Temporarily start the container again
docker-compose run --rm app /bin/bash
#Install a tool called "nodemon" that detects changes in the source code and restarts the application when needed.
#You don't have to stop and restart the container every time you make a change.
npm install -D nodemon
Fixed package.json
src/package.json
"scripts": {
"dev": "nodemon ./bin/www",
"start": "node ./bin/www"
},
Edit docker-compose.yml file
docker-compose.yml
(Change before)
command: npm start
(After change)
command: npm run dev
Added mysql settings to docker-compose.yml
docker-compose.yml
version: '3'
services:
mysql:
image: mysql:5.7
env_file: ./mysql/mysql.env
#Environment variable settings. Set the time zone to Japan time.
environment:
- TZ=Asia/Tokyo
ports:
- '3306:3306'
volumes:
#Override the mysql default configuration file.:Read-only setting from the container side with ro.
- ./mysql/conf:/etc/mysql/conf.d/:ro
#By mounting it on mysqldata, the data will remain even if the container is deleted.
- mysqldata:/var/lib/mysql
networks:
- backend
app:
image: node:12
env_file: ./app.env
#Environment variable settings. Set the time zone to Japan time.
environment:
- TZ=Asia/Tokyo
- DEBUG=app:*
tty: true
ports:
- '3000:3000'
volumes:
- ./src:/app
working_dir: /app
command: npm run dev
networks:
- backend
#Since it depends on mysql, the app container is created after creating the mysql container.
depends_on:
- mysql
networks:
backend:
volumes:
mysqldata:
Create my.conf (located in /etc/mysql/conf.d/ on the container side)
mysql/conf/my.conf
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
[mysqldump]
default-character-set=utf8mb4
[mysqld]
character-set-server=utf8mb4
collation-server=utf8mb4_bin
lower_case_table_names=1
# Enable access from the host machine.
bind-address=0.0.0.0
Create a mysql.env file in the mysql folder. (Env_file: The contents are set in the environment variable of mysql on the container side by the description of ./mysql/mysql.env)
mysql/mysql.env
MYSQL_ROOT_HOST=%
MYSQL_ROOT_PASSWORD=(Root password)
MYSQL_USER=(username)
MYSQL_PASSWORD=(password)
MYSQL_DATABASE=todo
Create app.env file (application container environment variable settings)
app.env
MYSQL_SERVER=mysql
MYSQL_USER=(username)
MYSQL_PASSWORD=(password)
MYSQL_DATABASE=todo
docker-compose.yml
version: '3'
services:
mysql:
image: mysql:5.7
env_file: ./mysql/mysql.env
environment:
- TZ=Asia/Tokyo
ports:
- '3306:3306'
volumes:
- ./mysql/conf:/etc/mysql/conf.d/:ro
- mysqldata:/var/lib/mysql
networks:
- backend
app:
image: node:12
env_file: ./app.env
environment:
- TZ=Asia/Tokyo
- DEBUG=app:*
tty: true
ports:
- '3000:3000'
volumes:
- ./src:/app
working_dir: /app
command: npm run dev
networks:
- backend
depends_on:
- mysql
#Create a network to use. docker-In case of compose, the name is resolved using the name under service, so app and mysql are automatically connected.
networks:
backend:
volumes:
mysqldata:
Log in to mysql
host.
docker-compose exec mysql mysql -uroot -p
Can not,,,
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
Results of various trials
host.
docker-compose down --volumes
After deleting the volume, I was able to log in. The cause is unknown.
Place the dockerfile that describes the installation directly under the root to use ejs for the template engine.
#node.Pull the image of js.
FROM node:12
#Make the working directory the application directory.
WORKDIR /src
Install ejs.
RUN npm install ejs
Fixed because the application container is built with Dockerfile (originally pulling node: 12 image)
docker-compose.yml
(Change before)
image: node:12
(After change)
build: .
Completed for the time being.
Recommended Posts