创建一个新的容器但不启动它。语法同docker run。
docker create [OPTIONS] CONTAINER
示例:
在一个终端执行docker create,在另一个终端执行docker events。
# 第一个终端
fly@fly:~$ docker create nginx
ae61ef82b9477f32607a6f4a438d1f02711ad0c77309808f075fa0a8c7e7a35a
# 第二个终端
fly@fly:~$ docker events
2022-12-08T14:58:30.633969517Z container create ae61ef82b9477f32607a6f4a438d1f02711ad0c77309808f075fa0a8c7e7a35a (image=nginx, maintainer=NGINX Docker Maintainers <[email protected]>, name=pedantic_poincare)
创建一个新的容器并运行一个命令。
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
帮助:
fly@fly:~$ 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
docker run --rm -it --add-host db-static:86.75.30.9 ubuntu cat /etc/hosts
docker run --rm -a stdin -a stdout -i -t ubuntu /bin/bash
docker run --rm --cidfile /tmp/idfile ubuntu
docker run --rm -it --entrypoint /bin/bash nginx
docker run --rm --group-add audio --group-add nogroup --group-add 777 ubuntu id
# ps 对比容器的元祖进程
docker run --rm -it ubuntu bash
docker run --rm -it --init ubuntu bash
# 挂载volume
docker run -t -i --rm --mount type=volume,target=/data ubuntu bash
# 创建目录
mkdir /tmp/data
# 修改目录所有权,仅当使用userns的情况下需要
sudo chown 165536:root /tmp/data
# bind 磁盘已存在目录
docker run -t -i --rm --mount type=bind,src=/tmp/data,dst=/data ubuntu bash
no:当容器退出时,不要自动重新启动
on-failure[:max-retries]:仅当容器以非零退出状态退出时才重新启动。
always:无论退出状态如何,始终重新启动容器。无论容器的当前状态如何,容器也将始终在守护程序
启动时启动
unless-stopped:论退出状态如何,始终重新启动容器,包括守护程序启动时,除非容器在Docker守护
程序停止之前处于停止状态。
docker run -dit --rm --tmpfs /run1:rw,noexec,nosuid,size=65536k ubuntu bash
docker run --cap-add=ALL --cap-drop=MKNOD ...
docker run --cap-add=SYS_ADMIN ...
docker run --cap-add=CAP_SYS_ADMIN ...
#示例:
fly@fly:~$ docker run --cap-add=ALL --cap-drop=MKNOD nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2022/12/09 14:44:55 [notice] 1#1: using the "epoll" event method
2022/12/09 14:44:55 [notice] 1#1: nginx/1.23.2
2022/12/09 14:44:55 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2022/12/09 14:44:55 [notice] 1#1: OS: Linux 5.4.0-135-generic
2022/12/09 14:44:55 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/12/09 14:44:55 [notice] 1#1: start worker processes
2022/12/09 14:44:55 [notice] 1#1: start worker process 29
2022/12/09 14:44:55 [notice] 1#1: start worker process 30
^C2022/12/09 14:45:05 [notice] 1#1: signal 2 (SIGINT) received, exiting
2022/12/09 14:45:05 [notice] 30#30: exiting
2022/12/09 14:45:05 [notice] 29#29: exiting
2022/12/09 14:45:05 [notice] 30#30: exit
2022/12/09 14:45:05 [notice] 29#29: exit
2022/12/09 14:45:05 [notice] 1#1: signal 17 (SIGCHLD) received from 30
2022/12/09 14:45:05 [notice] 1#1: worker process 29 exited with code 0
2022/12/09 14:45:05 [notice] 1#1: worker process 30 exited with code 0
2022/12/09 14:45:05 [notice] 1#1: exit
linux系统:只支持default,即linux命名空间隔离技术。
windows系统:default:表示使用docker守护进程配置的选项,否则默认使用process选项。
process:命名空间隔离
hyperv:基于Hyper-V 管理程序基于分区的隔离
示例:
docker run --rm --health-cmd "curl http://localhost" --health-interval 2s --health-retries 5 --health-start-period 10s --health-timeout 1s nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2022/12/09 14:51:51 [notice] 1#1: using the "epoll" event method
2022/12/09 14:51:51 [notice] 1#1: nginx/1.23.2
2022/12/09 14:51:51 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2022/12/09 14:51:51 [notice] 1#1: OS: Linux 5.4.0-135-generic
2022/12/09 14:51:51 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/12/09 14:51:51 [notice] 1#1: start worker processes
2022/12/09 14:51:51 [notice] 1#1: start worker process 29
2022/12/09 14:51:51 [notice] 1#1: start worker process 30
127.0.0.1 - - [09/Dec/2022:14:51:53 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.74.0" "-"
127.0.0.1 - - [09/Dec/2022:14:51:55 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.74.0" "-"
127.0.0.1 - - [09/Dec/2022:14:51:57 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.74.0" "-"
docker start [OPTIONS] CONTAINER [CONTAINER...]
docker stop [OPTIONS] CONTAINER [CONTAINER...]
docker restart [OPTIONS] CONTAINER [CONTAINER...]
示例:
docker restart mynginx
docker stop mynginx
docker start mynginx
杀掉一个运行中的容器。
docker kill [OPTIONS] CONTAINER [CONTAINER...]
OPTIONS说明:
-s :向容器发送一个信号。
命令 | 默认信号量 | 默认信号量的值 |
---|---|---|
kill | TERM | 15 |
docker kill | KILL | 9 |
docker stop | TERM | 15 |
示例:
docker kill -s KILL mynginx
docker kill -s TERM mynginx
如果容器终止时的状态对用户而言很重要,用户可能会想要了解docker kill 和docker stop之间的区别。docker kill 的行为和标准的kill 命令行程序并不相同。kill 程序的默认工作方式是向指定的进程发送
TERM信号(即信号值为15)。这个信号表示程序应该被终止,但是不要强迫程序终止。当这个信号被处理时,大多数程序将执行某种清理工作,但是该程序也可以执行其他操作,包括忽略该信号。
而docker kill 对正在运行的程序使用的是KILL信号,这使得该进程没办法处理终止过程。这就意味着一些诸如包含运行进程ID之类的文件可能会残留在文件系统中。根据应该用程序管理状态的能力,如果再次启动容器,这可能会也可能不会造成问题。docker stop 命令则像kill 命令那样工作,发送的是一个TERM型号。
删除一个或多个容器。
docker rm [OPTIONS] CONTAINER [CONTAINER...]
OPTIONS说明:
-f :通过 SIGKILL 信号强制删除一个运行中的容器。
-l :移除容器间的网络连接,而非容器本身。
-v :删除与容器关联的卷。
示例:
删除所有的容器。
docker rm `docker ps -f status=exited -q`
fly@fly:~$ docker rm `docker ps -f status=exited -q`
002e50006cc4
14ccc9a0dc5a
36f6736b9c62
cf6976e1333f
1f2d1a8d657f
361a3588db08
5b94ae9400df
fly@fly:~$
fly@fly:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
docker pause CONTAINER [CONTAINER...]
docker unpause CONTAINER [CONTAINER...]
(1)docker的create命令和run命令区别在于create只创建容器,而run命令除了创建容器还会连接网络和启动容器;start命令会连接网络和启动容器。即docker run = docker create + docker start。
(2)docker stop发送的信号值默认是15,但是nginx构建的时候会指定为3;docker kill发送的信号值是9,强制删除。
(3)docker run使用最多的参数:
本专栏知识点是通过<零声教育>的系统学习,进行梳理总结写下文章,对c/c++linux系统提升感兴趣的读者,可以点击链接查看详细的服务:C/C++服务器开发 。