Docker packages nodejs project and database

Keywords: Linux Docker MySQL SQL JSON

To read this article, you must know the basic docker. This article only provides ideas and part of the code, not responsible for teaching

All orders must be handled carefully! look before you leap

command

  • Stop all running containers
docker stop $(docker ps -a -q)
  • Delete all containers
docker rm $(docker ps -a -q)
  • Using docker compose to start the container
docker-compose up -d
  • Use docker compose to close the container
docker-compose down
  • View Log
docker logs ${container id}

Deploying mysql

docker-compose

services:
    mysql:
        network_mode: "host"
        environment:
            MYSQL_ROOT_PASSWORD: "yourpassword"
            MYSQL_USER: 'test'
            MYSQL_PASS: 'yourpassword'
        image: "docker.io/mysql:latest" 
        restart: always // Try again if you can't connect
        // Depends on: select to depend on a service. The dependent service will be loaded first
        //    - 'sss'
        volumes:
            - "./db:/var/lib/mysql"
            - "./conf/my.cnf:/etc/my.cnf"
            - "./init:/docker-entrypoint-initdb.d/"
        ports:
            - "3306:33060"

Automatically loading sql statements to initialize the database

In the official image of mysql, the sql file in the folder under docker-entrypoint-initdb.d will be executed when loading, which is used for initialization

If there is data in the mysql data folder, the sql file will not be loaded

EGG project with docker: directly interrupted

Because egg scripts has its own set of daemons, remove the daemon parameter

"start": "egg-scripts start --title=egg-server-broken-chain",

Deploy NodeJs project

  • New Dockerfile file
FROM node:10.13-alpine
ENV NODE_ENV production
WORKDIR /usr/src/app
COPY ["package.json", "package-lock.json*", "npm-shrinkwrap.json*", "./"]
RUN npm install -g cnpm
RUN cnpm install --production --silent
COPY . .
EXPOSE 7001
CMD [ "npm", "start"]
  • It is very convenient to add information in docker-compose.yml and manage multiple images with docker-compose

The final docker-compose.yml file

version: '2'

services:
  broken-chain:
    image: broken-chain
    build: .
    environment:
      NODE_ENV: production
    ports:
      - 7001:7001
    depends_on:
      - "mysql"
    restart: always
  mysql:
    environment:
        MYSQL_ROOT_PASSWORD: "123"
    image: "docker.io/mysql:5.6" 
    volumes:
        - "./mysql/init:/docker-entrypoint-initdb.d/"
    ports:
        - "3306:3306"

Publishing mirroring

  • Log in to Dockerhub
docker login
  • Label the image
docker tag ${image id} ${user name} / ${image name}: ${tag tag}
  • push to dockerhub
docker push ${username} / ${image name}: ${tag label}

Last

In the end, you need to give your project to others, the kind that you can use out of the box. You need

  • New directory structure
- init
   - xxx.sql //Initialized sql statement
- docker-compose.yml
  • docker-compose.yml is as follows
version: '2'

services:
  broken-chain:
    image: ${Your user name}/${Your mirror image} // This time, pull your image on dockerhub directly
    // build:
    environment:
      NODE_ENV: production
    ports:
      - 7001:7001
    depends_on:
      - "mysql"
    restart: always
  mysql:
    environment:
        MYSQL_ROOT_PASSWORD: "123"
    image: "docker.io/mysql:5.6" 
    volumes:
        - "./mysql/init:/docker-entrypoint-initdb.d/"
    ports:
        - "3306:3306"
  • Compress zip to others

Run with one key command (database + project)

docker-compose up -d
  • Comfortable.

Posted by dreamwest on Sat, 16 Nov 2019 08:40:37 -0800