Docker Tutorial for Beginners

Posted by Laiaike on 2023-09-24

Docker Tutorial for Beginners

Course: Docker Tutorial for Beginners

容器的优点

容器与虚拟机区别

  1. Docker image更小
  2. 更快运行
  3. 运行的层面不同

基本操作

Container is a running environment for image.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
docker stop [id]
docker run -d [image] = docker pull + docker run
docker ps -a //查询所有的image起始状态,不止是正在运行的
docker run -p6000(laptop):6379(container) redis
docker logs admiring_einstein(name) or (id)
docker run --name redisname redis
docker rm rm [container id] //移除运行容器
docker rmi [image id] //移除image

docker exec -it (id) /bin/bash //debug时好用 base on lightweight Linux distributions

docker run //创建镜像
docker start [id] //不是image而是container,开启之前stop的镜像,而不是重新创建

docker network ls
docker network create mongo-network

项目

  1. JS and Nodejs application
  2. MongoDB Docker Container

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
docker pull mongo-express
docker pull mongo

docker network create mongo-network

// 通过container name连接mongo和mongo express
docker run -p 27017:27017 -d -e MONGO_INITDB_ROOT_USERNAME=root -e MONGO_INITDB_ROOT_PASSWORD=root --name mongodb --network mongo-network mongo

docker run -d\
--name mongo-express \
-p 8082:8081 \
-e ME_CONFIG_MONGODB_SERVER=mongodb \
-e ME_CONFIG_MONGODB_ADMINUSERNAME=root \
-e ME_CONFIG_MONGODB_ADMINPASSWORD=root \
--net mongo-network \
mongo-express
// 放在同一个网络下

mongo和mongo-express的连接只需要container name不需要localhost or port,因为他们处于同一网络。

Docker Compose

1
2
3
4
5
// mongo.yaml为docker compose文件
// run
docker-compose -f mongo.yaml up
// stop
docker-compose -f mongo.yaml down

Dockerfile

builds JS App & create Docker image

ENV 也可以写在Dockerfile但是建议写在docker-compose

RUN (command):在container中执行

COPY :在host中执行

CMD:程序入口 (RUN可以有多条)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
FROM node:13-alpine

ENV MONGO_DB_USERNAME=root \
MONGO_DB_PWD=root

RUN mkdir -p /home/app

COPY ./app /home/app

# set default dir so that next commands executes in /home/app dir
WORKDIR /home/app

# will execute npm install in /home/app because of WORKDIR
RUN npm install

# no need for /home/app/server.js because of WORKDIR
CMD ["node", "server.js"]
1
2
// 在dockerfile同目录文件夹指定
docker build -t my-app:1.0 .(current directory)

jenkinsbuild image base on dockerfile

修改dockerfile后需要rebuild image

1
2
3
4
5
6
7
8
9
10
docker run my-app:1.0
docker exec -it (id) sh(or bash)
exit (退出docker terminal)

docker run -i -t -v /root/software/:/mnt/software/ 9f38484d220f /bin/bash
参数解析
  -i:表示以“交互模式”运行容器
  -t:表示容器启动后会进入其命令行
  -v:表示需要将本地哪个目录挂载到容器中,格式:-v <宿主机目录>:<容器目录>
  /bin/bash:一旦容器启动,需要执行的命令,当前使用 "/bin/bash", 表示启动后直接进bash shell

一般会和其他images一起启动

Docker Registry

这里使用阿里云容器镜像服务 ACR

1
2
3
4
5
tagname:直接从阿里云copy

$ docker login --username=username registry.cn-hangzhou.aliyuncs.com
$ docker tag [ImageId] tagname
$ docker push tagname
1
docker-compose -f mongo.yaml up

Docker Volumes

  • restart ——> data is lost (Docker Volumes for data persistence)

  • 3 Volume Types:推荐使用Named Volumes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
version: '3'
services:
my-app:
image: ${docker-registry}/my-app:1.0
ports:
- 3000:3000
mongodb:
image: mongo
ports:
- 27017:27017
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=root
volumes:
- mongo-data:/data/db
mongo-express:
image: mongo-express
restart: always # fixes MongoNetworkError when mongodb is not ready when mongo-express starts
ports:
- 8082:8081
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=root
- ME_CONFIG_MONGODB_ADMINPASSWORD=root
- ME_CONFIG_MONGODB_SERVER=mongodb
volumes:
mongo-data:
driver: local
  • 数据存放位置
    • 容器中:根据数据库不同,默认存放位置不同
      • Mongo: /data/db
      • Mysql: /var/lib/mysql
      • Postgre: /var/lib/postgresql/data
    • 宿主机中: