Excerpts from the course bar learning materials
1. Getting started:
Will be ordered
- Pull mirror:
docker pull nginx
- View mirror:
docker images/docker images nginx
- Start image (map WWW directory to nginx/ HTML):
- Front desk startup:
docker run -p 8000:80 -v $PWD/www:/usr/share/nginx/html nginx
- Background startup:
docker run -d -p 8000:80 -v $PWD/www:/usr/share/nginx/html nginx
- Front desk startup:
- The container exists:
Docker start < container ID>
- Stop container:
Docker stop < container ID>
- To view the running process:
docker ps
- Enter the container:
Docker exec it < container ID> /bin/bash
- Delete container:
Docker rm < container ID>
2. Customize the image
Case 1 (Nginx) :
- Create a Dockerfile file
# Mirror content
FROM nginx:latest
RUN echo '<h1>Hello Docker</h1>' > /usr/share/nginx/html/index.html
Copy the code
- compile
//docker build-t nginx:ospoonCopy the code
- Start the
docker run -p 8000:80 nginx:ospoon
Copy the code
Case 2 (Node) :
- Create a directory:
mkdir node-koa
- Initialization:
cd node-koa & npm i koa -S
- 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
- 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
- Compiling images:
docker build -t node-koa .
- Activation:
docker run -d -p 3000:3000 node-koa
Case 3 (pm2) :
- Copy Case 2:
cp -R node-koa pm2-koa
- 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
- 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
- Compiling images:
docker build -t pm2-koa .
- Activation:
docker run -d -p 3000:3000 pm2-koa
3. docker-compose
Responsible for implementing fast choreography of Docker container clusters
Case study:
- 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
- Run:
docker-compose up