Docker常用命令

Docker常用命令小结
Update: Translate to EN

Gather info

List all containers
列出所有容器:

1
docker ps -a

Get container IP
获取容器IP:

1
docker inspect 2d0142bd65a7 | grep IPAddress

2d0142bd65a7 is a container id.
其中 2d0142bd65a7 为容器ID。

List all loadable images
列出可以装载的镜像:

1
docker images

Save,Load,Tag and cp

Save image to local file
导出镜像:

1
docker save 1865b6805867 > image.tar

1865b6805867 is a image id.
其中 1865b6805867 为镜像ID。

Create docker image based on a container
基于容器创建镜像

1
docker commit ${container_id} ${image_name}:${image_tag}

Load image from local file
导入镜像:

1
docker load < image.tar

Tag a image which id is 1865b6805867(Only first 4 digits of ID is acceptable)

为ID为1865b6805867的镜像打上TAG(没错,只需要ID前几位就可以):

1
docker tag 1865 gcr.io/tensorflow/tensorflow:latest-devel

Copy a file or folder from container to local host:

从容器复制文件到主机(如果你忘了从主机挂载文件夹):

1
docker cp ${containerID}:${file_path_in_container} ${host_path}

It’s basically same when you trying to copy a file from host to container, just swap two parameters.

Clean

Stop all containers
停止所有容器

1
docker kill $(docker ps -aq)

Delete all containers
删除所有容器

1
docker rm $(docker ps -aq)

Change $(docker ps -aq) to container id to stop or delete the specific container.

Delete all images
删除所有镜像

1
docker rmi $(docker images -q)

Change $(docker images -q) to image id to delete the specific image.

Remove unused data to free disk space
删除未使用的数据以释放磁盘空间

1
docker system prune

Docker build without cache

1
docker build --no-cache .