您好,登錄后才能下訂單哦!
首發:arppinging
在之前我們打過一個比分,容器就像vmware workstation一樣,而鏡像類似于一個OVA文件,如果我們想讓容器變得有意義,那鏡像是我們必不可少的一個"貨物"。
跟OVA文件類似,如果我們基于某一個鏡像創建容器,那么在容器里面的任何操作不會被寫入原本的鏡像,除非對所有的操作進行一次鏡像重構,把所有操作的結果導出形成一個新的鏡像。
格式:docker search xxx
docker search
:查找鏡像。使用命令查找的鏡像比較少,如有需要,可以進入dockerhub.com查找。
[root@node1 /]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 10502 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1483 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 660 [OK]
jrcs/letsencrypt-nginx-proxy-companion LetsEncrypt container to use with nginx as p… 450 [OK]
kong Open-source Microservice & API Management la… 257 [OK]
webdevops/php-nginx Nginx with PHP-FPM 118 [OK]
kitematic/hello-world-nginx A light-weight nginx container that demonstr… 113
zabbix/zabbix-web-nginx-mysql Zabbix frontend based on Nginx web-server wi… 81 [OK]
bitnami/nginx Bitnami nginx Docker Image 59 [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 47 [OK]
linuxserver/nginx An Nginx container, brought to you by LinuxS… 44
tobi312/rpi-nginx NGINX on Raspberry Pi / armhf 23 [OK]
nginx/nginx-ingress NGINX Ingress Controller for Kubernetes 13
blacklabelops/nginx Dockerized Nginx Reverse Proxy Server. 12 [OK]
wodby/drupal-nginx Nginx for Drupal container image 11 [OK]
centos/nginx-18-centos7 Platform for running nginx 1.8 or building n… 8
nginxdemos/hello NGINX webserver that serves a simple page co… 8 [OK]
centos/nginx-112-centos7 Platform for running nginx 1.12 or building … 5
1science/nginx Nginx Docker images that include Consul Temp… 4 [OK]
mailu/nginx Mailu nginx frontend 2 [OK]
pebbletech/nginx-proxy nginx-proxy sets up a container running ngin… 2 [OK]
travix/nginx NGinx reverse proxy 2 [OK]
toccoag/openshift-nginx Nginx reverse proxy for Nice running on same… 1 [OK]
ansibleplaybookbundle/nginx-apb An APB to deploy NGINX 0 [OK]
wodby/nginx Generic nginx 0 [OK]
[root@node1 /]#
以上就是查找nginx鏡像的結果,以下是個字段的介紹:NAME
:鏡像名。xxx/nignx -- xxx用戶的nginx鏡像,沒有用戶名的一般都是官方鏡像。DESCRIPTION
:描述STARS
:用戶評價OFFICIAL
:是否為官方鏡像AUTOATED
:自動構建的
格式:docker image pull xxx:tag
有些鏡像經過多次的修改,有很多的版本tag。在不指定版本的情況下,會自動下載最新的版本。
[root@node1 yum.repos.d]# docker image pull nginx:1.14-alpine
1.14-alpine: Pulling from library/nginx
4fe2ade4980c: Pull complete
c691664ebb08: Pull complete
a6f6a50701b6: Pull complete
5980ba3b5a39: Pull complete
Digest: sha256:75cf17cdf89cbd8da65c83050ebdab1026b98cf217442d6a1f2a8892f47967d7
Status: Downloaded newer image for nginx:1.14-alpine
[root@node1 yum.repos.d]#
不指定版本下載
[root@node1 yum.repos.d]# docker pull busybox
Using default tag: latest
latest: Pulling from library/busybox
90e01955edcd: Pull complete
Digest: sha256:2a03a6059f21e150ae84b0973863609494aad70f0a80eaeb64bddd8d92465812
Status: Downloaded newer image for busybox:latest
[root@node1 yum.repos.d]#
格式:docker image ls
[root@node1 /]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 1.14-alpine 77bae8d00654 4 weeks ago 17.7MB
redis 4-alpine 05097a3a0549 2 months ago 30MB
busybox latest 59788edf1f3e 2 months ago 1.15MB
[root@node1 /]#
格式:docker container create [opiton] image [command][arg...]
基于nginx:1.14-alpine鏡像創建一個名為nginx_web1的容器。
[root@node1 /]# docker container create --name web_nginx nginx:1.14-alpine
a1146d1371979a4f4bccb5ada33d072b983b13cef7cb7d86af97dd149aa3fc6b
更多選項和命令可查docker使用手冊。
格式:docker container list -a
[root@node1 /]# docker container list -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a1146d137197 nginx:1.14-alpine "nginx -g 'daemon of…" 15 seconds ago Created web_nginx
格式:docker container start [OPTIONS] CONTAINER [CONTAINER...]
啟動web_nginx容器
[root@node1 /]# docker container start web_nginx
web_nginx
[root@node1 /]#
格式:docker exec [option] container [command] [arg...]
常用opiton:
-t:附加終端
-i:交互式訪問
進入容器交互式sh界面
[root@node1 /]# docker exec -it web_nginx /bin/sh
/ # ls
bin dev etc home lib media mnt proc root run sbin srv sys tmp usr var
/ #
/ # ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
24: eth0@if25: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
/ # ps aux
PID USER TIME COMMAND
1 root 0:00 nginx: master process nginx -g daemon off;
6 nginx 0:00 nginx: worker process
12 root 0:00 /bin/sh
18 root 0:00 ps aux
/ #
在docker服務器上訪問docker上的nginx網頁
[root@node1 /]# curl 172.17.0.2
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h2>Welcome to nginx!</h2>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a >nginx.org</a>.<br/>
Commercial support is available at
<a >nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@node1 /]#
格式:docker ps [option]
常用option:
-a:所有的
查看正在運行的容器
[root@node1 /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a1146d137197 nginx:1.14-alpine "nginx -g 'daemon of…" 17 minutes ago Up 8 minutes 80/tcp web_nginx
[root@node1 /]#
以上是標準的創建容器、啟動容器、進入容器的步驟。但是步驟比較繁瑣,在docker中,還有一種更加便捷的使用容器的方式,那就是docker run
。
docker run
不僅能一次性創建、啟動、進入容器,而且當鏡像不存在的時候,還會自動下載鏡像,然后再基于鏡像創建容器。
格式:docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
常用option:
--name:容器名字
-i:交互式訪問
-t:附加終端
-d:后臺運行
使用docker run
命令創建和啟動一個名為web1_nginx
的容器
[root@node1 /]# docker run --name web1_nginx -d nginx:1.14-alpine
e4e2b8be9d575abeae83274b74f458ccee3379b0ee8c793ae50f6aa69f836e80
[root@node1 /]#
格式:docker inspect containername
[root@node1 /]# docker inspect web1_nginx
[
{
"Id": "e4e2b8be9d575abeae83274b74f458ccee3379b0ee8c793ae50f6aa69f836e80",
"Created": "2018-12-08T11:10:18.774695021Z",
"Path": "nginx",
"Args": [
"-g",
"daemon off;"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 5791,
"ExitCode": 0,
"Error": "",
"StartedAt": "2018-12-08T11:10:19.146125066Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:77bae8d0065423e2338884d3698ef5ff9de8dec05a55dc81cf48ae9e78008b3f",
"ResolvConfPath": "/var/lib/docker/containers/e4e2b8be9d575abeae83274b74f458ccee3379b0ee8c793ae50f6aa69f836e80/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/e4e2b8be9d575abeae83274b74f458ccee3379b0ee8c793ae50f6aa69f836e80/hostname",
"HostsPath": "/var/lib/docker/containers/e4e2b8be9d575abeae83274b74f458ccee3379b0ee8c793ae50f6aa69f836e80/hosts",
"LogPath": "/var/lib/docker/containers/e4e2b8be9d575abeae83274b74f458ccee3379b0ee8c793ae50f6aa69f836e80/e4e2b8be9d575abeae83274b74f458ccee3379b0ee8c793ae50f6aa69f836e80-json.log",
"Name": "/web1_nginx",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "shareable",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DiskQuota": 0,
"KernelMemory": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": 0,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"MaskedPaths": [
"/proc/acpi",
"/proc/kcore",
"/proc/keys",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/proc/scsi",
"/sys/firmware"
],
"ReadonlyPaths": [
"/proc/asound",
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/e4a3721bd14332ca2dc141329145225948c10996a5f97b241f294684d3523d89-init/diff:/var/lib/docker/overlay2/69eb3722a2c91d1c958d6bce3f7ed6cc655bda3e2109e37935c436f4ee2cb812/diff:/var/lib/docker/overlay2/934f065c79fbae15243e93d47842844769fe7c66aa0dbc32a2f6eeb15903e419/diff:/var/lib/docker/overlay2/e9d7067494ef1fb51b5881fcda1a9a1fa2c7e8ede5679ee8d22b0d3c7cd12b93/diff:/var/lib/docker/overlay2/3b67f01d17ef6c823e57268a589d45653141740eb183dcb3cde5868d55b9faf5/diff",
"MergedDir": "/var/lib/docker/overlay2/e4a3721bd14332ca2dc141329145225948c10996a5f97b241f294684d3523d89/merged",
"UpperDir": "/var/lib/docker/overlay2/e4a3721bd14332ca2dc141329145225948c10996a5f97b241f294684d3523d89/diff",
"WorkDir": "/var/lib/docker/overlay2/e4a3721bd14332ca2dc141329145225948c10996a5f97b241f294684d3523d89/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "e4e2b8be9d57",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"80/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"NGINX_VERSION=1.14.1"
],
"Cmd": [
"nginx",
"-g",
"daemon off;"
],
"ArgsEscaped": true,
"Image": "nginx:1.14-alpine",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
},
"StopSignal": "SIGTERM"
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "b079c526decde91c7f776dda3d0811a92842b6e1600148d71ca7ff0659d5df5b",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"80/tcp": null
},
"SandboxKey": "/var/run/docker/netns/b079c526decd",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "2603be0a10b32ac76de23b6ba1e48f2ab8916569efcae18f7c29af58a6ecde53",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "54eb4006f6f6e37274e853f6ce8dfc268913e399ddc57e2e3a5325f2369f03bc",
"EndpointID": "2603be0a10b32ac76de23b6ba1e48f2ab8916569efcae18f7c29af58a6ecde53",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
}
}
]
[root@node1 /]#
[root@node1 /]# curl 172.17.0.2
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h2>Welcome to nginx!</h2>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a >nginx.org</a>.<br/>
Commercial support is available at
<a >nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@node1 /]#
停止容器類似于虛擬機的掛起。
停止格式:docker container pause [OPTIONS] CONTAINER [CONTAINER...]
恢復格式:docker container unpause [OPTIONS] CONTAINER [CONTAINER...]
格式:docker container ps [option]
[root@node1 /]# docker container ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e4e2b8be9d57 nginx:1.14-alpine "nginx -g 'daemon of…" 11 minutes ago Up 11 minutes 80/tcp web1_nginx
[root@node1 /]#
格式:docker container pause [OPTIONS] CONTAINER [CONTAINER...]
停止web1_nginx容器
[root@node1 /]# docker container pause web1_nginx
web1_nginx
[root@node1 /]#
查看容器狀態,可看到status上標注著paused
[root@node1 /]# docker container ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e4e2b8be9d57 nginx:1.14-alpine "nginx -g 'daemon of…" 15 minutes ago Up 15 minutes (Paused) 80/tcp web1_nginx
[root@node1 /]#
測試容器是否在運行,會發現無法獲取網頁信息。(ps.但ping得通...不知道咋回事,知道的兄弟的可否告知一下。)
[root@node1 /]# curl 172.17.0.2
格式:docker container unpause [OPTIONS] CONTAINER [CONTAINER...]
恢復web1_nginx容器
[root@node1 /]# docker container unpause web1_nginx
web1_nginx
[root@node1 /]#
查看容器狀態
[root@node1 /]# docker container ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e4e2b8be9d57 nginx:1.14-alpine "nginx -g 'daemon of…" 21 minutes ago Up 21 minutes 80/tcp web1_nginx
[root@node1 /]#
獲取容器中的網頁信息
[root@node1 /]# curl 172.17.0.2
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h2>Welcome to nginx!</h2>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a >nginx.org</a>.<br/>
Commercial support is available at
<a >nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@node1 /]#
關閉容器的方式有兩種,一種是正常關閉,一種是關閉
格式:docker container stop [OPTIONS] CONTAINER [CONTAINER...]
關閉web1_nginx容器
[root@node1 /]# docker container stop web1_nginx
web1_nginx
查看容器狀態,此時ps查看不到已經關閉的容器信息,需要使用-a
參數
[root@node1 /]# docker container ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@node1 /]# docker container ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e4e2b8be9d57 nginx:1.14-alpine "nginx -g 'daemon of…" 27 minutes ago Exited (0) 9 seconds ago web1_nginx
[root@node1 /]#
可以看到status為exited
驗證容器是否關閉,發現無法ping通
[root@node1 /]# ping 172.17.0.2
PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data.
From 172.17.0.1 icmp_seq=10 Destination Host Unreachable
From 172.17.0.1 icmp_seq=11 Destination Host Unreachable
From 172.17.0.1 icmp_seq=12 Destination Host Unreachable
啟動已經關閉的web1_nginx容器
[root@node1 /]# docker container start web1_nginx
web1_nginx
[root@node1 /]#
驗證容器是否開啟
[root@node1 /]# ping 172.17.0.2
PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data.
64 bytes from 172.17.0.2: icmp_seq=1 ttl=64 time=0.076 ms
64 bytes from 172.17.0.2: icmp_seq=2 ttl=64 time=0.062 ms
64 bytes from 172.17.0.2: icmp_seq=3 ttl=64 time=0.062 ms
^C
--- 172.17.0.2 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.062/0.066/0.076/0.011 ms
[root@node1 /]#
格式:docker container kill [OPTIONS] CONTAINER [CONTAINER...]
強制關閉web1_nginx容器
[root@node1 /]# docker container kill web1_nginx
web1_nginx
[root@node1 /]#
查看和驗證容器是否被關閉
[root@node1 /]# docker container ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e4e2b8be9d57 nginx:1.14-alpine "nginx -g 'daemon of…" 39 minutes ago Exited (137) 56 seconds ago web1_nginx
[root@node1 /]# ping 172.16.0.2
PING 172.16.0.2 (172.16.0.2) 56(84) bytes of data.
^C
--- 172.16.0.2 ping statistics ---
65 packets transmitted, 0 received, 100% packet loss, time 63999ms
[root@node1 /]#
將容器刪除,容器處于關閉狀態時才能被刪除
格式:docker container rm [OPTIONS] CONTAINER [CONTAINER...]
[root@node1 /]# docker container rm web1_nginx
web1_nginx
[root@node1 /]#
查看容器列表,發現容器web1_nginx容器已消失
[root@node1 /]# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@node1 /]# docker container ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@node1 /]#
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。