1. Overview of Docker command types
- Docker environment information — docker [info | version]
- Container lifecycle management — docker [create | exec | run | start | stop | restart | kill | rm | pause | unpause]
- Container operation and maintenance — docker [ps | inspect | top | attach | wait | export | port | rename | stat]
- Container rootfs command — docker [commit | cp | diff]
- Mirror warehouse — docker [login | pull | push | search]
- Local image management — docker [build | images | rmi | tag | save | import | load]
- Container resource management — docker [volume | network]
- System log information — docker [events | history | logs]
Second, commonly used commands
View Docker version information
docker version
View Docker system information, including images and containers
docker info
View docker help
docker help
Search for eligible images from Docker Hub
# 搜索 redis 镜像
docker search redis
Pull or update the specified image from Docker Hub
# 拉取 java 镜像(默认最新版本,即 :latest )
docker pull java
List all local mirrors
docker images
Start a container
# 启动 redis ,其中 redis 为镜像名
docker run redis
# 后台启动 redis
docker run -d redis
# 创建容器运行 centos:centos6 镜像并进入交互模式
docker run -i -t --name centos-01 centos:centos6 /bin/bash
View container startup logs
# 查看 redis-01 的容器启动日志, -f : 跟踪日志输出, -t : 显示时间戳
docker logs -f -t redis-01
List all running containers
docker ps
docker container ls
List all created containers, including completed containers
docker container ls -a
Stop a running container
docker stop nginx-01
Kill a running container process
docker kill nginx-01
Remove one or more specified containers locally
# 移除 nginx-01 和 nginx-02 容器
docker rm nginx-01 nginx-02
# -f 强行移除容器,即使其正在运行
docker rm -f nginx-01
Remove one or more specified images from the local
# 移除 nginx:latest 和 python:latest 镜像
docker rmi nginx:latest python:latest
# -f 强行移除镜像,即使其正被使用
docker rmi -f nginx:latest
Image export and import
# 导出镜像到文件
docker save ubuntu:latest > /root/ubuntu.tar
# 导入镜像文件
docker load < ubuntu.tar
Curing a container into a new image
# 将容器 nginx-01 固化为镜像 mynginx:latest
docker commit nginx-01 mynginx:latest
File copy between host and container
# 拷贝主机文件到容器
docker cp /home/myfile centos-01:/home/
# 拷贝容器文件到主机
docker cp centos-01:/home/myfile /home/