0%

Docker实操

基于Docker Tutorial的Docker 实操

https://www.youtube.com/watch?v=3c-iBn73dDE&t=4967s

Workflow with Docker

image-20220305025005217

Command

docker pull

抓取镜像(默认最新)

docker run postgres:9.6

image-20220305013150795

如果本地没有镜像,则自动下载最新版

docker images

image-20220305013748490

docker run redis

docker run = starts new container with a command

image-20220305014302107

docker ps

ps 相当于 list running containers

列举出运行中的容器

image-20220305014419761

ctrl + c

用于终止容器

image-20220305014629139

image-20220305014740934

docker run -d

-d : in a detached mode

image-20220305015032043

docker stop + ID

docker stop = stops the container

image-20220305015154120

docker start + ID

docker start = starts stopped container

image-20220305015259641

docker ps -a

Docker ps -a = lists running and stopped container

image-20220305015547588

docker run redis:4.0

和pull的小区别:

​ docker run = pulls images and starts containers

image-20220305020133021

同一景象不同版本,

ports

CIONTAINER Port vs HOST Port

  • multiple container can run on host machine
  • laptop has only certain ports available
  • Conflict when same port on host machine

具体ports信息在logs里面可以看到

image-20220305020438704

占用相同端口之后需要人为修改绑定的端口

docker run -p6000:6379 redis

主机端口绑定容器端口

image-20220305021250249

image-20220305021307536

image-20220305021717734

当绑定新端口,就会创造新容器

image-20220305021814569

docker run -p6001:6379 redis4.0

不能使用同一端口了

image-20220305021923857

docker logs + ID/Name

搭配docker ps(查看docker ID)就可以查看logs

docker run -d -p 6001:6379 –name redis-older redis:4.0

–name 重命名

image-20220305022928557

重命名之后查看logs会简单很多

image-20220305023106728

docker exec -it ID或者name /bin/bash

*: bash后面没有/

docker exec 命令用于在运行容器中运行命令。 该命令开始使用 docker exec 只在容器的主进程(PID 1)运行时运行,并且如果容器重新启动,则不重新启动该命令。

如果从这个容器退出,容器不会停止,这就是为什么推荐大家使用 docker exec 的原因。

  • -i : 交互式操作。
  • -t : 终端
  • /bin/bash:放在镜像名后的是命令,这里我们希望有个交互式 Shell,因此用的是 /bin/bash

image-20220305023755035

退出exec

image-20220305023900771

docker run vs start
  • docker run will take an image with a specific version(attributes)

is used to create a new container

​ docker run -d -p –name

  • docker start is working with container

is used to restart a stopped container

docker rm -f ID

删除container

*:

docker container prune

清理掉所有处于终止状态的容器。

docker network ls

image-20220305045344896

docker network create Name

创建docker网络

-e: 设置环境变量

可以在docker hub上找到https://hub.docker.com/_/mongo

start mongodb

docker run -d \

-p 27017:27017 \

-e MONGO_INITDB_ROOT_USERNAME=admin \

-e MONGO_INITDB_ROOT_PASSWORD=password \

–net mongo-network \

–name mongodb \

mongo

start mongo-express

docker run -d \

-p 8081:8081 \

-e ME_CONFIG_MONGODB_ADMINUSERNAME=admin \

-e ME_CONFIG_MONGODB_ADMINPASSWORD=password \

-e ME_CONFIG_MONGODB_SERVER=mongodb \

–net mongo-network \

–name mongo-express\

mongo-express

docker run -d -p 8081:8081 -e ME_CONFIG_MONGODB_ADMINUSERNAME=admin -e ME_CONFIG_MONGODB_ADMINPASSWORD=password -e ME_CONFIG_MONGODB_SERVER=mongodb –net mongo-network –name mongo-express mongo-express

image-20220305051359394

用\换行

mongodb报错

(base) caochao ~ > docker logs mongodb
about to fork child process, waiting until server is ready for connections.
forked process: 31

如何进入?docker desktop

Docker Compose

mongo-docker-compose.yaml

version:’3’

services:

​ mongoldb:

​ image: mongo

​ ports:

​ - 27017:27017

​ environment:

​ - MONGO_INITDB_ROOT_USERNAME=admin

​ ```

mongo-express:

​ image: mongo-express

​ ports:

​ - 8080:8080

​ environment:

​ - ME_CONFIG_MONGODB_ADMINUSERNAME=admin

​ ```

*: the network configuration is not in the docker compose

mongo.yaml
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
28
29
30
version:'3'

services:

mongoldb:

image: mongo

ports:

- 27017:27017

environment:

- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=password

mongo-express:

image: mongo-express

ports:

- 8080:8081

environment:

- ME_CONFIG_MONGODB_ADMINUSERNAME=admin
- ME_CONFIG_MONGODB_ADMINPASSWORD=password
- ME_CONFIG_MONGODB_SERVER=mongodb
docker-compose -f mongo.yaml up

up: start all the containers

Dockerfile

Image Environment Blueprint
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
install node



set MONGO_DB_USERNAME=admin

set MONGO_DB_PWD=password



create /home/app folder



copy curent folder files to /home/app

start the app with: "node server.js"
Dockerfile

COPY - executes on the HOST machine!

CMD - entrypoint command

We can have multiple RUN commands

Every image is always based on another image.

1
2
3
4
5
6
7
8
9
10
FROM node

ENV MONGO_DB_USERNAME=admin \
MONGO_DB_PWD=password

RUN mkdir -p /home/app

COPY ./home/app

CMD["node","server.js"]
docker-node/17/alpine3.15/Dockerfile

https://github.com/nodejs/docker-node/blob/6256caf2c507e7aafdeb8e7f837bab51f46f99e0/17/alpine3.15/Dockerfile

Create a dockerfile (old)
1
2
3
4
5
6
7
8
9
10
11
FROM node:13-alpine

ENV MONGO_DB_USERNAME=admin \
MONGO_DB_PWD=password

RUN mkdir -p /home/app

% . /home/app 有间隔
COPY . /home/app

CMD ["node","server.js"]

:**Name must be “Dockerfile”!*

复制指令,从上下文目录中复制文件或者目录到容器里指定路径。

上下文路径,是指 docker 在构建镜像,有时候想要使用到本机的文件(比如复制),docker build 命令得知这个路径后,会将路径下的所有内容打包。

解析:由于 docker 的运行模式是 C/S。我们本机是 C,docker 引擎是 S。实际的构建过程是在 docker 引擎下完成的,所以这个时候无法用到我们本机的文件。这就需要把我们本机的指定目录下的文件一起打包提供给 docker 引擎使用。

如果未说明最后一个参数,那么默认上下文路径就是 Dockerfile 所在的位置。

Build docker image based on dockerfile

docker build -t my-app:1.0 .

-t: tag

Q: . 代表本次执行的上下文路径

image-20220305062139173

image-20220305062234611

App including Dockerfile is comitted to Git

Jenkins builds image from Dockerfile(like what we did)

docker run my-app:1.0

image-20220305062604551

docker rmi ID
  • rmi删容器

  • rm删镜像

image-20220305062917171

When you adjust the Dockerfile, you MUSt rebuild the Image

docker build -t my-app:1.0 .

image-20220305064916518

Docker Registry

https://docs.aws.amazon.com/ecr/?id=docs_gatewayaws

AWS —> ECR (Fully-managed Docker container registry)

Amazon Elastic Container Registry
Share and deploy container software, publicly or privately

image-20220305070458043

image-20220305070542683

Save different tags (versions) of the sane image

image-20220305070707952

how to push

view push command

image-20220305070832743

push command

image-20220305070901902

You always have to login to private repo! = docker login

Q: jenkin’s server?

Pre-Requisites
  1. AWS Cli needs to be installed

    https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html

  2. Credentials configured

image-20220305071337433

https://docs.aws.amazon.com/AmazonECR/latest/userguide/Registries.html#registry_auth

Set up AWS Credentials and Region for Development

image-20220305074140673

Solution:

image-20220305081827581

两个权限加上:

image-20220305081917401

push

image-20220305082321830image-20220305082338677

push一个更新的版本

  1. docker tag my-app:1.1 429996114549.dkr.ecr.us-east-1.amazonaws.com/my-app:1.1
  2. docker push 429996114549.dkr.ecr.us-east-1.amazonaws.com/my-app:1.1
如何从私人库pull一个image
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
28
29
30
31
32
version:'3'

services:
my-app:
image: 429996114549.dkr.ecr.us-east-1.amazonaws.com/my-app:1.0
ports
mongoldb:

image: mongo

ports:

- 27017:27017

environment:

- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=password

mongo-express:

image: mongo-express

ports:

- 8080:8081

environment:

- ME_CONFIG_MONGODB_ADMINUSERNAME=admin
- ME_CONFIG_MONGODB_ADMINPASSWORD=password
- ME_CONFIG_MONGODB_SERVER=mongodb