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:
--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
-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:
--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:
[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
[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
[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
"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:
--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=
--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 ~]#