docker入门之简单的容器使用
docker入门之简单的容器使用
首发:arppinging
创新互联建站始终坚持【策划先行,效果至上】的经营理念,通过多达十多年累计超上千家客户的网站建设总结了一套系统有效的营销解决方案,现已广泛运用于各行各业的客户,其中包括:格栅板等企业,备受客户赞赏。
一、运行容器
1)搜索和下载镜像
在之前我们打过一个比分,容器就像vmware workstation一样,而镜像类似于一个OVA文件,如果我们想让容器变得有意义,那镜像是我们必不可少的一个"货物"。
跟OVA文件类似,如果我们基于某一个镜像创建容器,那么在容器里面的任何操作不会被写入原本的镜像,除非对所有的操作进行一次镜像重构,把所有操作的结果导出形成一个新的镜像。
1.1 搜索需要的镜像
格式: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
:自动构建的
1.2 下载镜像
格式: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]#
1.3 查看已有的镜像
格式: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 /]#
2)创建和运行容器
2.1 创建容器
格式: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使用手册。
2.2 查看容器
格式: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
2.3 运行容器
格式:docker container start [OPTIONS] CONTAINER [CONTAINER...]
启动web_nginx容器
[root@node1 /]# docker container start web_nginx
web_nginx
[root@node1 /]#
2.4 进入容器
格式: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: 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: 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
Welcome to nginx!
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
For online documentation and support please refer to
nginx.org.
Commercial support is available at
nginx.com.
Thank you for using nginx.
[root@node1 /]#
2.5 查看容器的状态
格式: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 /]#
3)更加简便的方式
以上是标准的创建容器、启动容器、进入容器的步骤。但是步骤比较繁琐,在docker中,还有一种更加便捷的使用容器的方式,那就是docker run
。
docker run
不仅能一次性创建、启动、进入容器,而且当镜像不存在的时候,还会自动下载镜像,然后再基于镜像创建容器。
格式:docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
常用option:
--name:容器名字
-i:交互式访问
-t:附加终端
-d:后台运行
3.1 创建和启动容器
使用docker run
命令创建和启动一个名为web1_nginx
的容器
[root@node1 /]# docker run --name web1_nginx -d nginx:1.14-alpine
e4e2b8be9d575abeae83274b74f458ccee3379b0ee8c793ae50f6aa69f836e80
[root@node1 /]#
3.2 查看容器的详细信息
格式: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 "
},
"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 /]#
3.3 获取容器中的网页
[root@node1 /]# curl 172.17.0.2
Welcome to nginx!
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
For online documentation and support please refer to
nginx.org.
Commercial support is available at
nginx.com.
Thank you for using nginx.
[root@node1 /]#
二、停止、恢复、关闭和删除容器
1)停止和恢复容器
停止容器类似于虚拟机的挂起。
停止格式:docker container pause [OPTIONS] CONTAINER [CONTAINER...]
恢复格式:docker container unpause [OPTIONS] CONTAINER [CONTAINER...]
1.1 查看容器状态
格式: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 /]#
1.2 停止容器
格式: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
1.3 恢复容器
格式: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
Welcome to nginx!
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
For online documentation and support please refer to
nginx.org.
Commercial support is available at
nginx.com.
Thank you for using nginx.
[root@node1 /]#
2)关闭容器
关闭容器的方式有两种,一种是正常关闭,一种是关闭
2.1 关闭容器
格式:docker container stop [OPTIONS] CONTAINER [CONTAINER...]
关闭web1_nginx容器
[root@node1 /]# docker container stop web1_nginx
web1_nginx
2.2 查看容器状态
查看容器状态,此时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
2.3 启动容器
启动已经关闭的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 /]#
2.4 强制关闭容器
格式: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 /]#
3)删除容器
3.1 删除容器
将容器删除,容器处于关闭状态时才能被删除
格式: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 /]#
当前文章:docker入门之简单的容器使用
本文地址:http://scyanting.com/article/jehehs.html