Docker 命令 #
登录/登出 #
sh
# 登录 Docker Registry 或 第三方仓库
docker login [-u username] [-p password] [SERVER]
# 登出 Docker Registry 或 第三方仓库
docker logout [SERVER]
镜像相关 #
列出镜像 #
sh
# 列出镜像
docker images
# 所有镜像(包括中间镜像)
docker images -a
查看镜像详情 #
sh
docker image inspect IMAGE...
构建镜像 #
sh
# 构建镜像常用参数
docker build [OPTIONS] PATH | URL | -
--build-arg list
-f, --file string
--pull
-q, --quiet
-t, --tag list
--target string
# 删除构建缓存
docker builder prune [OPTIONS]
-a, --all Remove all unused images, not just dangling ones
--filter filter Provide filter values (e.g. 'unused-for=24h')
-f, --force
--keep-storage bytes Amount of disk space to keep for cache
例:
sh
cd example
cat Dockerfile
docker build -t example:1.0.0 -t example:latest .
创建标签 #
sh
docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
从容器创建镜像 #
sh
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
-a, --author list Author (e.g., "John Smith <john@a-team.com>")
-c, --change list Apply Dockerfile instruction to the created image
-m, --message string
拉取/推送镜像 #
sh
docker pull NAME[:TAG|@DIGEST]
docker push NAME[:TAG]
例:
sh
docker pull maven
dpcker pull maven:latest
docker pull redis:5-alpine
docker pull ubuntu:bionic-20200112@sha256:bc025862c3e8ec4a8754ea4756e33da6c41cba38330d7e324abd25c8e0b93300
删除镜像 #
sh
# 删除镜像
docker rmi [OPTIONS] IMAGE...
-f, --force Force removal of the image
--no-prune Do not delete untagged parents
# 删除无用镜像
docker image prune [OPTIONS]
-a, --all Remove all unused image, not just dangling ones
--filter filter Provider filter values (e.g. 'until=<timestamp>')
-f, --force Do not prompt for confirmation
容器相关 #
列出容器 #
sh
# 列出运行中的容器
docker ps
# 所有容器
docker ps -a
查看容器详情 #
sh
docker container inspect CONTAINER...
创建/运行容器 #
sh
docker create ...
# 运行容器常用参数
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
-d, --detach
--entrypoint string
-e, --env list
-i, --interactive
--name string
--network network
--privileged
-p, --publish list
-P, --publish-all
--restart string
--rm
-t, --tty
-u, --user string
-v, --volume list
-w, --workdir string
例:
sh
docker run -it --rm maven:latest sh
docker run -d -p 16379:6379 -v $HOME/redis-data:/data redis
启动/停止/重启容器 #
sh
# 启动容器
docker start [-i, --interactive] CONTAINER...
# 停止容器
docker stop [-t seconds] CONTAINER...
# 重启容器
docker restart [-t seconds] CONTAINER...
# 强制终止容器
docker kill CONTAINER...
容器内执行命令 #
sh
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
-d, --detach
-e, --env list
-i, --interactive
--privileged
-t, --tty
-u, --user string
-w, --workdir string
例:
sh
docker exec -it 3f5ef4 sh
容器与本地之间复制文件 #
sh
# Use '-' to read/write a tar archive from stdin/stdout.
docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
-a, --archive Archive mode (copy all uid/gid information)
-L, --follow-link Always follow symbol link in SRC_PATH
重命名容器 #
sh
docker rename CONTAINER NEW_NAME
删除容器 #
sh
# 删除容器
docker rm [OPTIONS] CONTAINER...
-f, --force Force the removal of a running container (uses SIGKILL)
-l, --link Remove the specified link
-v, --volumes Remove the volumes associated with the container
# 删除所有已停止的容器
docker container prune [OPTIONS]
-f, --force Do not prompt for confirmation
--filter filter Provide filter values (e.g. 'until=<timestamp>')
输出日志 #
sh
docker log [OPTIONS] CONTAINER
-f, --follow
--since string
--tail string
-t, --timestamps
--until string
查看端口映射 #
sh
docker port CONTAINER [PRIVATE_PORT[/PROTO]]
存储相关 #
列出卷 #
sh
docker volume ls
查看卷详情 #
sh
docker volume inspect VOLUME...
创建卷 #
sh
docker volume create [OPTIONS] [VOLUME]
-d, --driver string Specify volume driver name (default "local")
--label list Set metadata for a volume
-o, --opt map Set driver specific options (default map[])
删除卷 #
sh
# 删除卷
docker volume rm [OPTIONS] VOLUME...
-f, --force
# 删除无用的卷
docker volume prume [OPTIONS]
-f, --force
--filter filter Provide filter values (e.g. 'label=<label>')
网络相关 #
列出网络 #
sh
docker network ls
查看网络详情 #
sh
docker network inspect [OPTIONS] NETWORK...
创建网络 #
sh
docker network create [OPTIONS] NETWORK
--attachable Enable manual container attachment
--config-from string The network from which copying the configuration
--config-only Create a configuration only network
-d, --driver string Driver to manage the Network (default "bridge")
--gateway strings IPv4 or IPv6 Gateway for the master subnet
--internal Restrict external access to the network
--ip-range strings Allocate container ip from a sub-range
--ipam-driver string IP Address Management Driver (default "default")
--ipam-opt map Set IPAM driver specific options (default map[])
--ipv6 Enalbe IPv6 networking
--label list Set metadata on a network
-o, --opt map Set driver specific options (default map[])
--scope string Control the network's scope
--subnet strings Subnet in CIDR format that represents a network segment
容器连接到网络 #
sh
docker network connect [OPTIONS] NETWORK CONTAINER
--alias strings Add network-scoped alias for the container
--driver-opt strings Driver options for the network
--ip string IPv4 address (e.g., 172.30.100.104)
--ip6 string IPv6 address (e.g., 2001:db8::33)
--link list Add link to another container
--link-local-ip strings Add a link-local address for the container
取消连接到网络 #
sh
docker network disconnect [OPTIONS] NETWORK CONTAINER
-f, --force
删除网络 #
sh
# 删除网络
docker network rm NETWORK...
# 删除无用的网络
docker network prune [OPTIONS]
-f, --force
--filter filter Provide filter values (e.g. 'until=<timestamp>')