[Road _node.js_1-1] Road to build Node.js Express MySQL environment using Docker

Completed image composition nodoDocker (root directory) a03c9651ffb5bba1c53532460a232639.png

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

Create a container for your application

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

Add container for MySQL

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

[Road _node.js_1-1] Road to build Node.js Express MySQL environment using Docker
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ Express ~
[App development 0.5] [Node.js express Docker] Build an environment for Node.js Express MongoDB using Docker
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ MySQL edition ~
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ Sequelize ~
Try to build a Java development environment using Docker
I tried to build an environment using Docker (beginner)
[App development 1] [Node.js express Docker] Build an environment for Node.js Express MongoDB (mongoose) using Docker [December 2020]
How to build an environment of [TypeScript + Vue + Express + MySQL] with Docker ~ Vue edition ~
Build a Node.js environment with Docker
How to build CloudStack using Docker
I tried to build the environment little by little using docker
[Docker] Create Node.js + express + webpack environment with Docker
How to build Rails + Vue + MySQL environment with Docker [2020/09 latest version]
How to build Rails 6 environment with Docker
How to build a Ruby on Rails environment using Docker (for Docker beginners)
[Node.js express Docker] How to define Docker environment variables and load them with node.js
[Rails] How to build an environment with Docker
How to build docker environment with Gradle for intelliJ
Build Rails (API) x MySQL x Nuxt.js environment with Docker
[2021] Build a Docker + Vagrant environment for using React / TypeScript
MySQL 5.7 (Docker) environment construction memo
Build docker environment with WSL
multi-project docker build using jib
How to build Rails, Postgres, ElasticSearch development environment with Docker
Build a Node-RED environment with Docker to move and understand
I tried to build the environment of WSL2 + Docker + VSCode
Rails + MySQL environment construction with Docker
[Node.js] Docker-compose up makes it easy to build a development environment
Node.js environment construction with Docker Compose
Build Couchbase local environment with Docker
Build apache7.4 + mysql8 environment with Docker (with initial data) (your own memo)
Build PlantUML environment with VSCode + Docker
I tried to build the environment of PlantUML Server with Docker
Build environment with vue.js + rails + docker
Build Rails environment with Docker Compose
Build a browser test environment using Capybara in the Docker development environment
[Environment construction with Docker] Rails 6 & MySQL 8
Build a development environment for Django + MySQL + nginx with Docker Compose
Pass environment variables to docker container
Build Unity development environment on docker
Update MySQL from 5.7 to 8.0 with Docker
I tried to build a laravel operating environment while remembering Docker
Build docker + laravel environment with laradock
Check MySQL logs in Docker environment
When I tried to build an environment of PHP7.4 + Apache + MySQL with Docker, I got stuck [Windows & Mac]
Build an environment of "API development + API verification using Swagger UI" with Docker
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)
How to build an environment for any version of Ruby using rbenv
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
Complete roadmap for building environment up to Docker + rails6 + MySQL + bootstrap, jquery
Install PHP7.4 to build CentOS7 Laravel environment
Create a web environment quickly using Docker
SQL statement learning ~ Environment construction ~ Docker + MySQL
Build a PureScript development environment with Docker
Create Rails 6 + MySQL environment with Docker compose
[Docker] Building an environment to use Hugo
Migrate existing Rails 6 apps to Docker environment
Deploy to heroku with Docker (Rails 6, MySQL)