Docker基本操作命令
查看docker進程
[root@fieldyang01 run]# ps -ef|grep docker
root 6451 1 0 04:54 ? 00:00:05 /usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current --default-runtime=docker-runc --exec-opt native.cgroupdriver=systemd --userland-proxy-path=/usr/libexec/docker/docker-proxy-current --init-path=/usr/libexec/docker/docker-init-current --seccomp-profile=/etc/docker/seccomp.json --selinux-enabled=false --log-driver=journald --signature-verification=false --storage-driver overlay2
root 6460 6451 0 04:54 ? 00:00:02 /usr/bin/docker-containerd-current -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeout 2m --state-dir /var/run/docker/libcontainerd/containerd --shim docker-containerd-shim --runtime docker-runc --runtime-args --systemd-cgroup=true
root 6735 2326 0 05:07 pts/0 00:00:00 grep --color=auto docker
1、docker:查看docker使用基本說明
[root@fieldyang01 run]# docker
Usage: docker COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default "/root/.docker")
-D, --debug Enable debug mode
--help Print usage
-H, --host list Daemon socket(s) to connect to (default [])
-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:
container Manage containers
image Manage images
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
volume Manage volumes
Commands:
attach Attach 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 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.
2、docker info:查看docker信息
[root@fieldyang01 run]# docker info
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 1.13.1
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: journald
Cgroup Driver: systemd
Plugins:
Volume: local
Network: bridge host macvlan null overlay
Swarm: inactive
Runtimes: docker-runc runc
Default Runtime: docker-runc
Init Binary: /usr/libexec/docker/docker-init-current
containerd version: (expected: aa8187dbd3b7ad67d8e5e3a15115d3eef43a7ed1)
runc version: 66aedde759f33c190954815fb765eedc1d782dd9 (expected: 9df8b306d01f59d3a8029be411de015b7304dd8f)
init version: fec3683b971d9c3ef73f284f176672c44b448662 (expected: 949e6facb77383876aeff8a6944dde66b3089574)
Security Options:
seccomp
WARNING: You're not using the default seccomp profile
Profile: /etc/docker/seccomp.json
Kernel Version: 4.4.215-1.el7.elrepo.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
Number of Docker Hooks: 3
CPUs: 4
Total Memory: 984.1 MiB
Name: fieldyang01
ID: ETLN:LM7P:XOUC:CGHI:Q3K7:UVML:S4PE:SF4M:JVQI:ZI7S:S4XF:4ZJZ
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
Registries: docker.io (secure)
[root@fieldyang01 run]#
3、docker attach進入容器內部
[root@fieldyang01 run]# docker attach --help
Usage: docker attach [OPTIONS] CONTAINER
Attach to a running container
Options:
--detach-keys string Override the key sequence for detaching a container
--help Print usage
--no-stdin Do not attach STDIN
--sig-proxy Proxy all received signals to the process (default true)
實例:使用docker啓動一個交互容器並進去
[root@fieldyang01 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) (default [])
-a, --attach list Attach to STDIN, STDOUT or STDERR (default [])
--blkio-weight uint16 Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
--blkio-weight-device weighted-device Block IO weight (relative device weight) (default [])
--cap-add list Add Linux capabilities (default [])
--cap-drop list Drop Linux capabilities (default [])
--cgroup-parent string Optional parent cgroup for the container
--cidfile string Write the container ID to the file
--cpu-count int CPU count (Windows only)
--cpu-percent int CPU percent (Windows only)
--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 (default 0.000)
--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)
--credentialspec string Credential spec for managed service account (Windows only)
-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 (default [])
--device-read-bps throttled-device Limit read rate (bytes per second) from a device (default [])
--device-read-iops throttled-device Limit read rate (IO per second) from a device (default [])
--device-write-bps throttled-device Limit write rate (bytes per second) to a device (default [])
--device-write-iops throttled-device Limit write rate (IO per second) to a device (default [])
--disable-content-trust Skip image verification (default true)
--dns list Set custom DNS servers (default [])
--dns-option list Set DNS options (default [])
--dns-search list Set custom DNS search domains (default [])
--entrypoint string Overwrite the default ENTRYPOINT of the image
-e, --env list Set environment variables (default [])
--env-file list Read in a file of environment variables (default [])
--expose list Expose a port or a range of ports (default [])
--group-add list Add additional groups to join (default [])
--health-cmd string Command to run to check health
--health-interval duration Time between running the check (ns|us|ms|s|m|h) (default 0s)
--health-retries int Consecutive failures needed to report unhealthy
--health-timeout duration Maximum time to allow one check to run (ns|us|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
--init-path string Path to the docker-init binary
-i, --interactive Keep STDIN open even if not attached
--io-maxbandwidth string Maximum IO bandwidth limit for the system drive (Windows only)
--io-maxiops uint Maximum IOps limit for the system drive (Windows only)
--ip string Container IPv4 address (e.g. 172.30.100.104)
--ip6 string Container IPv6 address (e.g. 2001:db8::33)
--ipc string IPC namespace to use
--isolation string Container isolation technology
--kernel-memory string Kernel memory limit
-l, --label list Set meta data on a container (default [])
--label-file list Read in a line delimited file of labels (default [])
--link list Add link to another container (default [])
--link-local-ip list Container IPv4/IPv6 link-local addresses (default [])
--log-driver string Logging driver for the container
--log-opt list Log driver options (default [])
--mac-address string Container MAC address (e.g. 92:d0:c6:0a:29:33)
-m, --memory string Memory limit
--memory-reservation string Memory soft limit
--memory-swap string Swap limit equal to memory plus swap: '-1' to enable unlimited swap
--memory-swappiness int Tune container memory swappiness (0 to 100) (default -1)
--name string Assign a name to the container
--network string Connect a container to a network (default "default")
--network-alias list Add network-scoped alias for the container (default [])
--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)
--privileged Give extended privileges to this container
-p, --publish list Publish a container's port(s) to the host (default [])
-P, --publish-all Publish all exposed ports to random ports
--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 (default [])
--shm-size string Size of /dev/shm, default value is 64MB
--sig-proxy Proxy received signals to the process (default true)
--stop-signal string Signal to stop a container, SIGTERM by default (default "SIGTERM")
--stop-timeout int Timeout (in seconds) to stop a container
--storage-opt list Storage driver options for the container (default [])
--sysctl map Sysctl options (default map[])
--tmpfs list Mount a tmpfs directory (default [])
-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 (default [])
--volume-driver string Optional volume driver for the container
--volumes-from list Mount volumes from the specified container(s) (default [])
-w, --workdir string Working directory inside the container
[root@fieldyang01 run]
#以交互式方式運行一個Ubuntu鏡像
[root@fieldyang01 run]# docker run -itd --name ubuntu ubuntu:14.04
Unable to find image 'ubuntu:14.04' locally
Trying to pull repository docker.io/library/ubuntu ...
14.04: Pulling from docker.io/library/ubuntu
2e6e20c8e2e6: Pull complete
30bb187ac3fc: Pull complete
b7a5bcc4a58a: Pull complete
Digest: sha256:ffc76f71dd8be8c9e222d420dc96901a07b61616689a44c7b3ef6a10b7213de4
Status: Downloaded newer image for docker.io/ubuntu:14.04
f21ba87f00f9ec3448314010590dd331fef996471da79f3f510357bf5b9d7527
[root@fieldyang01 run]#
[root@fieldyang01 run]#
##查看運行的所有容器
[root@fieldyang01 run]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f21ba87f00f9 ubuntu:14.04 "/bin/bash" 45 seconds ago Up 25 seconds ubuntu
[root@fieldyang01 run]#
##進入容器內部
[root@fieldyang01 run]# docker attach ubuntu
root@f21ba87f00f9:/#
root@f21ba87f00f9:/# ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:ac:11:00:02
inet addr:172.17.0.2 Bcast:0.0.0.0 Mask:255.255.0.0
inet6 addr: fe80::42:acff:fe11:2/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:22 errors:0 dropped:0 overruns:0 frame:0
TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:2442 (2.4 KB) TX bytes:648 (648.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
root@f21ba87f00f9:/#
root@f21ba87f00f9:/# exit
exit
[root@fieldyang01 run]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f21ba87f00f9 ubuntu:14.04 "/bin/bash" About a minute ago Exited (0) 3 seconds ago ubuntu
[root@fieldyang01 run]#
[root@fieldyang01 run]# docker run -itd --name centos centos:6.8
Unable to find image 'centos:6.8' locally
Trying to pull repository docker.io/library/centos ...
6.8: Pulling from docker.io/library/centos
7ce0cebb9dca: Pull complete
Digest: sha256:39abd0c8e375de6fb7334d42ec2a46643f34cbc1bbaf37e2b484065f05eaa7a2
Status: Downloaded newer image for docker.io/centos:6.8
aa658f0d72691c31874b2a349afd8f346c330e206e805f5babd3dac53c6f007e
[root@fieldyang01 run]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aa658f0d7269 centos:6.8 "/bin/bash" 24 seconds ago Up 20 seconds centos
f21ba87f00f9 ubuntu:14.04 "/bin/bash" 3 minutes ago Exited (0) About a minute ago ubuntu
[root@fieldyang01 run]# docker attach centos
[root@aa658f0d7269 /]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
overlay overlay 17G 5.2G 12G 31% /
tmpfs tmpfs 493M 0 493M 0% /dev
tmpfs tmpfs 493M 0 493M 0% /sys/fs/cgroup
/dev/mapper/centos-root
xfs 17G 5.2G 12G 31% /etc/resolv.conf
/dev/mapper/centos-root
xfs 17G 5.2G 12G 31% /etc/hostname
/dev/mapper/centos-root
xfs 17G 5.2G 12G 31% /etc/hosts
##Ctrl+P再Ctrl+Q 即可退出容器的虛擬終端,然後容器仍在運行
[root@aa658f0d7269 /]# [root@fieldyang01 run]#
[root@fieldyang01 run]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aa658f0d7269 centos:6.8 "/bin/bash" About a minute ago Up About a minute centos
f21ba87f00f9 ubuntu:14.04 "/bin/bash" 4 minutes ago Exited (0) 2 minutes ago ubuntu
[root@fieldyang01 run]#
4、docker build 構建鏡像
[root@fieldyang01 run]# docker build --help
Usage: docker build [OPTIONS] PATH | URL | -
Build an image from a Dockerfile
Options:
--build-arg list Set build-time variables (default [])
--cache-from stringSlice Images to consider as cache sources
--cgroup-parent string Optional parent cgroup for the container
--compress Compress the build context using gzip
--cpu-period int Limit the CPU CFS (Completely Fair Scheduler) period
--cpu-quota int Limit the CPU CFS (Completely Fair Scheduler) quota
-c, --cpu-shares int CPU shares (relative weight) 控制CPU使用
--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)
--disable-content-trust Skip image verification (default true)
-f, --file string Name of the Dockerfile (Default is 'PATH/Dockerfile') 選擇dockerfile名稱
--force-rm Always remove intermediate containers
--help Print usage
--isolation string Container isolation technology
--label list Set metadata for an image (default [])
-m, --memory string Memory limit 設置內存上限
--memory-swap string Swap limit equal to memory plus swap: '-1' to enable unlimited swap
--network string Set the networking mode for the RUN instructions during build (default "default")
--no-cache Do not use cache when building the image
--pull Always attempt to pull a newer version of the image
-q, --quiet Suppress the build output and print image ID on success 不顯示構建過程的一些信息
--rm Remove intermediate containers after a successful build (default true)
--security-opt stringSlice Security options
--shm-size string Size of /dev/shm, default value is 64MB
-t, --tag list Name and optionally a tag in the 'name:tag' format (default []) 爲構建的鏡像打上標籤
--ulimit ulimit Ulimit options (default [])
-v, --volume list Set build-time bind mounts (default [])
[root@fieldyang01 run]#
5、docker commit 把當前容器提交打包爲鏡像
[root@fieldyang01 0307]# docker commit --help
Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Create a new image from a container's changes
Options:
-a, --author string Author (e.g., "John Hannibal Smith <[email protected]>") 添加作者信息
-c, --change list Apply Dockerfile instruction to the created image (default []) 修改dockerfile指令
--help Print usage
-m, --message string Commit message 提交修改信息
-p, --pause Pause container during commit (default true) 暫停正在commit的操作
[root@fieldyang01 0307]#
6、docker cp 複製文件到宿主機
[root@fieldyang01 0307]# docker cp --help
Usage: docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
Copy files/folders between a container and the local filesystem
Options:
-a, --archive Archive mode (copy all uid/gid information)
-L, --follow-link Always follow symbol link in SRC_PATH
--help Print usage
示例:
[root@fieldyang01 0307]# docker run -itd --name centos centos:6.8 bash
/usr/bin/docker-current: Error response from daemon: Conflict. The container name "/centos" is already in use by container aa658f0d72691c31874b2a349afd8f346c330e206e805f5babd3dac53c6f007e. You have to remove (or rename) that container to be able to reuse that name..
See '/usr/bin/docker-current run --help'.
[root@fieldyang01 0307]# docker exec -it centos bash
[root@aa658f0d7269 /]# mkdir yangzw
[root@aa658f0d7269 /]# mkdir -p yangzw/0307
[root@aa658f0d7269 /]# cd yangzw/0307/
[root@aa658f0d7269 0307]# echo "這是一個Docker容器。">test2.txt
[root@aa658f0d7269 0307]# ll
total 4
-rw-r--r--. 1 root root 28 Mar 7 10:19 test2.txt
[root@aa658f0d7269 0307]# exit
exit
[root@fieldyang01 0307]# docker cp centos:/yangzw/0307/test2.txt ~/yangzw/0307/
[root@fieldyang01 0307]# ll
total 8
-rw-r--r--. 1 root root 1308 Mar 7 04:24 docker.service
-rw-r--r--. 1 root root 28 Mar 7 05:19 test2.txt
[root@fieldyang01 0307]# cat test2.txt
這是一個Docker容器。
[root@fieldyang01 0307]#
7、docker create 創建容器
[root@fieldyang01 0307]# 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) (default [])
-a, --attach list Attach to STDIN, STDOUT or STDERR (default [])
--blkio-weight uint16 Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
--blkio-weight-device weighted-device Block IO weight (relative device weight) (default [])
--cap-add list Add Linux capabilities (default [])
--cap-drop list Drop Linux capabilities (default [])
--cgroup-parent string Optional parent cgroup for the container
--cidfile string Write the container ID to the file
--cpu-count int CPU count (Windows only)
--cpu-percent int CPU percent (Windows only)
--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 (default 0.000)
--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)
--credentialspec string Credential spec for managed service account (Windows only)
--device list Add a host device to the container (default [])
--device-read-bps throttled-device Limit read rate (bytes per second) from a device (default [])
--device-read-iops throttled-device Limit read rate (IO per second) from a device (default [])
--device-write-bps throttled-device Limit write rate (bytes per second) to a device (default [])
--device-write-iops throttled-device Limit write rate (IO per second) to a device (default [])
--disable-content-trust Skip image verification (default true)
--dns list Set custom DNS servers (default [])
--dns-option list Set DNS options (default [])
--dns-search list Set custom DNS search domains (default [])
--entrypoint string Overwrite the default ENTRYPOINT of the image
-e, --env list Set environment variables (default [])
--env-file list Read in a file of environment variables (default [])
--expose list Expose a port or a range of ports (default [])
--group-add list Add additional groups to join (default [])
--health-cmd string Command to run to check health
--health-interval duration Time between running the check (ns|us|ms|s|m|h) (default 0s)
--health-retries int Consecutive failures needed to report unhealthy
--health-timeout duration Maximum time to allow one check to run (ns|us|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
--init-path string Path to the docker-init binary
-i, --interactive Keep STDIN open even if not attached
--io-maxbandwidth string Maximum IO bandwidth limit for the system drive (Windows only)
--io-maxiops uint Maximum IOps limit for the system drive (Windows only)
--ip string Container IPv4 address (e.g. 172.30.100.104)
--ip6 string Container IPv6 address (e.g. 2001:db8::33)
--ipc string IPC namespace to use
--isolation string Container isolation technology
--kernel-memory string Kernel memory limit
-l, --label list Set meta data on a container (default [])
--label-file list Read in a line delimited file of labels (default [])
--link list Add link to another container (default [])
--link-local-ip list Container IPv4/IPv6 link-local addresses (default [])
--log-driver string Logging driver for the container
--log-opt list Log driver options (default [])
--mac-address string Container MAC address (e.g. 92:d0:c6:0a:29:33)
-m, --memory string Memory limit
--memory-reservation string Memory soft limit
--memory-swap string Swap limit equal to memory plus swap: '-1' to enable unlimited swap
--memory-swappiness int Tune container memory swappiness (0 to 100) (default -1)
--name string Assign a name to the container
--network string Connect a container to a network (default "default")
--network-alias list Add network-scoped alias for the container (default [])
--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)
--privileged Give extended privileges to this container
-p, --publish list Publish a container's port(s) to the host (default [])
-P, --publish-all Publish all exposed ports to random ports
--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 (default [])
--shm-size string Size of /dev/shm, default value is 64MB
--stop-signal string Signal to stop a container, SIGTERM by default (default "SIGTERM")
--stop-timeout int Timeout (in seconds) to stop a container
--storage-opt list Storage driver options for the container (default [])
--sysctl map Sysctl options (default map[])
--tmpfs list Mount a tmpfs directory (default [])
-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 (default [])
--volume-driver string Optional volume driver for the container
--volumes-from list Mount volumes from the specified container(s) (default [])
-w, --workdir string Working directory inside the container
[root@fieldyang01 0307]#
8、docker diff 查看容器變化:列出容器內發生變化的文件和目錄
變化包括添加(A-add)、刪除(D-delete)、修改(C-change)
語法:docker diff containerID
[root@fieldyang01 0307]# docker diff --help
Usage: docker diff CONTAINER
Inspect changes on a container's filesystem
Options:
--help Print usage
[root@fieldyang01 0307]#
[root@fieldyang01 0307]# docker diff centos
C /root
A /root/.bash_history
A /run
A /run/secrets
A /yangzw
A /yangzw/0307
A /yangzw/0307/test2.txt
[root@fieldyang01 0307]#
9、docker events實時輸出docker服務端事件,包括容器的創建、啓動、關閉等
[root@www 0304]# docker events --help
Usage: docker events [OPTIONS]
Get real time events from the server
-f, --filter=[] Filter output based on conditions provided
--help=false Print usage
--since= Show all events created since timestamp
--until= Stream events until this timestamp
[root@www 0304]#
示例:
--終端1
[root@fieldyang01 0307]# docker start ubuntu
ubuntu
[root@fieldyang01 0307]# docker stop ubuntu
ubuntu
--終端2
[root@fieldyang01 ~]# docker events
2020-03-07T05:24:08.910217214-05:00 container kill f21ba87f00f9ec3448314010590dd331fef996471da79f3f510357bf5b9d7527 (image=ubuntu:14.04, name=ubuntu, signal=15)
2020-03-07T05:24:08.984604686-05:00 container die f21ba87f00f9ec3448314010590dd331fef996471da79f3f510357bf5b9d7527 (exitCode=0, image=ubuntu:14.04, name=ubuntu)
2020-03-07T05:24:09.120546333-05:00 network disconnect e3d6083c132f9a2e0daa326bc04ba2d767217c4743322c7bb8b4417bb9453cbb (container=f21ba87f00f9ec3448314010590dd331fef996471da79f3f510357bf5b9d7527, name=bridge, type=bridge)
2020-03-07T05:24:09.136759944-05:00 container stop f21ba87f00f9ec3448314010590dd331fef996471da79f3f510357bf5b9d7527 (image=ubuntu:14.04, name=ubuntu)
--終端1
[root@fieldyang01 0307]# docker start ubuntu
ubuntu
[root@fieldyang01 0307]# docker exec -it ubuntu bash
root@f21ba87f00f9:/#
root@704a5c100217:/#
--終端2
2020-03-07T05:25:08.994810591-05:00 container start f21ba87f00f9ec3448314010590dd331fef996471da79f3f510357bf5b9d7527 (image=ubuntu:14.04, name=ubuntu)
2020-03-07T05:25:18.063416087-05:00 container exec_create: bash f21ba87f00f9ec3448314010590dd331fef996471da79f3f510357bf5b9d7527 (image=ubuntu:14.04, name=ubuntu)
2020-03-07T05:25:18.064823488-05:00 container exec_start: bash f21ba87f00f9ec3448314010590dd331fef996471da79f3f510357bf5b9d7527 (image=ubuntu:14.04, name=ubuntu)
10、docker exec 進入容器內部進行操作,該命令可以類似SSH登錄服務器一樣操作容器。一般選擇docker exec 代替docker attach
[root@fieldyang01 0307]# docker exec --help
Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Run a command in a running container
Options:
-d, --detach Detached mode: run command in the background 分離模式:在後臺運行命令
--detach-keys string Override the key sequence for detaching a container
-e, --env list Set environment variables (default [])
--help Print usage
-i, --interactive Keep STDIN open even if not attached 交互模式
--privileged Give extended privileges to the command
-t, --tty Allocate a pseudo-TTY 分配一個TTY
-u, --user string Username or UID (format: <name|uid>[:<group|gid>]) 指定用戶和用戶組
[root@fieldyang01 0307]#
進入一個交互式的centos容器
[root@fieldyang01 0307]# docker exec -it centos bash
[root@aa658f0d7269 /]# whoami
root
[root@aa658f0d7269 /]# exit
exit
#使用exit退出不會退出容器,此外每個exec都會分配一個TTY給用戶,不會導致衝突
[root@fieldyang01 0307]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aa658f0d7269 centos:6.8 "/bin/bash" 14 minutes ago Up 14 minutes centos
f21ba87f00f9 ubuntu:14.04 "/bin/bash" 17 minutes ago Up 2 minutes ubuntu
[root@fieldyang01 0307]#
11、docker export導出本地儲存的容器,導出的容器不會壓縮
[root@fieldyang01 0307]# docker export --help
Usage: docker export [OPTIONS] CONTAINER
Export a container's filesystem as a tar archive
Options:
--help Print usage
-o, --output string Write to a file, instead of STDOUT
[root@fieldyang01 0307]#
[root@fieldyang01 0307]# docker export -o centos.tar centos
[root@fieldyang01 0307]# docker export ubuntu >ubuntu.tar
[root@fieldyang01 0307]# ll
total 399240
-rw-------. 1 root root 202660864 Mar 7 05:29 centos.tar
-rw-r--r--. 1 root root 1308 Mar 7 04:24 docker.service
-rw-r--r--. 1 root root 28 Mar 7 05:19 test2.txt
-rw-r--r--. 1 root root 206150656 Mar 7 05:29 ubuntu.tar
[root@fieldyang01 0307]#
12、docker import 導入容器
[root@fieldyang01 0307]# docker import --help
Usage: docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]
Import the contents from a tarball to create a filesystem image
Options:
-c, --change list Apply Dockerfile instruction to the created image (default [])
--help Print usage
-m, --message string Set commit message for imported image
[root@fieldyang01 0307]#
13、docker history 顯示鏡像的歷史,用於查看鏡像的歷史變化
[root@fieldyang01 0307]# docker history --help
Usage: docker history [OPTIONS] IMAGE
Show the history of an image
Options:
--help Print usage
-H, --human Print sizes and dates in human readable format (default true)
--no-trunc Don't truncate output
-q, --quiet Only show numeric IDs
[root@fieldyang01 0307]#
[root@fieldyang01 0307]# docker history centos:6.8
IMAGE CREATED CREATED BY SIZE COMMENT
82f3b5f3c58f 11 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0 B
<missing> 11 months ago /bin/sh -c #(nop) LABEL name=CentOS Base ... 0 B
<missing> 11 months ago /bin/sh -c #(nop) ADD file:14c084af88ccd62... 195 MB
<missing> 17 months ago /bin/sh -c #(nop) MAINTAINER The CentOS P... 0 B
[root@fieldyang01 0307]#
[root@fieldyang01 0307]# docker history ubuntu:14.04
IMAGE CREATED CREATED BY SIZE COMMENT
6e4f1fe62ff1 2 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0 B
<missing> 2 months ago /bin/sh -c mkdir -p /run/systemd && echo '... 7 B
<missing> 2 months ago /bin/sh -c set -xe && echo '#!/bin/sh' >... 195 kB
<missing> 2 months ago /bin/sh -c [ -z "$(apt-get indextargets)" ] 0 B
<missing> 2 months ago /bin/sh -c #(nop) ADD file:276b5d943a4d284... 196 MB
[root@fieldyang01 0307]#
14、docker images : 列出本地鏡像。
語法
docker images [OPTIONS] [REPOSITORY[:TAG]]
OPTIONS說明:
-a :列出本地所有的鏡像(含中間映像層,默認情況下,過濾掉中間映像層);
--digests :顯示鏡像的摘要信息;
-f :顯示滿足條件的鏡像;
--format :指定返回值的模板文件;
--no-trunc :顯示完整的鏡像信息;
-q :只顯示鏡像ID。
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Options:
-a, --all Show all images (default hides intermediate images)
列出本地所有的鏡像(含中間映像層,默認情況下,過濾掉中間映像層);
--digests Show digests
顯示鏡像的摘要信息;
-f, --filter filter Filter output based on conditions provided
過濾,顯示滿足條件的鏡像;
--format string Pretty-print images using a Go template --help Print usage --no-trunc Don't truncate output -q, --quiet Only show numeric IDs
指定返回值的模板文件;列表包含了倉庫名、標籤、鏡像 ID、創建時間以及所佔用的空間。
如:列出虛懸鏡像
docker images -f dangling=true
這類鏡像已經是沒用的了,可使用
$ docker rmi $(docker images -q -f dangling=true)
進行刪除
默認情況下,docker images 會輸出一個完整的表格,但是我們並非所有時候都會需要這些內容。比如,剛纔刪除虛懸鏡像的時候,我們需要利用 docker images 把所有的虛懸鏡像的 ID 列出來,然後纔可以交給 docker rmi 命令作爲參數來刪除指定的這些鏡像,這個時候就用到了 -q 參數。
[root@fieldyang01 0307]# docker images -f dangling=false
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/ubuntu 14.04 6e4f1fe62ff1 2 months ago 197 MB
docker.io/centos 6.8 82f3b5f3c58f 11 months ago 195 MB
[root@fieldyang01 0307]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/ubuntu 14.04 6e4f1fe62ff1 2 months ago 197 MB
docker.io/centos 6.8 82f3b5f3c58f 11 months ago 195 MB
[root@fieldyang01 0307]# docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/ubuntu 14.04 6e4f1fe62ff1 2 months ago 197 MB
docker.io/centos 6.8 82f3b5f3c58f 11 months ago 195 MB
[root@fieldyang01 0307]# docker images --digests
REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE
docker.io/ubuntu 14.04 sha256:ffc76f71dd8be8c9e222d420dc96901a07b61616689a44c7b3ef6a10b7213de4 6e4f1fe62ff1 2 months ago 197 MB
docker.io/centos 6.8 sha256:39abd0c8e375de6fb7334d42ec2a46643f34cbc1bbaf37e2b484065f05eaa7a2 82f3b5f3c58f 11 months ago 195 MB
[root@fieldyang01 0307]#
[root@fieldyang01 0307]# docker images -q
6e4f1fe62ff1
82f3b5f3c58f
根據倉庫名列出鏡像
[root@fieldyang01 0307]# docker images centos
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos 6.8 82f3b5f3c58f 11 months ago 195 MB
[root@fieldyang01 0307]# docker images centos:6.8
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos 6.8 82f3b5f3c58f 11 months ago 195 MB
[root@fieldyang01 0307]#
15、docker info 查看docker信息
[root@fieldyang01 0307]# docker info
Containers: 2
Running: 2
Paused: 0
Stopped: 0
Images: 2
Server Version: 1.13.1
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: journald
Cgroup Driver: systemd
Plugins:
Volume: local
Network: bridge host macvlan null overlay
Swarm: inactive
Runtimes: runc docker-runc
Default Runtime: docker-runc
Init Binary: /usr/libexec/docker/docker-init-current
containerd version: (expected: aa8187dbd3b7ad67d8e5e3a15115d3eef43a7ed1)
runc version: 66aedde759f33c190954815fb765eedc1d782dd9 (expected: 9df8b306d01f59d3a8029be411de015b7304dd8f)
init version: fec3683b971d9c3ef73f284f176672c44b448662 (expected: 949e6facb77383876aeff8a6944dde66b3089574)
Security Options:
seccomp
WARNING: You're not using the default seccomp profile
Profile: /etc/docker/seccomp.json
Kernel Version: 4.4.215-1.el7.elrepo.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
Number of Docker Hooks: 3
CPUs: 4
Total Memory: 984.1 MiB
Name: fieldyang01
ID: ETLN:LM7P:XOUC:CGHI:Q3K7:UVML:S4PE:SF4M:JVQI:ZI7S:S4XF:4ZJZ
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
Registries: docker.io (secure)
[root@fieldyang01 0307]#
16、docker inspect 檢查容器或者鏡像詳細信息
[root@fieldyang01 0307]# docker inspect --help
Usage: docker inspect [OPTIONS] NAME|ID [NAME|ID...]
Return low-level information on Docker objects
Options:
-f, --format string Format the output using the given Go template
--help Print usage
-s, --size Display total file sizes if the type is container
--type string Return JSON for specified type
[root@fieldyang01 0307]#
docker inspect --format='{{ XXX }}' $(docker ps -aq)
一級屬性{{.屬性}} 二級屬性 {{.屬性.屬性}} 三級屬性 {{.屬性.屬性.屬性}}
17、docker inspect 源文件
[root@fieldyang01 0307]# docker inspect centos:6.8
[
{
"Id": "sha256:82f3b5f3c58f22e50d6b05f227c675af504cffc9dff7e318df5fc40faee6410e",
"RepoTags": [
"docker.io/centos:6.8"
],
"RepoDigests": [
"docker.io/centos@sha256:39abd0c8e375de6fb7334d42ec2a46643f34cbc1bbaf37e2b484065f05eaa7a2"
],
"Parent": "",
"Comment": "",
"Created": "2019-03-14T21:23:02.159760046Z",
"Container": "bb7ddd338ba52aca9c6b70f256c2bef70f3170e53c999ab34bf127cc955a2318",
"ContainerConfig": {
"Hostname": "bb7ddd338ba5",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/sh",
"-c",
"#(nop) ",
"CMD [\"/bin/bash\"]"
],
"ArgsEscaped": true,
"Image": "sha256:49f6cacb1ca91a2a20a5dc7c4624015c9c89390cdf54d0f79491141fcca148bc",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"build-date": "2016-06-02",
"license": "GPLv2",
"name": "CentOS Base Image",
"vendor": "CentOS"
}
},
"DockerVersion": "18.06.1-ce",
"Author": "The CentOS Project <[email protected]>",
"Config": {
"Hostname": "",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash"
],
"ArgsEscaped": true,
"Image": "sha256:49f6cacb1ca91a2a20a5dc7c4624015c9c89390cdf54d0f79491141fcca148bc",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"build-date": "2016-06-02",
"license": "GPLv2",
"name": "CentOS Base Image",
"vendor": "CentOS"
}
},
"Architecture": "amd64",
"Os": "linux",
"Size": 194549320,
"VirtualSize": 194549320,
"GraphDriver": {
"Name": "overlay2",
"Data": {
"MergedDir": "/var/lib/docker/overlay2/08052b4308ef70ab4e40fc137d862b3ed1026155ac9fd2f6a052d8a19b910a16/merged",
"UpperDir": "/var/lib/docker/overlay2/08052b4308ef70ab4e40fc137d862b3ed1026155ac9fd2f6a052d8a19b910a16/diff",
"WorkDir": "/var/lib/docker/overlay2/08052b4308ef70ab4e40fc137d862b3ed1026155ac9fd2f6a052d8a19b910a16/work"
}
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:ad337ac82f03249b90fb8e937abb29b9ee4dc1d1beb9251af80d0c44edfa178a"
]
}
}
]
[root@fieldyang01 0307]#
示例:
獲取容器的名字
[root@fieldyang01 0307]# docker inspect --format='{{.Name}}' $(docker ps -aq)
/centos
/ubuntu
獲取容器的pid
[root@fieldyang01 0307]# docker inspect -f='{{ .State.Pid }} {{ .Id }}' $(docker ps -aq)
7071 aa658f0d72691c31874b2a349afd8f346c330e206e805f5babd3dac53c6f007e
7674 f21ba87f00f9ec3448314010590dd331fef996471da79f3f510357bf5b9d7527
[root@fieldyang01 0307]# docker inspect -f='{{ .State.Pid }} {{ .Id }}' centos
7071 aa658f0d72691c31874b2a349afd8f346c330e206e805f5babd3dac53c6f007e
[root@fieldyang01 0307]#
18、docker kill 殺死容器
[root@fieldyang01 0307]# docker kill --help
Usage: docker kill [OPTIONS] CONTAINER [CONTAINER...]
Kill one or more running containers
Options:
--help Print usage
-s, --signal string Signal to send to the container (default "KILL")
[root@fieldyang01 0307]#
dockerstop,支持“優雅退出”。先發送SIGTERM信號,在一段時間之後(10s)再發送SIGKILL信號。Docker內部的應用程序可以接收SIGTERM信號,然後做一些“退出前工作”,比如保存狀態、處理當前請求等。
docker kill,發送SIGKILL信號,應用程序直接退出。
線上應用優雅退出十分必要。docker stop也不是docker獨有的設計,lxc和google borg系統都有類似設計,即在發送SIGKILL之前,發送SIGTERM信號通知任務。
[root@fieldyang01 0307]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aa658f0d7269 centos:6.8 "/bin/bash" 21 minutes ago Up 21 minutes centos
f21ba87f00f9 ubuntu:14.04 "/bin/bash" 25 minutes ago Up 10 minutes ubuntu
[root@fieldyang01 0307]#
ubuntu
[root@fieldyang01 0307]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aa658f0d7269 centos:6.8 "/bin/bash" 22 minutes ago Up 22 minutes centos
f21ba87f00f9 ubuntu:14.04 "/bin/bash" 25 minutes ago Exited (137) 3 seconds ago ubuntu
[root@fieldyang01 0307]#
19、docker load : 導入使用 docker save 命令導出的鏡像。
語法
docker load [OPTIONS]
OPTIONS 說明:
--input , -i : 指定導入的文件,代替 STDIN。
[root@fieldyang01 0307]# docker load --help
Usage: docker load [OPTIONS]
Load an image from a tar archive or STDIN
Options:
--help Print usage
-i, --input string Read from tar archive file, instead of STDIN
-q, --quiet Suppress the load output
20、docker save : 將指定鏡像保存成 tar 歸檔文件
[root@fieldyang01 0307]# docker save --help
Usage: docker save [OPTIONS] IMAGE [IMAGE...]
Save one or more images to a tar archive (streamed to STDOUT by default)
Options:
--help Print usage
-o, --output string Write to a file, instead of STDOUT
[root@fieldyang01 0307]#
[root@fieldyang01 0307]# docker save -o centos6.8.tar centos:6.8
[root@fieldyang01 0307]# ll
total 596716
-rw-------. 1 root root 202213376 Mar 7 05:37 centos6.8.tar
-rw-------. 1 root root 202660864 Mar 7 05:29 centos.tar
-rw-r--r--. 1 root root 1308 Mar 7 04:24 docker.service
-rw-r--r--. 1 root root 28 Mar 7 05:19 test2.txt
-rw-r--r--. 1 root root 206150656 Mar 7 05:29 ubuntu.tar
[root@fieldyang01 0307]# docker load -i centos6.8.tar
Loaded image: centos:6.8
[root@fieldyang01 0307]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/ubuntu 14.04 6e4f1fe62ff1 2 months ago 197 MB
centos 6.8 82f3b5f3c58f 11 months ago 195 MB
docker.io/centos 6.8 82f3b5f3c58f 11 months ago 195 MB
[root@fieldyang01 0307]#
21、docker login 登錄docker hub
[root@fieldyang01 0307]# docker login --help
Usage: docker login [OPTIONS] [SERVER]
Log in to a Docker registry
Options:
--help Print usage
-p, --password string Password
-u, --username string Username
[root@fieldyang01 0307]#
[root@fieldyang01 0307]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: fieldyang
Password:
Login Succeeded
[root@fieldyang01 0307]#
登錄證書保留在$HOME/.docker/config.json
[root@fieldyang01 0307]# cd /root/.docker/
[root@fieldyang01 .docker]# ll
total 4
-rw-------. 1 root root 103 Mar 7 06:27 config.json
[root@fieldyang01 .docker]# cat config.json
{
"auths": {
"https://index.docker.io/v1/": {
"auth": "ZmllbGR5YW5nOnpld2VpODM1NzY5MA=="
}
}
}[root@fieldyang01 .docker]#
22、docker logout 登出
[root@fieldyang01 .docker]# docker logout --help
Usage: docker logout [SERVER]
Log out from a Docker registr
Options:
--help Print usage
[root@fieldyang01 .docker]# docker logout
Removing login credentials for https://index.docker.io/v1/
[root@fieldyang01 .docker]# cat config.json
{
"auths": {}
}[root@fieldyang01 .docker]#
23、docker network docker網絡管理
[root@fieldyang01 ~]# docker network --help
Usage: docker network COMMAND
Manage networks
Options:
--help Print usa
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 netwoks 顯示指定網絡詳細信息
ls List networks 顯示全部docker網絡
prune Remove all unused networks 刪除所有未使用的網絡
rm Remove one or more networks 刪除指定網絡
Run 'docker network COMMAND --help' for more information on a command.
[root@fieldyang01 ~]#
[root@fieldyang01 ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
e3d6083c132f bridge bridge local
9e73921207a8 host host local
d5d12750ca8b none null local
[root@fieldyang01 ~]#
24、docker node docker集羣節點管理
[root@fieldyang01 ~]# docker node --help
Usage: docker node COMMAND
Manage Swarm nodes
Options:
--help Print usage
Commands:
demote Demote one or more nodes from manager in the swarm 將集羣中指定manager節點降權
inspect Display detailed information on one or more nodes 顯示節點詳細信息
ls List nodes in the swarm 列出集羣中所有節點
promote Promote one or more nodes to manager in the swarm 提升節點爲manager節點
ps List tasks running on one or more nodes, defaults to current node 顯示正在運行的節點
rm Remove one or more nodes from the swarm 從集羣中刪除指定節點
update Update a node 更新節點屬性
Run 'docker node COMMAND --help' for more information on a command.
[root@fieldyang01 ~]#
[root@fieldyang01 ~]# docker node ps
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
Error: No such node:
[root@fieldyang01 ~]# docker node ls
Error response from daemon: This node is not a swarm manager. Use "docker swarm init" or "docker swarm join" to connect this node to swarm and try again.
[root@fieldyang01 ~]#
25、docker pause 暫停容器
[root@fieldyang01 ~]# docker pause --help
Usage: docker pause CONTAINER [CONTAINER...]
Pause all processes within one or more containers
Options:
--help Print usage
[root@fieldyang01 ~]#
26、docker unpause 恢復容器內所有進程
[root@fieldyang01 ~]# docker unpause --help
Usage: docker unpause CONTAINER [CONTAINER...]
Unpause all processes within one or more containers
Options:
--help Print usage
[root@fieldyang01 ~]#
27、docker port 查看容器的端口
[root@fieldyang01 ~]# docker port --help
Usage: docker port CONTAINER [PRIVATE_PORT[/PROTO]]
List port mappings or a specific mapping for the container
Options:
--help Print usage
[root@fieldyang01 ~]# docker port nginx
Error: No such container: nginx
[root@fieldyang01 ~]# docker port mysql
Error: No such container: mysql
[root@fieldyang01 ~]#
28、docker ps 查看正在運行的容器。
[root@fieldyang01 ~]# docker ps --help
Usage: docker ps [OPTIONS]
List containers
Options:
-a, --all Show all containers (default shows just running) 顯示全部容器
-f, --filter filter Filter output based on conditions provided 添加過濾條件
--format string Pretty-print containers using a Go template
--help Print usage
-n, --last int Show n last created containers (includes all states) (default -1)
顯示最近出創建的幾個容器(包括所有狀態的容器)
-l, --latest Show the latest created container (includes all states) 顯示最近創建的一個
--no-trunc Don't truncate output 追加輸出持續輸出的信息
-q, --quiet Only display numeric IDs 只顯示id
-s, --size Display total file sizes 顯示容器大小
[root@fieldyang01 ~]#
[root@fieldyang01 ~]# docker ps -a -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aa658f0d7269 centos:6.8 "/bin/bash" 2 hours ago Up 2 hours centos
[root@fieldyang01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aa658f0d7269 centos:6.8 "/bin/bash" 2 hours ago Up 2 hours centos
f21ba87f00f9 ubuntu:14.04 "/bin/bash" 2 hours ago Exited (137) About an hour ago ubuntu
[root@fieldyang01 ~]# docker ps -q
aa658f0d7269
[root@fieldyang01 ~]#
29、docker pull 拉取鏡像
[root@fieldyang01 ~]# docker pull --help
Usage: docker pull [OPTIONS] NAME[:TAG|@DIGEST]
Pull an image or a repository from a registry
Options:
-a, --all-tags Download all tagged images in the repository 拉取所有標籤到本地
--disable-content-trust Skip image verification (default true) 略過鏡像校驗 =false時進行校驗
--help Print usage
[root@fieldyang01 ~]#
30、docker push 推送鏡像到鏡像倉庫
[root@fieldyang01 ~]# docker push --help
Usage: docker push [OPTIONS] NAME[:TAG]
Push an image or a repository to a registry
Options:
--disable-content-trust Skip image verification (default true)
--help Print usage
[root@fieldyang01 ~]#
31、docker rename 可以在不改變容器狀態的情況下重命名容器,該命令只對容器有效。
docker rename <舊容器名> <新容器名>
[root@fieldyang01 ~]# docker rename --help
Usage: docker rename CONTAINER NEW_NAME
Rename a container
Options:
--help Print usage
[root@fieldyang01 ~]#
32、docker restart 重啓一個或多個容器 相當於 docker stop docker start
docker restart CONTAINER1 [CONTAINER2 ...]
[root@fieldyang01 ~]# docker restart --help
Usage: docker restart [OPTIONS] CONTAINER [CONTAINER...]
Restart one or more containers
Options:
--help Print usage
-t, --time int Seconds to wait for stop before killing the container (default 10) 重啓時設置等待容器停止的時間,如果在指定的時間內沒停止,則執行docker kill 操作殺死容器,已完成重啓
[root@fieldyang01 ~]#
33、docker rm 刪除容器
[root@fieldyang01 ~]# docker rm --help
不帶參數只能刪除已經停止的一個或多個容器
Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]
Remove one or more containers
Options:
-f, --force Force the removal of a running container (uses SIGKILL) 直接刪除正在運行的容器
--help Print usage
-l, --link Remove the specified link 刪除容器與其它容器的關聯,保留容器
-v, --volumes Remove the volumes associated with the container 刪除容器的數據卷,默認不刪除
[root@fieldyang01 ~]#
[root@fieldyang01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aa658f0d7269 centos:6.8 "/bin/bash" 2 hours ago Up 2 hours centos
f21ba87f00f9 ubuntu:14.04 "/bin/bash" 2 hours ago Up 49 seconds ubuntu
[root@fieldyang01 ~]# docker rm ubuntu
Error response from daemon: You cannot remove a running container f21ba87f00f9ec3448314010590dd331fef996471da79f3f510357bf5b9d7527. Stop the container before attempting removal or use -f
[root@fieldyang01 ~]#
34、docker rmi 刪除鏡像
不指定鏡像的tag,默認刪除鏡像的latest標籤,可加多個鏡像名稱,刪除多個鏡像
[root@fieldyang01 ~]# docker rmi --help
注意:正在運行的鏡像不會被刪除,只是刪除鏡像標籤,可以使用docker commit操作重新提交容器爲鏡像。
Usage: docker rmi [OPTIONS] IMAGE [IMAGE...]
Remove one or more images
Options:
-f, --force Force removal of the image
--help Print usage
--no-prune Do not delete untagged parents
[root@fieldyang01 ~]#
35、docker run 創建一個新的容器並運行一個命令
[root@fieldyang01 ~]# 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) (default []) 動態添加一行映射到/etc/hosts文件中 格式是 fieldyang01:192.168.2.111 、對應 /etc/hosts 中 192.168.2.111 fieldyang01
-a, --attach list Attach to STDIN, STDOUT or STDERR (default [])
-a stdin: 指定標準輸入輸出內容類型,可選 STDIN/STDOUT/STDERR 三項;
--blkio-weight uint16 Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
--blkio-weight-device weighted-device Block IO weight (relative device weight) (default []) 限制容器磁盤IO
--cap-add list Add Linux capabilities (default []) 添加容器對內核的權限
--cap-drop list Drop Linux capabilities (default []) 刪除容器對內核的權限
--cgroup-parent string Optional parent cgroup for the container 指定父級cgroup
--cidfile string Write the container ID to the file 將container ID輸出到文件
--cpu-count int CPU count (Windows only) 設置CPU個數,只針對Windows
--cpu-percent int CPU percent (Windows only)設置CPU使用百分比,只針對Windows
--cpu-period int Limit CPU CFS (Completely Fair Scheduler) period 指定容器CPU使用多長時間需要做一次重新分配
--cpu-quota int Limit CPU CFS (Completely Fair Scheduler) quota 指定容器CPU時間週期內,最多可以使用多長時間
--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)設置容器對CPU的權重
--cpus decimal Number of CPUs (default 0.000)
--cpuset-cpus string CPUs in which to allow execution (0-3, 0,1) --cpuset="0-2" or --cpuset="0,1,2": 綁定容器到指定CPU運行;
--cpuset-mems string MEMs in which to allow execution (0-3, 0,1)
--credentialspec string Credential spec for managed service account (Windows only)
-d, --detach Run container in background and print container ID 後臺運行容器,並返回容器ID;
--detach-keys string Override the key sequence for detaching a container
--device list Add a host device to the container (default [])
--device-read-bps throttled-device Limit read rate (bytes per second) from a device (default [])
--device-read-iops throttled-device Limit read rate (IO per second) from a device (default [])
--device-write-bps throttled-device Limit write rate (bytes per second) to a device (default [])
--device-write-iops throttled-device Limit write rate (IO per second) to a device (default [])
--disable-content-trust Skip image verification (default true)
--dns list Set custom DNS servers (default []) --dns 8.8.8.8: 指定容器使用的DNS服務器,默認和宿主一致
--dns-option list Set DNS options (default [])
--dns-search list Set custom DNS search domains (default []) --dns-search example.com: 指定容器DNS搜索域名,默認和宿主一致;
--entrypoint string Overwrite the default ENTRYPOINT of the image
-e, --env list Set environment variables (default []) -e username="ritchie": 設置環境變量
--env-file list Read in a file of environment variables (default []) --env-file=[]: 從指定文件讀入環境變量;
--expose list Expose a port or a range of ports (default []) 開放一個端口或一組端口;
--group-add list Add additional groups to join (default [])
--health-cmd string Command to run to check health
--health-interval duration Time between running the check (ns|us|ms|s|m|h) (default 0s)
--health-retries int Consecutive failures needed to report unhealthy
--health-timeout duration Maximum time to allow one check to run (ns|us|ms|s|m|h) (default 0s)
--help Print usage
-h, --hostname string Container host name -h "mars": 指定容器的hostname;
--init Run an init inside the container that forwards signals and reaps processes
--init-path string Path to the docker-init binary
-i, --interactive Keep STDIN open even if not attached 以交互模式運行容器,通常與 -t 同時使用;
--io-maxbandwidth string Maximum IO bandwidth limit for the system drive (Windows only)
--io-maxiops uint Maximum IOps limit for the system drive (Windows only)
--ip string Container IPv4 address (e.g. 172.30.100.104)
--ip6 string Container IPv6 address (e.g. 2001:db8::33)
--ipc string IPC namespace to use
--isolation string Container isolation technology
--kernel-memory string Kernel memory limit
-l, --label list Set meta data on a container (default []) 設置標籤
--label-file list Read in a line delimited file of labels (default [])
--link list Add link to another container (default []) --link=[]: 添加鏈接到另一個容器;
--link-local-ip list Container IPv4/IPv6 link-local addresses (default [])
--log-driver string Logging driver for the container
--log-opt list Log driver options (default [])
--mac-address string Container MAC address (e.g. 92:d0:c6:0a:29:33)
-m, --memory string Memory limit 設置可用內存大小
--memory-reservation string Memory soft limit
--memory-swap string Swap limit equal to memory plus swap: '-1' to enable unlimited swap
--memory-swappiness int Tune container memory swappiness (0 to 100) (default -1)
--name string Assign a name to the container 設置容器名稱
--network string Connect a container to a network (default "default") --net="bridge": 指定容器的網絡連接類型,支持 bridge/host/none/container: 四種類型;
--network-alias list Add network-scoped alias for the container (default [])
--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)
--privileged Give extended privileges to this container 添加管理員權限
-p, --publish list Publish a container's port(s) to the host (default []) 指定端口映射,格式爲:主機(宿主)端口:容器端口
-P, --publish-all Publish all exposed ports to random ports 隨機端口映射,容器內部端口隨機映射到主機的高端口
--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 (default [])
--shm-size string Size of /dev/shm, default value is 64MB
--sig-proxy Proxy received signals to the process (default true)
--stop-signal string Signal to stop a container, SIGTERM by default (default "SIGTERM")
--stop-timeout int Timeout (in seconds) to stop a container
--storage-opt list Storage driver options for the container (default [])
--sysctl map Sysctl options (default map[])
--tmpfs list Mount a tmpfs directory (default [])
-t, --tty Allocate a pseudo-TTY 爲容器重新分配一個僞輸入終端,通常與 -i 同時使用;
--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 (default []) 綁定一個卷
--volume-driver string Optional volume driver for the container
--volumes-from list Mount volumes from the specified container(s) (default [])
-w, --workdir string Working directory inside the container 指定工作目錄
[root@fieldyang01 ~]#
實例
1)、使用docker鏡像nginx:latest以後臺模式啓動一個容器,並將容器命名爲fieldng。
docker run --name fieldng -d nginx:latest
2)、使用鏡像nginx:latest以後臺模式啓動一個容器,並將容器的80端口映射到主機隨機端口。
docker run -P -d nginx:latest
3)、使用鏡像 nginx:latest,以後臺模式啓動一個容器,將容器的 80 端口映射到主機的 80 端口,主機的目錄 /data 映射到容器的 /data。
docker run--name fieldng -p 8088:80 -v /data:/data -d nginx:latest
4)、綁定容器的 8080 端口,並將其映射到本地主機 127.0.0.1 的 80 端口上。
docker run -p 127.0.0.1:80:8080/tcp ubuntu bash
5)、使用鏡像nginx:latest以交互模式啓動一個容器,在容器內執行/bin/bash命令。
[root@fieldyang01 ~]# docker run -it nginx:latest /bin/bash
root@c295e98e7fb7:/# df
Filesystem 1K-blocks Used Available Use% Mounted on
overlay 17811456 6161164 11650292 35% /
tmpfs 503880 0 503880 0% /dev
tmpfs 503880 0 503880 0% /sys/fs/cgroup
/dev/mapper/centos-root 17811456 6161164 11650292 35% /etc/hosts
shm 65536 0 65536 0% /dev/shm
tmpfs 503880 0 503880 0% /proc/acpi
tmpfs 503880 0 503880 0% /proc/scsi
tmpfs 503880 0 503880 0% /sys/firmware
root@c295e98e7fb7:/# exit
exit
[root@fieldyang01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c295e98e7fb7 nginx:latest "/bin/bash" 15 seconds ago Exited (0) 1 second ago nostalgic_ritchie
aa658f0d7269 centos:6.8 "/bin/bash" 43 hours ago Exited (255) 16 hours ago centos
f21ba87f00f9 ubuntu:14.04 "/bin/bash" 43 hours ago Exited (0) 39 hours ago ubuntu
[root@fieldyang01 ~]# docker rm c295e98e7fb7
c295e98e7fb7
[root@fieldyang01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aa658f0d7269 centos:6.8 "/bin/bash" 43 hours ago Exited (255) 16 hours ago centos
f21ba87f00f9 ubuntu:14.04 "/bin/bash" 43 hours ago Exited (0) 39 hours ago ubuntu
[root@fieldyang01 ~]#
6)、使用鏡像 nginx:latest,以後臺模式啓動一個容器,將容器的 80 端口映射到主機的 8088 端口。
[root@fieldyang01 ~]# docker run --name fieldng -p 8088:80 -d nginx:latest
Unable to find image 'nginx:latest' locally
Trying to pull repository docker.io/library/nginx ...
latest: Pulling from docker.io/library/nginx
68ced04f60ab: Pull complete
28252775b295: Pull complete
a616aa3b0bf2: Pull complete
Digest: sha256:2539d4344dd18e1df02be842ffc435f8e1f699cfc55516e2cf2cb16b7a9aea0b
Status: Downloaded newer image for docker.io/nginx:latest
a1d25e51233a7705d3ba9bbf6aca5337bf385d103817324de4f2a3d30eb18679
可以在Windows瀏覽器上訪問https://ip:8088確認
[root@fieldyang01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a1d25e51233a nginx:latest "nginx -g 'daemon ..." 11 seconds ago Up 6 seconds 0.0.0.0:8088->80/tcp fieldng
aa658f0d7269 centos:6.8 "/bin/bash" 42 hours ago Exited (255) 16 hours ago centos
f21ba87f00f9 ubuntu:14.04 "/bin/bash" 42 hours ago Exited (0) 39 hours ago ubuntu
強制刪除運行的容器
[root@fieldyang01 ~]# docker rm -f a1d25e51233a
a1d25e51233a
[root@fieldyang01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aa658f0d7269 centos:6.8 "/bin/bash" 43 hours ago Exited (255) 16 hours ago centos
f21ba87f00f9 ubuntu:14.04 "/bin/bash" 43 hours ago Exited (0) 39 hours ago ubuntu
[root@fieldyang01 ~]#
36、docker search搜索鏡像
[root@fieldyang01 ~]# docker search --help
Usage: docker search [OPTIONS] TERM
Search the Docker Hub for images
Options:
-f, --filter filter Filter output based on conditions provided
is-automated=(true|false) 是否自動構建
is-official=(true|false) 是否官方鏡像
stars=<number> 星級
--help Print usage
--limit int Max number of search results (default 25) 限制返回的數量
--no-index Don't truncate output
--no-trunc Don't truncate output
搜索Nginx鏡像,默認顯示25個
[root@fieldyang01 ~]# docker search nginx
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/nginx Official build of Nginx. 12763 [OK]
docker.io docker.io/jwilder/nginx-proxy Automated Nginx reverse proxy for docker c... 1749 [OK]
docker.io docker.io/richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable ... 758 [OK]
docker.io docker.io/linuxserver/nginx An Nginx container, brought to you by Linu... 95
docker.io docker.io/bitnami/nginx Bitnami nginx Docker Image 77 [OK]
docker.io docker.io/tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rt... 63 [OK]
docker.io docker.io/jc21/nginx-proxy-manager Docker container for managing Nginx proxy ... 45
docker.io docker.io/nginxdemos/hello NGINX webserver that serves a simple page ... 41 [OK]
docker.io docker.io/nginx/unit NGINX Unit is a dynamic web and applicatio... 36
docker.io docker.io/jlesage/nginx-proxy-manager Docker container for Nginx Proxy Manager 35 [OK]
docker.io docker.io/nginx/nginx-ingress NGINX Ingress Controller for Kubernetes 28
docker.io docker.io/privatebin/nginx-fpm-alpine PrivateBin running on an Nginx, php-fpm & ... 21 [OK]
docker.io docker.io/schmunk42/nginx-redirect A very simple container to redirect HTTP t... 18 [OK]
docker.io docker.io/blacklabelops/nginx Dockerized Nginx Reverse Proxy Server. 13 [OK]
docker.io docker.io/nginxinc/nginx-unprivileged Unprivileged NGINX Dockerfiles 13
docker.io docker.io/centos/nginx-112-centos7 Platform for running nginx 1.12 or buildin... 12
docker.io docker.io/centos/nginx-18-centos7 Platform for running nginx 1.8 or building... 12
docker.io docker.io/raulr/nginx-wordpress Nginx front-end for the official wordpress... 12 [OK]
docker.io docker.io/nginx/nginx-prometheus-exporter NGINX Prometheus Exporter 9
docker.io docker.io/sophos/nginx-vts-exporter Simple server that scrapes Nginx vts stats... 7 [OK]
docker.io docker.io/mailu/nginx Mailu nginx frontend 6 [OK]
docker.io docker.io/bitnami/nginx-ingress-controller Bitnami Docker Image for NGINX Ingress Con... 4 [OK]
docker.io docker.io/ansibleplaybookbundle/nginx-apb An APB to deploy NGINX 1 [OK]
docker.io docker.io/centos/nginx-110-centos7 Platform for running nginx 1.10 or buildin... 0
docker.io docker.io/wodby/nginx Generic nginx 0 [OK]
[root@fieldyang01 ~]# docker search -f stars=100 nginx
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/nginx Official build of Nginx. 12763 [OK]
docker.io docker.io/jwilder/nginx-proxy Automated Nginx reverse proxy for docker c... 1749 [OK]
docker.io docker.io/richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable ... 758 [OK]
搜索stars超過100且是官方構建的Nginx鏡像
[root@fieldyang01 ~]# docker search -f stars=100 -f is-official=true nginx
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/nginx Official build of Nginx. 12763 [OK]
搜索stars超過100且是自動構建的Ubuntu鏡像
[root@fieldyang01 ~]# docker search -f stars=100 -f is-automated=true ubuntu
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/dorowu/ubuntu-desktop-lxde-vnc Docker image to provide HTML5 VNC interfac... 401 [OK]
docker.io docker.io/rastasheep/ubuntu-sshd Dockerized SSH service, built on top of of... 243 [OK]
docker.io docker.io/consol/ubuntu-xfce-vnc Ubuntu container with "headless" VNC sessi... 211 [OK]
限制查找兩個鏡像
[root@fieldyang01 ~]# docker search nginx --limit 2
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/nginx Official build of Nginx. 12763 [OK]
docker.io docker.io/jwilder/nginx-proxy Automated Nginx reverse proxy for docker c... 1749 [OK]
[root@fieldyang01 ~]#
37、docker start啓動容器
[root@fieldyang01 ~]# docker start --help
Usage: docker start [OPTIONS] CONTAINER [CONTAINER...]
Start one or more stopped containers
Options:
-a, --attach Attach STDOUT/STDERR and forward signals 選擇顯示 STDOUT/STDERR 信息
--detach-keys string Override the key sequence for detaching a container
--help Print usage
-i, --interactive Attach container's STDIN 顯示STDIN信息
38、docker stop停止容器
[root@fieldyang01 ~]# docker stop --help
Usage: docker stop [OPTIONS] CONTAINER [CONTAINER...]
Stop one or more running containers
Options:
--help Print usage
-t, --time int Seconds to wait for stop before killing it (default 10) 等待多少時間後停止容器
[root@fieldyang01 ~]#
39、docker stats 查看容器信息
[root@fieldyang01 ~]# docker stats --help
默認只顯示正在運行的容器信息
Usage: docker stats [OPTIONS] [CONTAINER...]
Display a live stream of container(s) resource usage statistics
Options:
-a, --all Show all containers (default shows just running) 顯示全部
--format string Pretty-print images using a Go template
--help Print usage
--no-stream Disable streaming stats and only pull the first result 只查看某一時間的狀態信息
[root@fieldyang01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aa658f0d7269 centos:6.8 "/bin/bash" 3 hours ago Up 3 hours centos
f21ba87f00f9 ubuntu:14.04 "/bin/bash" 3 hours ago Exited (0) 44 seconds ago ubuntu
[root@fieldyang01 ~]# docker stats
CONTAINER CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
aa658f0d7269 0.00% 348 KiB / 984.1 MiB 0.03% 3.1 kB / 648 B 8.1 MB / 0 B 1
CONTAINER CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
aa658f0d7269 0.00% 348 KiB / 984.1 MiB 0.03% 3.1 kB / 648 B 8.1 MB / 0 B 1
CONTAINER CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
aa658f0d7269 0.00% 348 KiB / 984.1 MiB 0.03% 3.1 kB / 648 B 8.1 MB / 0 B 1
^C
[root@fieldyang01 ~]# docker stats -a
CONTAINER CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
aa658f0d7269 0.00% 348 KiB / 984.1 MiB 0.03% 3.1 kB / 648 B 8.1 MB / 0 B 1
f21ba87f00f9 -- -- / -- -- -- -- --
CONTAINER CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
aa658f0d7269 0.00% 348 KiB / 984.1 MiB 0.03% 3.1 kB / 648 B 8.1 MB / 0 B 1
f21ba87f00f9 -- -- / -- -- -- -- --
^C
[root@fieldyang01 ~]# docker stats --no-stream centos
CONTAINER CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
centos 0.00% 348 KiB / 984.1 MiB 0.03% 3.1 kB / 648 B 8.1 MB / 0 B 1
[root@fieldyang01 ~]#
40、docker top 查看容器進程
查看指定容器內正在運行的進程
[root@fieldyang01 ~]# docker top --help
Usage: docker top CONTAINER [ps OPTIONS]
Display the running processes of a container
Options:
--help Print usage
[root@fieldyang01 ~]#
[root@fieldyang01 ~]# docker top centos
UID PID PPID C STIME TTY TIME CMD
root 7071 7052 0 05:18 pts/1 00:00:00 /bin/bash
[root@fieldyang01 ~]#
41、docker service 管理集羣中的服務,需與 docker swarm配合使用,使用該命令時,主機必須是swarm的manager。
[root@fieldyang01 ~]# docker service --help
Usage: docker service COMMAND
Manage services
Options:
--help Print usage
Commands:
create Create a new service 創建服務
inspect Display detailed information on one or more services 取得服務的詳細信息
ls List services 列出服務
ps List the tasks of a service 取得服務的詳細運行情況
rm Remove one or more services 刪除service
scale Scale one or multiple replicated services 調整服務的replicas
update Update a service 更新服務
Run 'docker service COMMAND --help' for more information on a command.
[root@fieldyang01 ~]#
42、docker swarm管理集羣
[root@fieldyang01 ~]# docker swarm --help
Usage: docker swarm COMMAND
Manage Swarm
Options:
--help Print usage
Commands:
init Initialize a swarm 初始化集羣
join Join a swarm as a node and/or manager 以node(worker)或manager身份加入集羣
join-token Manage join tokens 管理join-token
leave Leave the swarm 退出集羣
unlock Unlock swarm 解鎖集羣
unlock-key Manage the unlock key
update Update the swarm 更新集羣
Run 'docker swarm COMMAND --help' for more information on a command.
[root@fieldyang01 ~]#
43、docker tag 設置鏡像標籤
[root@fieldyang01 ~]# docker tag --help
Usage: docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
Options:
--help Print usage
[root@fieldyang01 ~]#
44、docker update更新容器設置
使用時不需停止容器或重啓,會更新容器啓動時添加的參數,該命令不能使容器內部的應用程序程序重新讀取程序的配置。
例如:不能使Nginx容器重新加載配置,需要重新加載Nginx配置則需重啓容器
[root@fieldyang01 ~]# docker update --help
Usage: docker update [OPTIONS] CONTAINER [CONTAINER...]
Update configuration of one or more containers
Options:
--blkio-weight uint16 Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
--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 the CPU real-time period in microseconds
--cpu-rt-runtime int Limit the CPU real-time runtime in microseconds
-c, --cpu-shares int CPU shares (relative weight)
--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)
--help Print usage
--kernel-memory string Kernel memory limit
-m, --memory string Memory limit
--memory-reservation string Memory soft limit
--memory-swap string Swap limit equal to memory plus swap: '-1' to enable unlimited swap
--restart string Restart policy to apply when a container exits 當前更新的配置在下次重啓時生效
[root@fieldyang01 ~]#
45、docker version查看docker版本
[root@fieldyang01 ~]# docker version --help
Usage: docker version [OPTIONS]
Show the Docker version information
Options:
-f, --format string Format the output using the given Go template
--help Print usage
[root@fieldyang01 ~]#
[root@fieldyang01 ~]# docker version
Client:
Version: 1.13.1
API version: 1.26
Package version: docker-1.13.1-109.gitcccb291.el7.centos.x86_64
Go version: go1.10.3
Git commit: cccb291/1.13.1
Built: Tue Mar 3 17:21:24 2020
OS/Arch: linux/amd64
Server:
Version: 1.13.1
API version: 1.26 (minimum version 1.12)
Package version: docker-1.13.1-109.gitcccb291.el7.centos.x86_64
Go version: go1.10.3
Git commit: cccb291/1.13.1
Built: Tue Mar 3 17:21:24 2020
OS/Arch: linux/amd64
Experimental: false
[root@fieldyang01 ~]#
46、docker volume管理數據卷
數據卷是容器數據持久化的一個組件,數據卷是docker體系第一部分。
[root@fieldyang01 ~]# docker volume --help
Usage: docker volume COMMAND
Manage volumes
Options:
--help Print usage
Commands:
create Create a volume 創建一個數據卷
inspect Display detailed information on one or more volumes 查看數據卷詳細信息
ls List volumes 查看本機的全部數據卷
prune Remove all unused volumes 刪除所有不在使用的數據卷
rm Remove one or more volumes 因爲數據卷與容器的生命週期完全獨立,所以容器刪除後數據卷不會清除,會持續堆積,使用該命令可以刪除不需要的數據卷
Run 'docker volume COMMAND --help' for more information on a command.
[root@fieldyang01 ~]#
47、docker wait設置等待
[root@fieldyang01 ~]# docker wait --help
Usage: docker wait CONTAINER [CONTAINER...]
Block until one or more containers stop, then print their exit codes
Options:
--help Print usage
[root@fieldyang01 ~]#
[root@fieldyang01 ~]# docker wait centos
^C
[root@fieldyang01 ~]#