基于Docker Tutorial的Docker 实操
https://www.youtube.com/watch?v=3c-iBn73dDE&t=4967s
Workflow with Docker
Command
docker pull
抓取镜像(默认最新)
docker run postgres:9.6
如果本地没有镜像,则自动下载最新版
docker images
docker run redis
docker run = starts new container with a command
docker ps
ps 相当于 list running containers
列举出运行中的容器
ctrl + c
用于终止容器
docker run -d
-d : in a detached mode
docker stop + ID
docker stop = stops the container
docker start + ID
docker start = starts stopped container
docker ps -a
Docker ps -a = lists running and stopped container
docker run redis:4.0
和pull的小区别:
docker run = pulls images and starts containers
同一景象不同版本,
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里面可以看到
占用相同端口之后需要人为修改绑定的端口
docker run -p6000:6379 redis
主机端口绑定容器端口
当绑定新端口,就会创造新容器
docker run -p6001:6379 redis4.0
不能使用同一端口了
docker logs + ID/Name
搭配docker ps(查看docker ID)就可以查看logs
docker run -d -p 6001:6379 –name redis-older redis:4.0
–name 重命名
重命名之后查看logs会简单很多
docker exec -it ID或者name /bin/bash
*: bash后面没有/
docker exec 命令用于在运行容器中运行命令。 该命令开始使用 docker exec 只在容器的主进程(PID 1)运行时运行,并且如果容器重新启动,则不重新启动该命令。
如果从这个容器退出,容器不会停止,这就是为什么推荐大家使用 docker exec 的原因。
- -i : 交互式操作。
- -t : 终端
- /bin/bash:放在镜像名后的是命令,这里我们希望有个交互式 Shell,因此用的是 /bin/bash
退出exec
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
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
用\换行
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 | version:'3' |
docker-compose -f mongo.yaml up
up: start all the containers
Dockerfile
Image Environment Blueprint
1 | install node |
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 | FROM node |
docker-node/17/alpine3.15/Dockerfile
Create a dockerfile (old)
1 | FROM node:13-alpine |
:**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: . 代表本次执行的上下文路径
App including Dockerfile is comitted to Git
Jenkins builds image from Dockerfile(like what we did)
docker run my-app:1.0
docker rmi ID
rmi删容器
rm删镜像
When you adjust the Dockerfile, you MUSt rebuild the Image
docker build -t my-app:1.0 .
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
Save different tags (versions) of the sane image
how to push
view push command
push command
You always have to login to private repo! = docker login
Q: jenkin’s server?
Pre-Requisites
AWS Cli needs to be installed
https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html
Credentials configured
https://docs.aws.amazon.com/AmazonECR/latest/userguide/Registries.html#registry_auth
Set up AWS Credentials and Region for Development
Solution:
两个权限加上:
push
push一个更新的版本
- docker tag my-app:1.1 429996114549.dkr.ecr.us-east-1.amazonaws.com/my-app:1.1
- docker push 429996114549.dkr.ecr.us-east-1.amazonaws.com/my-app:1.1
如何从私人库pull一个image
1 | version:'3' |