本文链接: https://xiets.blog.csdn.net/article/details/117141753
相关文章: Docker 常用命令 与 Dockerfile
1. Docker 安装官方文档
Ubuntu 安装脚本:
#!/bin/sh
echo "#######################################"
echo "###### Uninstall old versions #########"
echo "#######################################"
echo "Older versions of Docker were called docker, docker.io, or docker-engine. "
echo "If these are installed, uninstall them:"
sudo apt -y remove docker docker-engine docker.io containerd runc
echo "#######################################"
echo "####### Set up the repository #########"
echo "#######################################"
echo "1. Update the apt package index and install packages to allow apt to use a repository over HTTPS:"
sudo apt -y update
sudo apt -y install \
ca-certificates \
curl \
gnupg \
lsb-release
echo "2. Add Docker’s official GPG key:"
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "3. Use the following command to set up the stable repository. "
echo " To add the nightly or test repository, "
echo " add the word nightly or test (or both) after the word stable in the commands below. "
echo " Learn about nightly and test channels."
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
echo "#######################################"
echo "######## Install Docker Engine ########"
echo "#######################################"
echo "1. Update the apt package index, and install the latest version of Docker Engine and containerd, "
echo "or go to the next step to install a specific version:"
sudo apt -y update
sudo apt -y install docker-ce docker-ce-cli containerd.io
echo "2. Verify that Docker Engine is installed correctly by running the hello-world image."
sudo docker run --rm hello-world
sudo docker --version
echo "#######################################"
echo "################# OVER ################"
echo "#######################################"
运行验证:
$ sudo docker --version
Docker version 20.10.6, build 370c289
$ sudo docker version
...
$ sudo docker run --rm hello-world
...
$ sudo docker run -p 80:80 --rm docker/getting-started
...
2. Docker 管理命令
Use the Docker command line: $ docker
# | 管理命令(Management Management) | 描述(EN) | 描述(CN) |
---|---|---|---|
1 | docker app* | Docker App (Docker Inc., v0.9.1-beta3) | Docker 应用程序 |
2 | docker builder | Manage builds | 管理构建 |
3 | docker buildx* | Build with BuildKit (Docker Inc., v0.5.1-docker) | 使用 BuildKit 进行构建 |
4 | docker config | Manage Docker configs | 管理 Docker 配置 |
5 | docker container | Manage containers | 管理容器 |
6 | docker context | Manage contexts | 管理上下文 |
7 | docker image | Manage images | 管理镜像 |
8 | docker manifest | Manage Docker image manifests and manifest lists | 管理 Docker 镜像清单和清单列表 |
9 | docker network | Manage networks | 管理网络 |
10 | docker node | Manage Swarm nodes | 管理 Swarm 节点 |
11 | docker plugin | Manage plugins | 管理插件 |
12 | docker secret | Manage Docker secrets | 管理 Docker 机密 |
13 | docker service | Manage services | 管理服务 |
14 | docker stack | Manage Docker stacks | 管理 Docker 堆栈 |
15 | docker swarm | Manage Swarm | 管理集群 |
16 | docker system | Manage Docker | 管理 Docker |
17 | docker trust | Manage trust on Docker images | 管理对 Docker 镜像的信任 |
18 | docker volume | Manage volumes | 管理数据卷 |
Docker 的每一个管理命令又有多个子命令,可以使用 --help
查看帮助,如:
$ docker image --help
子命令又可以继续使用 --help
查看帮助,如:
$ docker image pull --help
运行某些子命令时,管理命令也可以省略,如:
$ docker image pull [OPTIONS] NAME[:TAG|@DIGEST]
# 相当于
$ docker pull [OPTIONS] NAME[:TAG|@DIGEST]
3. Docker 命令
# | 命令(Commands) | 描述(EN) | 描述(CN) |
---|---|---|---|
1 | docker attach | Attach local standard input, output, and error streams to a running container | 将本地标准输入,输出和错误流附加到正在运行的容器 |
2 | docker build | Build an image from a Dockerfile | 从 Dockerfile 构建镜像 |
3 | docker commit | Create a new image from a container’s changes | 根据容器的更改创建新镜像 |
4 | docker cp | Copy files/folders between a container and the local filesystem | 在容器和本地文件系统之间复制文件/文件夹 |
5 | docker create | Create a new container | 创建一个新的容器 |
6 | docker diff | Inspect changes to files or directories on a container’s filesystem | 检查容器文件系统上文件或目录的更改 |
7 | docker events | Get real time events from the server | 从服务器获取实时事件 |
8 | docker exec | Run a command in a running container | 在正在运行的容器中运行命令 |
9 | docker export | Export a container’s filesystem as a tar archive | 将容器的文件系统导出为 tar 存档 |
10 | docker history | Show the history of an image | 显示镜像的历史记录 |
11 | docker images | List images | 列出所有镜像 |
12 | docker import | Import the contents from a tarball to create a filesystem image | 从 tarball 导入内容以创建文件系统镜像 |
13 | docker info | Display system-wide information | 显示系统范围的信息 |
14 | docker inspect | Return low-level information on Docker objects | 返回有关 Docker 对象的低级信息 |
15 | docker kill | Kill one or more running containers | 杀死一个或多个正在运行的容器 |
16 | docker load | Load an image from a tar archive or STDIN | 从 tar 存档或 STDIN 加载镜像 |
17 | docker login | Log in to a Docker registry | 登录 Docker 仓库注册服务器 |
18 | docker logout | Log out from a Docker registry | 登出 Docker 仓库注册服务器 |
19 | docker logs | Fetch the logs of a container | 获取容器的日志 |
20 | docker pause | Pause all processes within one or more containers | 暂停一个或多个容器中的所有进程 |
21 | docker port | List port mappings or a specific mapping for the container | 列出容器的端口映射或指定映射 |
22 | docker ps | List containers | 列出所有容器 |
23 | docker pull | Pull an image or a repository from a registry | 从注册服务器中拉取镜像或仓库 |
24 | docker push | Push an image or a repository to a registry | 推送镜像或仓库到注册服务器 |
25 | docker rename | Rename a container | 重命名容器 |
26 | docker restart | Restart one or more containers | 重启一个或多个容器 |
27 | docker rm | Remove one or more containers | 删除一个或多个容器 |
28 | docker rmi | Remove one or more images | 删除一个或多个镜像 |
29 | docker run | Run a command in a new container | 创建新的容器并运行命令 |
30 | docker save | Save one or more images to a tar archive (streamed to STDOUT by default) | 将一个或多个镜像保存到 tar 存档(默认以流式的方式输出到 STDOUT) |
31 | docker search | Search the Docker Hub for images | 从 Docker Hub 搜索镜像 |
32 | docker start | Start one or more stopped containers | 启动一个或多个已停止的容器 |
33 | docker stats | Display a live stream of container(s) resource usage statistics | 显示容器资源使用情况统计信息的实时流 |
34 | docker stop | Stop one or more running containers | 停止一个或多个容器 |
35 | docker tag | Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE | 引用 源镜像 创建一个 tag 成为“新”的目标镜像 |
36 | docker top | Display the running processes of a container | 显示容器中正在运行的进程 |
37 | docker unpause | Unpause all processes within one or more containers | 恢复一个或多个容器中的所有进程 |
38 | docker update | Update configuration of one or more containers | 更新一个或多个容器的配置 |
39 | docker version | Show the Docker version information | 显示 Docker 版本信息 |
40 | docker wait | Block until one or more containers stop, then print their exit codes | 阻塞直到一个或多个容器停止,然后打印其退出代码 |
4. docker --help
$ docker
Usage: docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default "/root/.docker")
-c, --context string Name of the context to use to connect to the daemon (overrides
DOCKER_HOST env var and default context set with "docker context
use")
-D, --debug Enable debug mode
-H, --host list Daemon socket(s) to connect to
-l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal")
(default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default "/root/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/root/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
Management Commands:
app* Docker App (Docker Inc., v0.9.1-beta3)
builder Manage builds
buildx* Build with BuildKit (Docker Inc., v0.5.1-docker)
config Manage Docker configs
container Manage containers
context Manage contexts
image Manage images
manifest Manage Docker image manifests and manifest lists
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
trust Manage trust on Docker images
volume Manage volumes
Commands:
attach Attach local standard input, output, and error streams to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes
Run 'docker COMMAND --help' for more information on a command.
To get more help with docker, check out our guides at https://docs.docker.com/go/guides/
4.1 docker image
$ docker image
Usage: docker image COMMAND
Manage images
Commands:
build Build an image from a Dockerfile
history Show the history of an image
import Import the contents from a tarball to create a filesystem image
inspect Display detailed information on one or more images
load Load an image from a tar archive or STDIN
ls List images
prune Remove unused images
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rm Remove one or more images
save Save one or more images to a tar archive (streamed to STDOUT by default)
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
Run 'docker image COMMAND --help' for more information on a command.
4.2 docker container
$ docker container
Usage: docker container COMMAND
Manage containers
Commands:
attach Attach local standard input, output, and error streams to a running container
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
exec Run a command in a running container
export Export a container's filesystem as a tar archive
inspect Display detailed information on one or more containers
kill Kill one or more running containers
logs Fetch the logs of a container
ls List containers
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
prune Remove all stopped containers
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
run Run a command in a new container
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
wait Block until one or more containers stop, then print their exit codes
Run 'docker container COMMAND --help' for more information on a command.
4.3 docker network
$ docker network
Usage: docker network COMMAND
Manage networks
Commands:
connect Connect a container to a network
create Create a network
disconnect Disconnect a container from a network
inspect Display detailed information on one or more networks
ls List networks
prune Remove all unused networks
rm Remove one or more networks
Run 'docker network COMMAND --help' for more information on a command.
4.4 docker system
$ docker system
Usage: docker system COMMAND
Manage Docker
Commands:
df Show docker disk usage
events Get real time events from the server
info Display system-wide information
prune Remove unused data
Run 'docker system COMMAND --help' for more information on a command.
4.5 docker volume
$ docker volume
Usage: docker volume COMMAND
Manage volumes
Commands:
create Create a volume
inspect Display detailed information on one or more volumes
ls List volumes
prune Remove all unused local volumes
rm Remove one or more volumes
Run 'docker volume COMMAND --help' for more information on a command.
5. 重点命令
5.1 docker create --help
Docker Docs: docker create
$ docker create --help
Usage: docker create [OPTIONS] IMAGE [COMMAND] [ARG...]
Create a new container
Options:
--add-host list Add a custom host-to-IP mapping (host:ip)
-a, --attach list Attach to STDIN, STDOUT or STDERR
--blkio-weight uint16 Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
--blkio-weight-device list Block IO weight (relative device weight) (default [])
--cap-add list Add Linux capabilities
--cap-drop list Drop Linux capabilities
--cgroup-parent string Optional parent cgroup for the container
--cgroupns string Cgroup namespace to use (host|private)
'host': Run the container in the Docker host's cgroup namespace
'private': Run the container in its own private cgroup namespace
'': Use the cgroup namespace as configured by the
default-cgroupns-mode option on the daemon (default)
--cidfile string Write the container ID to the file
--cpu-period int Limit CPU CFS (Completely Fair Scheduler) period
--cpu-quota int Limit CPU CFS (Completely Fair Scheduler) quota
--cpu-rt-period int Limit CPU real-time period in microseconds
--cpu-rt-runtime int Limit CPU real-time runtime in microseconds
-c, --cpu-shares int CPU shares (relative weight)
--cpus decimal Number of CPUs
--cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)
--cpuset-mems string MEMs in which to allow execution (0-3, 0,1)
--device list Add a host device to the container
--device-cgroup-rule list Add a rule to the cgroup allowed devices list
--device-read-bps list Limit read rate (bytes per second) from a device (default [])
--device-read-iops list Limit read rate (IO per second) from a device (default [])
--device-write-bps list Limit write rate (bytes per second) to a device (default [])
--device-write-iops list Limit write rate (IO per second) to a device (default [])
--disable-content-trust Skip image verification (default true)
--dns list Set custom DNS servers
--dns-option list Set DNS options
--dns-search list Set custom DNS search domains
--domainname string Container NIS domain name
--entrypoint string Overwrite the default ENTRYPOINT of the image
-e, --env list Set environment variables
--env-file list Read in a file of environment variables
--expose list Expose a port or a range of ports
--gpus gpu-request GPU devices to add to the container ('all' to pass all GPUs)
--group-add list Add additional groups to join
--health-cmd string Command to run to check health
--health-interval duration Time between running the check (ms|s|m|h) (default 0s)
--health-retries int Consecutive failures needed to report unhealthy
--health-start-period duration Start period for the container to initialize before starting
health-retries countdown (ms|s|m|h) (default 0s)
--health-timeout duration Maximum time to allow one check to run (ms|s|m|h) (default 0s)
--help Print usage
-h, --hostname string Container host name
--init Run an init inside the container that forwards signals and reaps processes
-i, --interactive Keep STDIN open even if not attached
--ip string IPv4 address (e.g., 172.30.100.104)
--ip6 string IPv6 address (e.g., 2001:db8::33)
--ipc string IPC mode to use
--isolation string Container isolation technology
--kernel-memory bytes Kernel memory limit
-l, --label list Set meta data on a container
--label-file list Read in a line delimited file of labels
--link list Add link to another container
--link-local-ip list Container IPv4/IPv6 link-local addresses
--log-driver string Logging driver for the container
--log-opt list Log driver options
--mac-address string Container MAC address (e.g., 92:d0:c6:0a:29:33)
-m, --memory bytes Memory limit
--memory-reservation bytes Memory soft limit
--memory-swap bytes Swap limit equal to memory plus swap: '-1' to enable unlimited swap
--memory-swappiness int Tune container memory swappiness (0 to 100) (default -1)
--mount mount Attach a filesystem mount to the container
--name string Assign a name to the container
--network network Connect a container to a network
--network-alias list Add network-scoped alias for the container
--no-healthcheck Disable any container-specified HEALTHCHECK
--oom-kill-disable Disable OOM Killer
--oom-score-adj int Tune host's OOM preferences (-1000 to 1000)
--pid string PID namespace to use
--pids-limit int Tune container pids limit (set -1 for unlimited)
--platform string Set platform if server is multi-platform capable
--privileged Give extended privileges to this container
-p, --publish list Publish a container's port(s) to the host
-P, --publish-all Publish all exposed ports to random ports
--pull string Pull image before creating ("always"|"missing"|"never") (default "missing")
--read-only Mount the container's root filesystem as read only
--restart string Restart policy to apply when a container exits (default "no")
--rm Automatically remove the container when it exits
--runtime string Runtime to use for this container
--security-opt list Security Options
--shm-size bytes Size of /dev/shm
--stop-signal string Signal to stop a container (default "SIGTERM")
--stop-timeout int Timeout (in seconds) to stop a container
--storage-opt list Storage driver options for the container
--sysctl map Sysctl options (default map[])
--tmpfs list Mount a tmpfs directory
-t, --tty Allocate a pseudo-TTY
--ulimit ulimit Ulimit options (default [])
-u, --user string Username or UID (format: <name|uid>[:<group|gid>])
--userns string User namespace to use
--uts string UTS namespace to use
-v, --volume list Bind mount a volume
--volume-driver string Optional volume driver for the container
--volumes-from list Mount volumes from the specified container(s)
-w, --workdir string Working directory inside the container
5.2 docker run --help
Docker Docs: docker run
$ docker run --help
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Run a command in a new container
Options:
--add-host list Add a custom host-to-IP mapping (host:ip)
-a, --attach list Attach to STDIN, STDOUT or STDERR
--blkio-weight uint16 Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
--blkio-weight-device list Block IO weight (relative device weight) (default [])
--cap-add list Add Linux capabilities
--cap-drop list Drop Linux capabilities
--cgroup-parent string Optional parent cgroup for the container
--cgroupns string Cgroup namespace to use (host|private)
'host': Run the container in the Docker host's cgroup namespace
'private': Run the container in its own private cgroup namespace
'': Use the cgroup namespace as configured by the
default-cgroupns-mode option on the daemon (default)
--cidfile string Write the container ID to the file
--cpu-period int Limit CPU CFS (Completely Fair Scheduler) period
--cpu-quota int Limit CPU CFS (Completely Fair Scheduler) quota
--cpu-rt-period int Limit CPU real-time period in microseconds
--cpu-rt-runtime int Limit CPU real-time runtime in microseconds
-c, --cpu-shares int CPU shares (relative weight)
--cpus decimal Number of CPUs
--cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)
--cpuset-mems string MEMs in which to allow execution (0-3, 0,1)
-d, --detach Run container in background and print container ID
--detach-keys string Override the key sequence for detaching a container
--device list Add a host device to the container
--device-cgroup-rule list Add a rule to the cgroup allowed devices list
--device-read-bps list Limit read rate (bytes per second) from a device (default [])
--device-read-iops list Limit read rate (IO per second) from a device (default [])
--device-write-bps list Limit write rate (bytes per second) to a device (default [])
--device-write-iops list Limit write rate (IO per second) to a device (default [])
--disable-content-trust Skip image verification (default true)
--dns list Set custom DNS servers
--dns-option list Set DNS options
--dns-search list Set custom DNS search domains
--domainname string Container NIS domain name
--entrypoint string Overwrite the default ENTRYPOINT of the image
-e, --env list Set environment variables
--env-file list Read in a file of environment variables
--expose list Expose a port or a range of ports
--gpus gpu-request GPU devices to add to the container ('all' to pass all GPUs)
--group-add list Add additional groups to join
--health-cmd string Command to run to check health
--health-interval duration Time between running the check (ms|s|m|h) (default 0s)
--health-retries int Consecutive failures needed to report unhealthy
--health-start-period duration Start period for the container to initialize before starting
health-retries countdown (ms|s|m|h) (default 0s)
--health-timeout duration Maximum time to allow one check to run (ms|s|m|h) (default 0s)
--help Print usage
-h, --hostname string Container host name
--init Run an init inside the container that forwards signals and reaps processes
-i, --interactive Keep STDIN open even if not attached
--ip string IPv4 address (e.g., 172.30.100.104)
--ip6 string IPv6 address (e.g., 2001:db8::33)
--ipc string IPC mode to use
--isolation string Container isolation technology
--kernel-memory bytes Kernel memory limit
-l, --label list Set meta data on a container
--label-file list Read in a line delimited file of labels
--link list Add link to another container
--link-local-ip list Container IPv4/IPv6 link-local addresses
--log-driver string Logging driver for the container
--log-opt list Log driver options
--mac-address string Container MAC address (e.g., 92:d0:c6:0a:29:33)
-m, --memory bytes Memory limit
--memory-reservation bytes Memory soft limit
--memory-swap bytes Swap limit equal to memory plus swap: '-1' to enable unlimited swap
--memory-swappiness int Tune container memory swappiness (0 to 100) (default -1)
--mount mount Attach a filesystem mount to the container
--name string Assign a name to the container
--network network Connect a container to a network
--network-alias list Add network-scoped alias for the container
--no-healthcheck Disable any container-specified HEALTHCHECK
--oom-kill-disable Disable OOM Killer
--oom-score-adj int Tune host's OOM preferences (-1000 to 1000)
--pid string PID namespace to use
--pids-limit int Tune container pids limit (set -1 for unlimited)
--platform string Set platform if server is multi-platform capable
--privileged Give extended privileges to this container
-p, --publish list Publish a container's port(s) to the host
-P, --publish-all Publish all exposed ports to random ports
--pull string Pull image before running ("always"|"missing"|"never") (default "missing")
--read-only Mount the container's root filesystem as read only
--restart string Restart policy to apply when a container exits (default "no")
--rm Automatically remove the container when it exits
--runtime string Runtime to use for this container
--security-opt list Security Options
--shm-size bytes Size of /dev/shm
--sig-proxy Proxy received signals to the process (default true)
--stop-signal string Signal to stop a container (default "SIGTERM")
--stop-timeout int Timeout (in seconds) to stop a container
--storage-opt list Storage driver options for the container
--sysctl map Sysctl options (default map[])
--tmpfs list Mount a tmpfs directory
-t, --tty Allocate a pseudo-TTY
--ulimit ulimit Ulimit options (default [])
-u, --user string Username or UID (format: <name|uid>[:<group|gid>])
--userns string User namespace to use
--uts string UTS namespace to use
-v, --volume list Bind mount a volume
--volume-driver string Optional volume driver for the container
--volumes-from list Mount volumes from the specified container(s)
-w, --workdir string Working directory inside the container