Excerpts from the course bar learning materials

1. Getting started:

Will be ordered

  1. Pull mirror:docker pull nginx
  2. View mirror:docker images/docker images nginx
  3. Start image (map WWW directory to nginx/ HTML):
    1. Front desk startup:docker run -p 8000:80 -v $PWD/www:/usr/share/nginx/html nginx
    2. Background startup:docker run -d -p 8000:80 -v $PWD/www:/usr/share/nginx/html nginx
  4. The container exists:Docker start < container ID>
  5. Stop container:Docker stop < container ID>
  6. To view the running process:docker ps
  7. Enter the container:Docker exec it < container ID> /bin/bash
  8. Delete container:Docker rm < container ID>

2. Customize the image

Case 1 (Nginx) :

  1. Create a Dockerfile file
# Mirror content
FROM nginx:latest
RUN echo '<h1>Hello Docker</h1>' > /usr/share/nginx/html/index.html
Copy the code
  1. compile
//docker build-t nginx:ospoonCopy the code
  1. Start the
docker run -p 8000:80 nginx:ospoon
Copy the code

Case 2 (Node) :

  1. Create a directory:mkdir node-koa
  2. Initialization:cd node-koa & npm i koa -S
  3. Creating a KOA service:
const Koa = require("koa");
const app = new Koa();
app.use((ctx) => {
  ctx.body = "Hello NodeJs";
});
app.listen(3000, () => {
  console.log("app started at 3000");
});

Copy the code
  1. Write Dockerfile
FROM node:10-alpine
# copy current directory to container /app directory
ADD . /app/
Go to the working directory
WORKDIR /app
# docker build process run
RUN npm install
# Exposed port
EXPOSE 3000
The container is executed at runtime
CMD ["node"."app.js"]
Copy the code
  1. Compiling images:docker build -t node-koa .
  2. Activation:docker run -d -p 3000:3000 node-koa

Case 3 (pm2) :

  1. Copy Case 2:cp -R node-koa pm2-koa
  2. Write PM2 related YML files
apps:
  - script: app.js
    # processes
    instances: 2
    # monitor mode
    watch: true
    env:
      # Runtime environment
      NODE_ENV: production
Copy the code
  1. Write Dockerfile
FROM keymetrics/pm2:latest-alpine
# Working directory
WORKDIR /usr/src/app
ADD . /usr/src/app
# connect two commands with && \
RUN npm config set registry https://registry.npm.taobao.org/ && \
    npm i
# Exposed port
EXPOSE 3000
The pm2 command is used in docker
CMD ["pm2-runtime"."start"."process.yml"]
Copy the code
  1. Compiling images:docker build -t pm2-koa .
  2. Activation:docker run -d -p 3000:3000 pm2-koa

3. docker-compose

Responsible for implementing fast choreography of Docker container clusters

Case study:

  1. Write the configuration file: docker-comemage.yml
# docker-compose.yml mongo + mongo-express
version: '3.1'
services:
  mongo:
    image: mongo
    restart: always
    ports:
      - 27017: 27017
  mongo-express:
    image: mongo-express
    restart: always
    ports:
      - 8081: 8081
Copy the code
  1. Run:docker-compose up