Article directory
foreword
This article mainly summarizes docker
the commands commonly used in the project development process for subsequent inquiries.
1. Docker image articles
1.1 Pull the image
You only need to give the name and label of the mirror to locate a mirror in the official warehouse. The format of the command is as follows:
docker [image] pull repository[:<tag>]
# Ubuntu镜像的拉取
# docker image pull ubuntu:latest
# docker pull ubuntu:20.04
# docker pull ubuntu
# 加载本地的镜像文件
docker load -i ubuntu_18.04.tar
# 导出镜像
docker save -o ubuntu_18.04.tar ubuntu:18.04
Generally speaking, the label of a mirror
latest
means that the content of the mirror will change to track the changes of the latest version, and the content is unstable.latest
Therefore, in terms of stability, do not ignore the tag information of the image or use the default tagged image in the production environment .
1.2 View image information
List basic information about existing mirrors on the local host:
docker images [ls]
REPOSITORY: 来自于哪个仓库,比如ubuntu表示ubuntu系列的基础镜像
TAG: 镜像的标签信息,比如18.04、latest表示不同的版本信息。标签只是标记,并不能标识镜像内容
IMAGE ID: 镜像唯一标识,如果两个镜像的ID相同,说明它们实际上指向了同一个镜像,只是具有不同标签名称而已
CREATED: 镜像最后的更新时间(创建时间)
SIZE: 镜像大小(优秀的镜像往往体积都较小)
In order to facilitate the use of specific images in subsequent work, you can also use docker tag
commands to arbitrarily add new labels to local images:
docker tag ubuntu:latest myubuntu:latest
# 此时从本地主机上的镜像信息中就可以看到多了一个myubuntu:latest标签的镜像
1.3 Search mirror
Use docker search
the command to search for Docker Hub
images in the official repository:
# 搜索官方提供的带nginx关键字的镜像
docker search --filter=is-official=true nginx
# 搜索所有收藏数超过4且关键词包括tensorflow的镜像
docker search --filter=stars=4 tensorflow
# -f,--filter:过滤输出内容;
# --format:格式化输出内容;
# --limit:限制输出结果个数,默认为25个;
# --no-trunc:不截断输出结果
1.4 Delete mirror
When a mirror is no longer needed, the docker rmi
or docker image rm
command can be used to delete the mirror:
# 通过tag来删除镜像
docker rmi name:tag
# 通过id来删除镜像
docker rmi image-id # 会先尝试删除所有指向该镜像的标签,然后删除该镜像文件本身
# -f,-force:强制删除镜像,即使有容器依赖它(不建议使用)
# -no-prune:不要清理未带标签的父镜像
If there are running containers on the deleted image, the delete operation will not be allowed (default). It is generally not recommended to use -f
parameters to forcefully delete an image that has container dependencies. The correct way is to delete all containers that depend on the image first, and then delete the image.
docker ps # 查看本机上运行中的容器
docker ps -a # 查看本机上存在的所有容器(包括已经停止运行的)
docker stop cotianername/containerid # 停止容器
docker start cotianername/containerid # 启动容器
docker rm cotianername/containerid # 删除容器
docker exec -it containername/containerid /bin/bash # 进入
exit # 退出容器
2. Docker container articles
2.1 Create and start the container
You can docker [container] create
create a new container with the command:
# -it 交互式
docker create -it ubuntu:latest
docker [container] create
The container created using the command is in a stopped state and can be docker [container] start
started using the command:
docker start CONTAINER-ID
Of course, you can also directly use docker [container] run
the command to create and start the container:
# 后台模式启动容器,并输出`Hello world`
docker run ubuntu:latest /bin/echo 'Hello world'
# 等价于先执行docker [container] create命令,再执行docker [container] start命令
# 交互式
docker run -it ubuntu:18.04 /bin/bash
# -t: 让Docker分配一个伪终端(pseudo-tty)并绑定到容器的标准输入上
# -i: 让容器的标准输入保持打开
# 更多的命令选项可以通过man docker-run 命令来查看
2.2 View container output
To get the output information of the container, you can use docker [container] logs
the command:
docker logs CONTAINER-ID
# -details:打印详细信息;
# -f,-follow:持续保持输出;
# -since:输出从某个时间开始的日志;
# -tail:输出最近的若干日志;
# -t,-timestamps:显示时间戳信息;
# -until:输出某个时间之前的日志
2.3 Stopping the container
docker [container] pause
A running container can be paused with the command:
docker pause CONTAINER-ID # 处于paused状态
# 恢复到运行状态
docker unpause CONTAINER-ID
docker [container] stop
To terminate a running container use :
docker stop CONTAINER-ID # 处于stop状态
# 此时,执行docker container prune命令,会自动清除掉所有处于停止状态的容器
A container that is in a terminated state can be docker [container] start
restarted with the command:
docker start CONTAINER-ID
# docker [container] restart命令会将一个运行态的容器先终止,然后再重新启动
2.4 Access to the container
When using -d
parameters, the container will enter the background after startup, and the user cannot see the information in the container or perform operations. At this time, if you need to enter the container for operation, it is recommended to use the official attach
or exec
command.
docker [container] attach CONTAINER
docker [container] exec CONTAINER COMMAND(推荐)
2.5 Delete container
docker [container] rm
A container in terminated or exited state can be removed using the command:
docker rm CONTAINER
3. About docker run
docker run
Indicates creating a new container and running an image.
docker run [option] image-name [the first command to execute after entering the container]
option |
desc |
---|---|
-d |
Run a container in the background and print the container ID |
-p |
Specify port mappinglocal port:docker port |
-P |
random port mapping |
--name |
Give the container a name |
-i |
Run the container in interactive mode |
-t |
After the container starts, it will enter its command line |
-it |
That is, -i with -t the combination of and, the container can be created and logged in, that is, a pseudo-terminal is allocated |
-u |
username |
-v |
file directory mapping |
-w |
working directory inside the container |
docker
Official mirror repository ----> dockerhub
4. Dockerfile
Dockerfile
is a text file containing a series of commands to create Docker
an image.
FROM python:3.8 # 定义基础镜像
MAINTAINER "chatGLM" # 镜像的作者
COPY configs /chatGLM/configs # 复制本地的configs目录到镜像
COPY webui.py /chatGLM/ # 复制本地的webui.py文件到镜像
WORKDIR /chatGLM # 设置工作目录(切换到镜像中的指定路径,可以代替cd指令)
RUN pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple/ --trusted-host pypi.tuna.tsinghua.edu.cn # 执行pip指令
CMD ["python", "-u", "webui.py"] # 指定在启动时需要执行的命令
Dockerfile
Execute in the directory docker build .
to build the image.
docker build -f your-dockerfile -t xxx:latest .