docker常用命令:
容器生命周期管理
run 创建一个新的容器并运行一个命令
docker run [OPTIONS] IMAGE [COMMAND] [ARG…]
这里有一个常见的docker run示例:
sudo docker rm -f test
docker run -itd \
--restart=always \
--gpus all \
-e NVIDIA_DRIVER_CAPABILITIES=compute,utility,video \
--name test \
-p 8080:8080 -p 9992:9992 \
-v /etc/localtime:/etc/localtime:ro \
-v /home/shenyan/project/flow-depends:/flowserver/flow-depends \
-v /home/shenyan/project/flow-data:/flowserver/flow-data \
-v /home/shenyan/project/flowserver/flowsvr9982:/flowserver/flowsvr \
-e ZOOKEEPER_CONFIG=/172.0.8.21:9982/2 \
-e DEPENDS_ROOT=/flowserver/flow-depends \
-w /flowserver/flowsvr flow-image:v3.0 \
/bin/bash ./run-server.sh
start/stop/restart 启动/停止/重启容器
docker start [OPTIONS] CONTAINER [CONTAINER…]
kill 杀掉一个运行中的容器
docker kill [OPTIONS] CONTAINER [CONTAINER…]
rm 删除一个或多个容器
docker rm [OPTIONS] CONTAINER [CONTAINER…]
docker rm -f test
pause/unpause 暂停/恢复容器中所有的进程
docker pause [OPTIONS] CONTAINER [CONTAINER…]
create 创建一个新的容器但不启动它
docker create [OPTIONS] IMAGE [COMMAND] [ARG…]
exec 在运行的容器中执行命令启动新进程(一般用以监控或查询)
docker exec [OPTIONS] CONTAINER COMMAND [ARG…]
docker exec -it test sh
进入docker后运行输入bash即可获取bash窗口
容器操作
ps 列出容器
docker ps [OPTIONS]
docker ps -a
inspect 获取容器/镜像的元数据
docker inspect [OPTIONS] NAME|ID [NAME|ID…]
top 查看容器中运行的进程信息,支持 ps 命令参数
docker top [OPTIONS] CONTAINER [ps OPTIONS]
attach 连接到正在运行中的容器
docker attach [OPTIONS] CONTAINER
这个和exec的区别是attach直接可以附着到你程序窗口中去
events 从服务器获取实时事件
docker events [OPTIONS]
logs 获取容器的日志
docker logs [OPTIONS] CONTAINER
(-t日志添加时间戳 -f跟踪日志变动 --tail n/all从倒数n条处打印)
wait 阻塞运行直到容器停止,然后打印出它的退出代码
docker wait [OPTIONS] CONTAINER [CONTAINER…]
export 将文件系统作为一个tar归档文件导出到STDOUT
docker export [OPTIONS] CONTAINER
port 列出指定的容器的端口映射,或者查找将PRIVATE_PORT NAT到面向公众的端口
docker port [OPTIONS] CONTAINER [PRIVATE_PORT[/PROTO]]
容器rootfs命令
commit 从容器创建一个新的镜像【像代码版本控制一样控制业务系统及其环境的版本】
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
(-a 作者 -m 注释 )
cp 用于容器与主机之间的数据拷贝
docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH| docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
diff 检查容器里文件结构的更改
docker diff [OPTIONS] CONTAINER
镜像仓库
login/logout 登陆/注销到一个Docker镜像仓库
docker login -u 用户名 -p 密码 [SERVER] docker logout 比如登录一个私有仓库 sudo docker login https://reg.trasai.net 然后会提示输入用户名和密码
pull 从镜像仓库中拉取或者更新指定镜像
docker pull [OPTIONS] NAME[:TAG|@DIGEST]
如果在私有仓库中下载,则首先要登录到私有仓库,登录如上所示。
然后下载
sudo docker pull reg.transai.net/dev/auto/crystal-image-devel:v1.0
push 将本地的镜像上传到镜像仓库
docker push [OPTIONS] NAME[:TAG] 注意在push之前一定要登录到私有仓库,然后才可以将本地的镜像push到私有仓库上去 私有仓库搭建可以放在gitlab上,如果你仓库在gitlab的dev/auto中,你的镜像名称必须是如下 reg.transai.net/dev/auto/crystal-image-devel:v1.0 sudo docker push reg.transai.net/dev/auto/crystal-image-devel:v1.0 push成功后就可以从私有仓库下载下来 sudo docker pull reg.transai.net/dev/auto/crystal-image-devel:v1.0
关于如何在gitlab上已经有一个私有仓库,在它的描述中这样说:
https://blog.csdn.net/weixin_43360707/article/details/122866281
这个在gitlab的More infomationzhong
我摘抄一段:
NOTE: If you pull container images from Docker Hub, you can also use
the GitLab Dependency Proxy to avoid running into rate limits and
speed up your pipelines. With the Docker Container Registry integrated
into GitLab, every GitLab project can have its own space to store its
Docker images. You can read more about Docker Registry at
https://docs.docker.com/registry/introduction/. This document is the
user guide. To learn how to enable the Container Registry for your
GitLab instance, visit the administrator documentation.
上传镜像后,可以在Packages&Registries中查看上传的镜像:
search 从Docker Hub查找镜像
docker search [OPTIONS] TERM
本地镜像管理
images 列出本地镜像
docker images [OPTIONS] [REPOSITORY[:TAG]]
rmi 删除本地一个或多个镜像
docker rmi [OPTIONS] IMAGE [IMAGE...]
tag 标记本地镜像,将其归入某一仓库
docker tag [OPTIONS] IMAGE[:TAG] [REGISTRYHOST/][USERNAME/]NAME[:TAG]
build 使用Dockerfile构建镜像
docker build [OPTIONS] PATH | URL |
(OPTIONS说明:
--build-arg=[] :设置镜像创建时的变量;
--cpu-shares :设置 cpu 使用权重;
--cpu-period :限制 CPU CFS周期;
--cpu-quota :限制 CPU CFS配额;
--cpuset-cpus :指定使用的CPU id;
--cpuset-mems :指定使用的内存 id;
--disable-content-trust :忽略校验,默认开启;
-f :指定要使用的Dockerfile路径;
--force-rm :设置镜像过程中删除中间容器;
--isolation :使用容器隔离技术;
--label=[] :设置镜像使用的元数据;
-m :设置内存最大值;
--memory-swap :设置Swap的最大值为内存+swap,"-1"表示不限swap;
--no-cache :创建镜像的过程不使用缓存;
--pull :尝试去更新镜像的新版本;
-q :安静模式,成功后只输出镜像ID;
--rm :设置镜像成功后删除中间容器;
--shm-size :设置/dev/shm的大小,默认值是64M;
--ulimit :Ulimit配置
)
这里有一个dockerfile案例:
##### flow-center-image:v1.0 contains:
## v1.0:
# - softwares and system tools in need
# - runtime dependencies
# - media-image dependencies
# - development dependencies
# - dependencies for Qt 5
# - PYTHON
# - GCC G++
# - git and git-lfs
##### usage:
# to build image, please run in shell:
# image_version=v1.0 #change in need
# sudo docker build -t reg.transai.net/dev/auto/crystal-image:${image_version} -f
# /path/to/Dockerfile_crystal .
# or run build-docker-image.sh:
# ./build-docker-image.sh -t reg.transai.net/dev/auto/crystal-image:${image_version} -f /path/to/Dockerfile_crystal -c .
# tag and upload the newly built image to gitlab registry(login firstly):
# sudo docker push reg.transai.net/dev/auto/crystal-image:${image_version}
FROM ubuntu:20.04
LABEL version="1.0" \
buildDate="20220114" \
maintainer01="Lin Picheng, TransAI Ltd. " \
maintainer02="Song Kaiyin" \
maintainer0X="caizc"
SHELL ["/bin/bash", "-c"]
ENV DEBIAN_FRONTEND noninteractive
ENV TZ=Asia/Shanghai
RUN sed -i 's/ports.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list \
&& sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list \
&& sed -i '/#/d' /etc/apt/sources.list \
&& sed -i '/^$/d' /etc/apt/sources.list \
&& echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main " >> /etc/apt/sources.list \
&& echo "deb http://mirrors.aliyun.com/ubuntu/ xenial universe" >> /etc/apt/sources.list \
&& echo "deb http://mirrors.aliyun.com/ubuntu/ bionic universe" >> /etc/apt/sources.list \
&& echo "APT::Acquire::Retries \"50\";" > /etc/apt/apt.conf.d/80-retries \
&& cat /etc/apt/sources.list
RUN echo -e "----- Adding utilites -----"
RUN apt-get clean && apt-get update
RUN echo -e "\n----- INSTALL GCC G++ BUILD ESSENTIAL-----"
RUN apt-get install -y --no-install-recommends \
build-essential \
apt-utils \
iproute2 \
iputils-ping \
lcov \
net-tools \
openssh-server \
pkg-config \
rsync \
software-properties-common \
sshpass \
telnet \
unzip \
vim \
wget \
zip \
dmidecode
RUN echo -e "\n--- Adding media codec dev libraries ---" \
&& echo -e "\n--- Adding Python --- " \
&& apt-get install -y --no-install-recommends \
python-dev \
python-numpy \
python-pip-whl \
python3-dev \
python3-numpy \
python3-pip \
libtbb2 \
libtbb-dev \
libjpeg-dev \
libpng-dev \
libtiff-dev \
libdc1394-22-dev
RUN echo -e "\n----- Adding runtime dependencies -----"
RUN apt-get install -y --no-install-recommends \
libgl1 \
libglib2.0-0 \
libtbb2 \
libjpeg8 \
libpng16-16 \
libtiff5 \
libdc1394-22 \
libfreetype6 \
libharfbuzz0b \
libxkbcommon-x11-0 \
libgomp1 \
libfontconfig1 \
libgssapi-krb5-2 \
libsm6 \
libxrender1 \
libxext-dev \
libpulse0 \
libpulse-mainloop-glib0 \
uuid-dev \
sysstat
RUN echo -e "\n----- Adding runtime tools -----"
RUN apt-get install -y --no-install-recommends --fix-missing\
tcpdump \
gdb \
dstat \
ffmpeg \
clang-tidy-8
RUN echo -e "\n--- Adding Git and Git LFS ---"
RUN curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | bash \
&& apt-get install -y --no-install-recommends \
git \
git-lfs \
&& sed -i "/StrictHostKeyChecking/c StrictHostKeyChecking no" /etc/ssh/ssh_config \
&& echo service ssh start >> ${HOME}/.bashrc
WORKDIR /workspace
可以用如下脚本:
#!/bin/bash
sudo docker build . -f cystalmedia.dockerfile --network host -t crystal-image:v1.0
history 查看指定镜像的构建过程
docker history [OPTIONS] IMAGE
save/load 将指定镜像保存成 tar 文件/将tar文件恢复成镜像
docker save [OPTIONS] IMAGE [IMAGE...] #镜像备份
docker load -i TAR.tar #镜像恢复 (-o :输出到的文件)
import/export 从归档文件中构建镜像/将镜像导出【export导出会丢失构建历史无法回滚,save会保留构建历史】
docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]
docker export -o PATH_TO_EXPORT_FILE CONTAINER
info|version
info 显示 Docker 系统信息,包括镜像和容器数
docker info [OPTIONS]
version 显示 Docker 版本信息
docker version [OPTIONS]