Docker 常用命令的汇总
此篇文章,针对是Docker世界中的小白,Docker使用的扫盲贴,在掌握Docker的基本命令后,我们后续会推出进阶篇~
0. Image vs Container
An instance of an image is called a container. You have an image, which is a set of layers as you describe. If you start this image, you have a running container of this image. You can have many running containers of the same image.
You can see all your images with docker images
whereas you can see your running containers with docker ps
1. docker login [option] [server]
docker login harbor-registry.youdao.com
此条命令就是登录您的远程代码仓库;
登录后,您才可以进行镜像的下载或者上传,这条命令作为作为最基础的命令,放在了第一条。
cat ~/my_password.txt | docker login --username foo --password-stdin harbor-registry.youdao.com
若您想一条命令直接登录,免去往复输入username 和密码的过程,您也可以采用以上的命令;
前半部分的cat ~/my_password.txt
的打印结果将会作为结果传入后面的表达式作为输入,在此条命令中,username 为foo
, password 为my_password.txt
的内容。
2. docker push image-name
docker push harbor-registry.youdao.com/xinhs/ubuntu:xinhs
where
image-name = repo_url : tag-name
其中repo_url 是 docker images
中repositories栏会显示的内容(远端代码仓库的地址), tag-name是tag栏会显示的内容(标签的名字)。
3. docker pull image-name 路径
docker pull harbor-registry.inner.com/xinhs/ubuntu:xinhs .
我们把远端代码仓库某个tag 名字的镜像下载到本地,最后的.
代表的是当前路径;整行命令翻译成大白话就是把叫做xinhs 位于harbor-registry.inner.com/xinhs/ubuntu 的镜像下载到当前的目录。
镜像的迁移一定要避免拷贝,一定要从本地上传到云端,然后从远端在下载到新的本地,否则展开时,docker 会一层一层的校验,校验的时候会报错。
4. docker ps + docker images
[liangqian@hd043 ~]$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fcde02b78d40 harbor-registry.inner.youdao.com/xinhs/ubuntu:xinhs "/docker-entrypoint.…" 2 weeks ago Up 2 weeks 0.0.0.0:8081->8080/tcp keen_leavitt
c6379c762ff9 harbor-registry.inner.youdao.com/ai/ubuntu:gpu-cuda8-ocr "sh" 3 weeks ago Up 3 weeks 0.0.0.0:7080->8080/tcp brave_nobel
显示当前的正在运行的container , ps 为process status , 当前进程的状态
[liangqian@hd043 ~]$ sudo docker images --all
REPOSITORY TAG IMAGE ID CREATED SIZE
harbor-registry.inner.youdao.com/xinhs/ubuntu version03112019 4c2acf65386f 13 days ago 12.9GB
harbor-registry.inner.youdao.com/xinhs/ubuntu xinhs 70318796ec31 2 weeks ago 12GB
<none> <none> f4c7490c5dff 2 weeks ago 12GB
<none> <none> 6531a947cd96 2 weeks ago 12GB
harbor-registry.inner.youdao.com/xinhs/ubuntu <none> 1f1008636eb7 2 weeks ago 12GB
harbor-registry.inner.youdao.com/xinhs/ubuntu <none> 7b51d43fe2bb 2 weeks ago 12GB
harbor-registry.inner.youdao.com/ai/ubuntu gpu-cuda8-ocr 32c20f25ef57 4 weeks ago 11.5GB
harbor-registry.inner.youdao.com/ai/ubuntu <none> c586cad291b7 4 weeks ago 11.4GB
harbor-registry.inner.youdao.com/ai/ubuntu <none> 362d2561aa7d 4 weeks ago 11.4GB
harbor-registry.inner.youdao.com/xinhs/ubuntu <none> 23ba2c88ca41 4 weeks ago 11.3GB
此为显示当前登记在册的所有images , 我们可以把一个镜像启动成一个container,也可以把一个container抽象成一个镜像。
5. docker commit
把一个正在运行的container打包成一个镜像
docker commit -m=“some message” expect_image_name:expect_tag
事例:
[liangqian@hd043 ~]$ sudo docker commit -m="20190324 pack a container" fcde02b78d40 harbor-registry.inner.youdao.com/xinhs/ubuntu:xinhs
sha256:48693afa495db15c3e569ec6c185788ab0ffa7e3bb2cd2c7b3f0334b50ecc705
当你显示一个sha256 的digest 就说明你成功了。
然后你输入
[liangqian@hd043 ~]$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
harbor-registry.inner.youdao.com/xinhs/ubuntu xinhs 48693afa495d 4 hours ago 12.9GB
你会发现,你刚刚打包好的镜像赫然的出现在这里。
6. docker save + scp + docker load
如果可以避免这么传输,尽量避免这么传输;而是采用上传代码仓库,然后在另一个机器上上从代码仓库直接下载;从LINUX直接向LINUX拷贝可能还可以,一旦涉及到从LINUX下载到MAC, 然后从MAC从新上传到LINUX,解压的时候会报错,请大家务必注意
docker save imageID > filename
示例:
[liangqian@hd043 liangqian]$ sudo docker save 48693afa495d > xinhs.tar
由于镜像中包含大量信息,所以需要等一下,命令才能执行结束,请大家耐心等待一下。
关于镜像中到底包含了什么,我这里有一张对比图
Docker vs VM
docker 打包相关依赖在镜像里面,所以可以让应用层尽量的小
远程拷贝,我们用SCP
scp 文件名.tar username@IP_address:/path/to/target/folder
[liangqian@hd043 liangqian]$ scp xinhs.tar liangqian@IP_address:/mfs_live/ead_cdn/ead/zhiyun/liangqian
加载镜像
docker load < filename
docker load < xinhs.tar
经过一层层验证,如果发现每一层验证就成功了,就进入本机了,你就可以通过docker images
来查看了
如果发现repository
和tag
不正确,我们可以用
docker tag imageID expect_repo_url:expect_tag
docker tag d0acfab308ff ubuntu:xinhs
7. docker run
docker run --runtime=nvidia -dt --shm-size 10G -p 宿主机端口号:镜像的端口号 -v
/workspace/path/in/server:/home/linhui/workspace expect_repo_url:expect_tag sh
docker run --runtime=nvidia -dt --shm-size 10G -p 9090:8080 -v
/home/liangqian/xinhs/workspace:/home/linhui/workspace registry.inner.youdao.com/xinhs/ubuntu:xinhs sh
镜像一经启动,就会创建一个instance, 即container, 当我们输入
docker ps
你应该能看正在运行的Container,其中应该有你刚刚启动的image
8. docker stop + docker rmi + docker rm
- docker stop — 停止正在运行的container
docker stop containerID or containerIDs
docker stop fcde02b78d40
docker ps -q
当前运行的container的所有container ID
docker stop $(docker ps -q)
停止当前所有正在运行的containers
docker kill $(docker ps -q)
停止当前所有正在运行的containers
- docker rm —删除container
docker rm containerID
docker rm fcde02b78d40
docker rm fcde02b78d40 5d4ac86220bd b819e94662da
- docker rmi imageID
docker rmi imageID
docker rmi 70318796ec31
docker rmi 70318796ec31 40035b3eabba 95d5fc92b745