運(yùn)行Docker容器的命令:docker run == docker create + docker start
查看容器:
docker ps:查看正在運(yùn)行的容器
docker ps -a:查看已經(jīng)創(chuàng)建的容器
[root@docker01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2f2bb2e87136 nginx "nginx -g 'daemon of…" 24 seconds ago Up 22 seconds 0.0.0.0:80->80/tcp thirsty_montalcini
[root@docker01 ~]#
[root@docker01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2f2bb2e87136 nginx "nginx -g 'daemon of…" 30 seconds ago Up 29 seconds 0.0.0.0:80->80/tcp thirsty_montalcini
2f7c755645e6 nginx "nginx -g 'daemon of…" About a minute ago Created kind_joliot
efe791c12bb9 nginx "nginx -g 'daemon of…" 18 hours ago Exited (0) 17 hours ago youthful_mccarthy
c57ccf765ec7 nginx "nginx -g 'daemon of…" 19 hours ago Created trusting_lumiere
[root@docker01 ~]#
停止容器:
docker stop container_id
docker container stop container_id (等效)
[root@docker01 ~]# docker stop 2f2bb2e87136
2f2bb2e87136
[root@docker01 ~]#
殺死容器:
docker kill container_id/container_name
docker container kill container_id/container_name(等效)
[root@docker01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
33f8577ba02e nginx "nginx -g 'daemon of…" 15 seconds ago Up 13 seconds 0.0.0.0:80->80/tcp xenodochial_boyd
[root@docker01 ~]# ^C
[root@docker01 ~]# docker kill 33f8577ba02e
33f8577ba02e
[root@docker01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@docker01 ~]#
刪除容器:
刪除容器 docker rm container_id1 container_id2 container_id3(一次可刪多個(gè))
批量刪除容器 docker rm -f docker ps -a -q
[root@docker01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
33f8577ba02e nginx "nginx -g 'daemon of…" 4 minutes ago Exited (137) 4 minutes ago xenodochial_boyd
2f2bb2e87136 nginx "nginx -g 'daemon of…" 4 hours ago Exited (0) 6 minutes ago thirsty_montalcini
2f7c755645e6 nginx "nginx -g 'daemon of…" 4 hours ago Created kind_joliot
efe791c12bb9 nginx "nginx -g 'daemon of…" 22 hours ago Exited (0) 20 hours ago youthful_mccarthy
c57ccf765ec7 nginx "nginx -g 'daemon of…" 22 hours ago Created trusting_lumiere
[root@docker01 ~]# docker rm 33f8577ba02e 2f2bb2e87136
33f8577ba02e
2f2bb2e87136
[root@docker01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@docker01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2f7c755645e6 nginx "nginx -g 'daemon of…" 4 hours ago Created kind_joliot
efe791c12bb9 nginx "nginx -g 'daemon of…" 22 hours ago Exited (0) 20 hours ago youthful_mccarthy
c57ccf765ec7 nginx "nginx -g 'daemon of…" 22 hours ago Created
[root@docker01 ~]# docker rm `docker ps -a -q`
2f7c755645e6
efe791c12bb9
c57ccf765ec7
[root@docker01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@docker01 ~]#
docker容器其他命令:
docker container 查看其他命令
[root@docker01 ~]# docker container
Usage: docker container COMMAND
Manage containers
Commands:
attach Attach local standard input, output, and error streams to a running container
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
exec Run a command in a running container
export Export a container's filesystem as a tar archive
inspect Display detailed information on one or more containers
kill Kill one or more running containers
logs Fetch the logs of a container
ls List containers
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
prune Remove all stopped containers
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
run Run a command in a new container
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
wait Block until one or more containers stop, then print their exit codes
Run 'docker container COMMAND --help' for more information on a command.
[root@docker01 ~]#
docker的run的擴(kuò)展:
docker run == docker create + docker start
創(chuàng)建容器:docker container create nginx:latest
啟動容器:docker container start container_id
[root@docker01 ~]# docker container create nginx:latest
6cea7211b48456a29e89139c802a08e3a40cdabb9d278deb08f8e7915f43a3ba
[root@docker01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6cea7211b484 nginx:latest "nginx -g 'daemon of…" 6 seconds ago Created lucid_sanderson
[root@docker01 ~]# docker start 6cea7211b484
6cea7211b484
[root@docker01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6cea7211b484 nginx:latest "nginx -g 'daemon of…" 47 seconds ago Up 4 seconds 80/tcp lucid_sanderson
[root@docker01 ~]# docker run -d nginx:latest
8a3657a09f00e8b36f6e42c3e6860e7b7a2255da380fcb4d846a3b79f2462606
root@docker01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8a3657a09f00 nginx:latest "nginx -g 'daemon of…" 36 seconds ago Up 36 seconds 80/tcp distracted_cartwright
6cea7211b484 nginx:latest "nginx -g 'daemon of…" About a minute ago Up About a minute 80/tcp lucid_sanderson
[root@docker01 ~]#
進(jìn)入容器:docker run -it centos:6.8 /bin/bash
-it 分配交互式的終端
--name 指定容器的名字
/bin/sh 覆蓋容器的初始命令
--cpus 指定cpu的數(shù)量
--memory 限定內(nèi)存的大小
-h 指定容器的主機(jī)名
[root@docker01 ~]# docker run -it centos:6.8 /bin/bash
[root@186fe66e29b4 /]# ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:AC:11:00:04
inet addr:172.17.0.4 Bcast:172.17.255.255 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:6 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:508 (508.0 b) TX bytes:0 (0.0 b)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
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@186fe66e29b4 /]# hostname
186fe66e29b4
[root@186fe66e29b4 /]# cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.4 186fe66e29b4
[root@186fe66e29b4 /]#
鏡像中打包了哪些命令,容器中就存在哪些命令,ip是容器自動按照順序分配,hosts也自動解析骗奖。
COMMAND 為進(jìn)入容器的初始命令间学,比如說啟動鏡像后睡眠10s
[root@docker01 ~]# docker run -it centos:6.8 sleep 10
[root@docker01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b8861cc3369a centos:6.8 "sleep 10" 20 seconds ago Exited (0) 9 seconds ago kind_brown
186fe66e29b4 centos:6.8 "/bin/bash" 5 minutes ago Exited (130) 35 seconds ago laughing_shamir
8a3657a09f00 nginx:latest "nginx -g 'daemon of…" 9 minutes ago Up 9 minutes 80/tcp distracted_cartwright
6cea7211b484 nginx:latest "nginx -g 'daemon of…" 10 minutes ago Up 9 minutes 80/tcp lucid_sanderson
[root@docker01 ~]#
--name=fxw 指定容器的名字(或--name fxw)
[root@docker01 ~]# docker run -d --name=fxw centos:6.8
e7efc2f1dcb120812d1f995eefb0147c35ff42ce392f676758191470fb00980a
[root@docker01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e7efc2f1dcb1 centos:6.8 "/bin/bash" 10 seconds ago Exited (0) 9 seconds ago fxw
b8861cc3369a centos:6.8 "sleep 10" 2 minutes ago Exited (0) 2 minutes ago kind_brown
186fe66e29b4 centos:6.8 "/bin/bash" 8 minutes ago Exited (130) 3 minutes ago laughing_shamir
8a3657a09f00 nginx:latest "nginx -g 'daemon of…" 11 minutes ago Up 11 minutes 80/tcp distracted_cartwright
6cea7211b484 nginx:latest "nginx -g 'daemon of…" 13 minutes ago Up 12 minutes 80/tcp lucid_sanderson
[root@docker01 ~]#
-h centos:指定容器的主機(jī)名
[root@docker01 ~]# docker run -it -h centos1233 centos:6.8 /bin/bash
[root@centos1233 /]#
其他命令操作:(不一一講解)
[root@docker01 ~]# docker run --help
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Run a command in a new container
Options:
--add-host list Add a custom host-to-IP mapping (host:ip)
-a, --attach list Attach to STDIN, STDOUT or STDERR
--blkio-weight uint16 Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
--blkio-weight-device list Block IO weight (relative device weight) (default [])
--cap-add list Add Linux capabilities
--cap-drop list Drop Linux capabilities
--cgroup-parent string Optional parent cgroup for the container
--cidfile string Write the container ID to the file
--cpu-period int Limit CPU CFS (Completely Fair Scheduler) period
--cpu-quota int Limit CPU CFS (Completely Fair Scheduler) quota
--cpu-rt-period int Limit CPU real-time period in microseconds
--cpu-rt-runtime int Limit CPU real-time runtime in microseconds
-c, --cpu-shares int CPU shares (relative weight)
--cpus decimal Number of CPUs
--cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)
--cpuset-mems string MEMs in which to allow execution (0-3, 0,1)
-d, --detach Run container in background and print container ID
--detach-keys string Override the key sequence for detaching a container
--device list Add a host device to the container
--device-cgroup-rule list Add a rule to the cgroup allowed devices list
--device-read-bps list Limit read rate (bytes per second) from a device (default [])
--device-read-iops list Limit read rate (IO per second) from a device (default [])
--device-write-bps list Limit write rate (bytes per second) to a device (default [])
--device-write-iops list Limit write rate (IO per second) to a device (default [])
--disable-content-trust Skip image verification (default true)
--dns list Set custom DNS servers
--dns-option list Set DNS options
--dns-search list Set custom DNS search domains
--entrypoint string Overwrite the default ENTRYPOINT of the image
-e, --env list Set environment variables
--env-file list Read in a file of environment variables
--expose list Expose a port or a range of ports
--group-add list Add additional groups to join
--health-cmd string Command to run to check health
--health-interval duration Time between running the check (ms|s|m|h) (default 0s)
--health-retries int Consecutive failures needed to report unhealthy
--health-start-period duration Start period for the container to initialize before starting health-retries countdown (ms|s|m|h) (default 0s)
--health-timeout duration Maximum time to allow one check to run (ms|s|m|h) (default 0s)
--help Print usage
-h, --hostname string Container host name
--init Run an init inside the container that forwards signals and reaps processes
-i, --interactive Keep STDIN open even if not attached
--ip string IPv4 address (e.g., 172.30.100.104)
--ip6 string IPv6 address (e.g., 2001:db8::33)
--ipc string IPC mode to use
--isolation string Container isolation technology
--kernel-memory bytes Kernel memory limit
-l, --label list Set meta data on a container
--label-file list Read in a line delimited file of labels
--link list Add link to another container
--link-local-ip list Container IPv4/IPv6 link-local addresses
--log-driver string Logging driver for the container
--log-opt list Log driver options
--mac-address string Container MAC address (e.g., 92:d0:c6:0a:29:33)
-m, --memory bytes Memory limit
--memory-reservation bytes Memory soft limit
--memory-swap bytes Swap limit equal to memory plus swap: '-1' to enable unlimited swap
--memory-swappiness int Tune container memory swappiness (0 to 100) (default -1)
--mount mount Attach a filesystem mount to the container
--name string Assign a name to the container
--network string Connect a container to a network (default "default")
--network-alias list Add network-scoped alias for the container
--no-healthcheck Disable any container-specified HEALTHCHECK
--oom-kill-disable Disable OOM Killer
--oom-score-adj int Tune host's OOM preferences (-1000 to 1000)
--pid string PID namespace to use
--pids-limit int Tune container pids limit (set -1 for unlimited)
--privileged Give extended privileges to this container
-p, --publish list Publish a container's port(s) to the host
-P, --publish-all Publish all exposed ports to random ports
--read-only Mount the container's root filesystem as read only
--restart string Restart policy to apply when a container exits (default "no")
--rm Automatically remove the container when it exits
--runtime string Runtime to use for this container
--security-opt list Security Options
--shm-size bytes Size of /dev/shm
--sig-proxy Proxy received signals to the process (default true)
--stop-signal string Signal to stop a container (default "SIGTERM")
--stop-timeout int Timeout (in seconds) to stop a container
--storage-opt list Storage driver options for the container
--sysctl map Sysctl options (default map[])
--tmpfs list Mount a tmpfs directory
-t, --tty Allocate a pseudo-TTY
--ulimit ulimit Ulimit options (default [])
-u, --user string Username or UID (format: <name|uid>[:<group|gid>])
--userns string User namespace to use
--uts string UTS namespace to use
-v, --volume list Bind mount a volume
--volume-driver string Optional volume driver for the container
--volumes-from list Mount volumes from the specified container(s)
-w, --workdir string Working directory inside the container
[root@docker01 ~]#
docker進(jìn)入容器的方法:
進(jìn)入容器的目的:排錯(cuò)茬高,調(diào)試
方法一:docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
例子:docker exec -it 容器id或容器名稱 /bin/bash
進(jìn)入容器默認(rèn)初始命令為/bin/bash用僧,如下:
[root@docker01 ~]# docker run -it centos:6.8
[root@512d307de28d /]# exit
exit
[root@docker01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
512d307de28d centos:6.8 "/bin/bash" 13 seconds ago Exited (0) 3 seconds ago dreamy_banzai
a2752e2fef9e centos:6.8 "/bin/bash" 8 minutes ago Exited (127) 5 minutes ago fervent_galois
[root@docker01 ~]
[root@docker01 ~]# docker container exec -it 2d50be31e6f2 /bin/bash
[root@2d50be31e6f2 /]#
方法二:docker attach [OPTIONS] CONTAINER
例子: docker attach 容器id或容器名字
nsenter(安裝yum install -y util-linux 棄用)
[root@docker01 ~]# docker container attach 2d50be31e6f2
[root@2d50be31e6f2 /]#
該容器只有兩個(gè)進(jìn)程一個(gè)是:/bin/bash,另一個(gè)是:運(yùn)行命令進(jìn)程
attach與exec的區(qū)別:attach與run采用的是同一個(gè)終端沫屡,相當(dāng)于同一個(gè)線程,如果采用多個(gè)窗口開發(fā)撮珠,則在一邊輸入命令沮脖,另一邊會同步出現(xiàn)金矛。而exec不是與run同一個(gè)終端,每次采用exec連接容器時(shí)勺届,都會重新分配終端(線程)驶俊,不會導(dǎo)致輸入命令同步的問題,所以建議采用exec連接進(jìn)入容器免姿,否則可能導(dǎo)致誤操作饼酿。
Docker ps命令解釋:
[root@docker01 ~]# docker ps -a -l --no-trunc
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2d50be31e6f26b029f3954692f620bf1bbec32daef11b6c6daeba1846566b2d5 centos:6.8 "/bin/bash" 26 minutes ago Exited (0) 2 minutes ago peaceful_booth
-a :顯示所有的容器,包括未運(yùn)行的胚膊。
-f :根據(jù)條件過濾顯示的內(nèi)容故俐。
--format :指定返回值的模板文件。
-l :顯示最近創(chuàng)建的容器紊婉。
-n :列出最近創(chuàng)建的n個(gè)容器药版。
--no-trunc :不截?cái)噍敵觥?br>
-q :靜默模式,只顯示容器編號喻犁。
-s :顯示總的文件大小槽片。
docker容器的核心理念
docker的本質(zhì)是:在隔離的環(huán)境運(yùn)行的一個(gè)進(jìn)程 所以:docker容器內(nèi)的第一個(gè)進(jìn)程必須一直處于前臺運(yùn)行的狀態(tài)(必須夯住)肢础,否則這個(gè)容器还栓,就會處于退出狀態(tài)!
正常情況下:docker run -d centos:6.8 是無法讓centos在后臺運(yùn)行乔妈,能夯住的命令:tail -f /-F
[root@docker01 ~]# docker run -d centos:6.8
a374f287482a063ced2831335bbeec9034524bc651d978037359c5b9fa537389
[root@docker01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a374f287482a centos:6.8 "/bin/bash" 3 seconds ago Exited (0) 2 seconds ago wonderful_feistel
[root@docker01 ~]# docker run -d centos:6.8 tail -F /var/log.txt
d52b42604011a90b516fc3313c4ad331f918eb69c0c79f7a031d908baeba3f2a
[root@docker01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d52b42604011 centos:6.8 "tail -F /var/log.txt" 6 seconds ago Up 5 seconds competent_galileo