启动docker服务
systemctl start docker
查看docker命令
查看所有命令
docker
查看某个详细命令
docker <command> --help
eg.
docker run --help
docker logs --help
启动容器:docker run
[wztshine@localhost ~]$ docker run ubuntu:15.10 /bin/echo 'hh'
hh
指定容器名:--name <名字>
[root@bogon ~]# docker run -d -P --name test training/webapp python app.py
578bd586972d194057383c109fcbfe855da53cf6bb10c546a439ddcaf19df932
[root@bogon ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
578bd586972d training/webapp "python app.py" 4 seconds ago Up 2 seconds 0.0.0.0:32769->5000/tcp test
docker run <镜像> <命令>
交互模式:-it
[wztshine@localhost ~]$ docker run -it ubuntu:15.10 /bin/bash
root@912f52b64384:/# ls <--- 此时已经进入交互模式,执行ls命令
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@912f52b64384:/# exit <--- 退出此交互模式
exit
-i:交互
-t:打开一个假的终端
后台模式: -d
[wztshine@localhost ~]$ docker run -d ubuntu:15.10 /bin/sh -c "while true; do echo hello world; sleep 1; done"
ded25db4a4d155e39ccfcf8acd766a2e741359bb64f0a775b3487f469663ed69 <--- 打印的容器id
列出活动容器:ps
ps -a:列出所有容器
[wztshine@localhost ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ded25db4a4d1 ubuntu:15.10 "/bin/sh -c 'while t…" About a minute ago Up About a minute agitated_wright
显示的信息依次是:
容器id;镜像;执行的命令;创建时间;容器状态;端口和连接类型(tcp/udp);容器名字;
查看容器日志:docker logs
[wztshine@localhost ~]$ docker logs agitated_wright <--- 可以使用名字,也可以使用容器id
hello world
hello world
hello world
hello world
hello world
。。。。。
停止容器: docker stop
[wztshine@localhost ~]$ docker stop agitated_wright
agitated_wright
[wztshine@localhost ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
拉取镜像: docker pull
当本地没有想要的镜像时,可以从仓库获取镜像,默认是latest
docker pull ubuntu
启动停止的容器: docker start
[wztshine@localhost ~]$ docker ps -a <--- 查找所有容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ded25db4a4d1 ubuntu:15.10 "/bin/sh -c 'while t…" 12 minutes ago Exited (137) 4 minutes ago agitated_wright
912f52b64384 ubuntu:15.10 "/bin/bash" 17 minutes ago Exited (0) 17 minutes ago objective_brattain
a0f58c903808 ubuntu:15.10 "/bin/echo hh" 18 minutes ago Exited (0) 18 minutes ago elegant_satoshi
97121c19a47c ubuntu:15.10 "/bin/echo" 18 minutes ago Exited (0) 18 minutes ago charming_poitras
12bd0811d316 ubuntu:15.10 "/bin/sh -c 'while t…" 25 hours ago Exited (137) 25 hours ago happy_wilson
0478af7610bd ubuntu:15.10 "/bin/bash" 25 hours ago Exited (0) 25 hours ago magical_faraday
035d693a9276 ubuntu:15.10 "/bin/echo 'Hello wo…" 25 hours ago Exited (0) 25 hours ago keen_hodgkin
[wztshine@localhost ~]$ docker start ded25db <--- 启动某个容器id
ded25db
[wztshine@localhost ~]$ docker ps <--- 查看是否有活动的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ded25db4a4d1 ubuntu:15.10 "/bin/sh -c 'while t…" 12 minutes ago Up 6 seconds agitated_wright
进入容器:docker exec
[wztshine@localhost ~]$ docker run -itd ubuntu:15.10 /bin/bash
29f69fe50b7fd6eeee2ac30039e738737317930a63173e5bd355d4beb8e96d3b
在使用 -d 参数时,容器启动后会进入后台。此时想要进入容器,可以通过以下指令进入:
-
docker attach
-
[wztshine@localhost ~]$ docker attach 29f6 <--- 进入容器 root@29f69fe50b7f:/# ls bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@29f69fe50b7f:/# exit 《--- 退出 exit [wztshine@localhost ~]$ docker ps 《--- 再看一下,容器没了 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
-
-
docker exec:推荐大家使用 docker exec 命令,因为此退出容器终端,不会导致容器的停止。
-
[wztshine@localhost ~]$ docker ps <--- 查看有无活动容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 29f69fe50b7f ubuntu:15.10 "/bin/bash" About a minute ago Up About a minute optimistic_nightingale [wztshine@localhost ~]$ docker exec -it 29f6 /bin/bash <--- 进入容器 root@29f69fe50b7f:/# ls bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@29f69fe50b7f:/# exit <--- 退出容器 exit [wztshine@localhost ~]$ docker ps <--- 查看容器是否还在 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 29f69fe50b7f ubuntu:15.10 "/bin/bash" 2 minutes ago Up 2 minutes optimistic_nightingale
-
导出容器: docker export
导出成快照
[wztshine@localhost ~]$ docker ps -a 《--- 查看所有的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
29f69fe50b7f ubuntu:15.10 "/bin/bash" 6 minutes ago Exited (0) 2 minutes ago optimistic_nightingale
[wztshine@localhost ~]$ docker export 29f6 > 29f6.tar 《--- 将id位29f6的容器导出成tar
[wztshine@localhost ~]$ ls
29f6.tar dfg 公共 模板 视频 图片 文档 下载 音乐 桌面
导入成镜像
ubuntu:[wztshine@localhost ~]$ cat 29f6.tar | docker import - ubuntu:f6 《--- 从tar导入成镜像ubuntu:f6,冒号后面代表版本号
sha256:3e225315cacfc54f483cc732176aaf29ae460168220784ee7c630912b972ffd9
[wztshine@localhost ~]$ docker image ls <---可以看出镜像库多了一个f6版本的镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu f6 3e225315cacf 6 seconds ago 119MB
ubuntu 15.10 9b9cb95443b5 4 years ago 137MB
删除容器:docker rm
docker容器建立后,不主动删除,是一直存在的,ps -a 可以查看所有的容器。
[wztshine@localhost ~]$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0478af7610bd ubuntu:15.10 "/bin/bash" 25 hours ago Exited (0) 25 hours ago magical_faraday
035d693a9276 ubuntu:15.10 "/bin/echo 'Hello wo…" 25 hours ago Exited (0) 25 hours ago keen_hodgkin
[wztshine@localhost ~]$ docker rm 0478 《--- 删掉id位0478的容器
0478
[wztshine@localhost ~]$ docker ps -a 《--- 再看已经没了
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
035d693a9276 ubuntu:15.10 "/bin/echo 'Hello wo…" 25 hours ago Exited (0) 25 hours ago keen_hodgkin
运行web应用
拉取镜像
docker pull training/webapp
docker run -d -P training/webapp python app.py
- -d:让容器在后台运行。
- -P:将容器内部使用的网络端口随机映射到我们使用的主机上。
查看容器:
[wztshine@localhost ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dea5b5682a85 training/webapp "python app.py" 55 seconds ago Up 52 seconds 0.0.0.0:32768->5000/tcp determined_banzai
发现PORTS有了内容:0.0.0.0:32768->5000/tcp
Docker 开放了 5000 端口(默认 Python Flask 端口)映射到主机端口 32768 上。
查看本机ip
[wztshine@localhost ~]$ ifconfig
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
inet6 fe80::42:92ff:fec7:1314 prefixlen 64 scopeid 0x20<link>
ether 02:42:92:c7:13:14 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 11 bytes 1556 (1.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
浏览器输入ip:port,也就是http://172.17.0.1:32768/,就可以看到Hello World!
查看端口映射
docker port <id/名字>
[wztshine@localhost ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dea5b5682a85 training/webapp "python app.py" 11 minutes ago Up 11 minutes 0.0.0.0:32768->5000/tcp determined_banzai
[wztshine@localhost ~]$ docker port dea5b
5000/tcp -> 0.0.0.0:32768
查看web程序日志
docker logs -ft <name/id>
-f:跟踪输出
-t:时间戳
[wztshine@localhost ~]$ docker logs -ft dea5b
2020-08-06T13:30:12.940557354Z * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
2020-08-06T13:38:15.104668410Z 172.17.0.1 - - [06/Aug/2020 13:38:15] "GET / HTTP/1.1" 200 -
2020-08-06T13:38:15.391461776Z 172.17.0.1 - - [06/Aug/2020 13:38:15] "GET /favicon.ico HTTP/1.1" 404 -
2020-08-06T13:38:40.266207601Z 172.17.0.1 - - [06/Aug/2020 13:38:40] "GET / HTTP/1.1" 200 -
2020-08-06T13:38:40.584193528Z 172.17.0.1 - - [06/Aug/2020 13:38:40] "GET /favicon.ico HTTP/1.1" 404 -
查看容器内部的进程
docker top <id>
[wztshine@localhost ~]$ docker top dea5b
UID PID PPID C STIME TTY TIME CMD
root 3701 3685 0 21:30 ? 00:00:00 python app.py
查看容器配置和状态
docker inspect <名字或id>
[wztshine@localhost ~]$ docker inspect dea5b
[
{
"Id": "dea5b5682a85bfeeb9ad1470f32bd1dd13dd8e43ee9407aa11d3dcd52cea07be",
"Created": "2020-08-06T13:30:10.630690845Z",
"Path": "python",
"Args": [
"app.py"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 3701,
"ExitCode": 0,
"Error": "",
"StartedAt": "2020-08-06T13:30:12.58211791Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:6fae60ef344644649a39240b94d73b8ba9c67f898ede85cf8e947a887b3e6557",
"ResolvConfPath": "/var/lib/docker/containers/dea5b5682a85bfeeb9ad1470f32bd1dd13dd8e43ee9407aa11d3dcd52cea07be/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/dea5b5682a85bfeeb9ad1470f32bd1dd13dd8e43ee9407aa11d3dcd52cea07be/hostname",
"HostsPath": "/var/lib/docker/containers/dea5b5682a85bfeeb9ad1470f32bd1dd13dd8e43ee9407aa11d3dcd52cea07be/hosts",
"LogPath": "/var/lib/docker/containers/dea5b5682a85bfeeb9ad1470f32bd1dd13dd8e43ee9407aa11d3dcd52cea07be/dea5b5682a85bfeeb9ad1470f32bd1dd13dd8e43ee9407aa11d3dcd52cea07be-json.log",
"Name": "/determined_banzai",
"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,
"Capabilities": null,
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "private",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": true,
"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,
"DeviceRequests": null,
"KernelMemory": 0,
"KernelMemoryTCP": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": null,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"MaskedPaths": [
"/proc/asound",
"/proc/acpi",
"/proc/kcore",
"/proc/keys",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/proc/scsi",
"/sys/firmware"
],
"ReadonlyPaths": [
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/43bb7e4fa43bbb32b931ffc3912ebf807b99fb2156acf9fb51b23d1411079bea-init/diff:/var/lib/docker/overlay2/1efff0912c5b2c6d788779c5f6d5bf6112a180ba53df6fce25a5c83dcb69af16/diff:/var/lib/docker/overlay2/2594e721e20c0aa6a343188e6fb3eb72e5febb10a1d117721775a67f7a01a1d4/diff:/var/lib/docker/overlay2/92fd8e1d0f0e2821430e9edb192aa32e3315d613475f0a973f9c2387c5663118/diff:/var/lib/docker/overlay2/cb78cacea1fe29891ca18b6fc109d9e21415abb990f409c4727bb1189e83ee86/diff:/var/lib/docker/overlay2/a117a02f4a730d0a9e633e476f8cf5c64643fb4e672d9648ac1d397c47d680fb/diff:/var/lib/docker/overlay2/3d42ccdfcabcc2261024b2c2ff00012ec4dd24a6e861bf2f411201d04326b06e/diff:/var/lib/docker/overlay2/1ccbfd081dc4e0bac3d97a8b75901c7475cc49d16d02ab53d96d3bca3a16d4a2/diff:/var/lib/docker/overlay2/edb883e5028ddfcb215ad4305921be8bbb108a3726c67e50c60aa95dbe6fe944/diff:/var/lib/docker/overlay2/8412f7d58496959bc379d6960ef082295c658404a0cc5cbdc1bc950695b4fb44/diff:/var/lib/docker/overlay2/a6adcf51ec67906df97203e518f00573fd66d8e7e4b74373ad295108d754ebfd/diff:/var/lib/docker/overlay2/800a5722157f0547309c12f3c179e3fbda028c11ba688cbf8f6a1da1639293fb/diff:/var/lib/docker/overlay2/b1c67df61ae347206d9da134f5a30dad3fb7e224e239964f6dd52e4d8ee3666f/diff:/var/lib/docker/overlay2/4979747741cbede5341027f2b6d328235f6799d62ddef5d03405eaed27ffd6c1/diff",
"MergedDir": "/var/lib/docker/overlay2/43bb7e4fa43bbb32b931ffc3912ebf807b99fb2156acf9fb51b23d1411079bea/merged",
"UpperDir": "/var/lib/docker/overlay2/43bb7e4fa43bbb32b931ffc3912ebf807b99fb2156acf9fb51b23d1411079bea/diff",
"WorkDir": "/var/lib/docker/overlay2/43bb7e4fa43bbb32b931ffc3912ebf807b99fb2156acf9fb51b23d1411079bea/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "dea5b5682a85",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"5000/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"python",
"app.py"
],
"Image": "training/webapp",
"Volumes": null,
"WorkingDir": "/opt/webapp",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "75b674918f782f54c66e816cf8d158ca5f874603a24d966a8bd27d95758a6514",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"5000/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "32768"
}
]
},
"SandboxKey": "/var/run/docker/netns/75b674918f78",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "a69b9135b267ca8c987570b9fae8709bc8bc4ccfa538d0f3732c9442aee45511",
"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": "17cb509b719556b590c2bf53731b373eb2ee7162c840d742fbcff6c4925d8d56",
"EndpointID": "a69b9135b267ca8c987570b9fae8709bc8bc4ccfa538d0f3732c9442aee45511",
"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
}
}
}
}
]
镜像管理
查看本地镜像
docker images
[wztshine@localhost ~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu f6 3e225315cacf 47 hours ago 119MB
ubuntu 15.10 9b9cb95443b5 4 years ago 137MB
training/webapp latest 6fae60ef3446 5 years ago 349MB
- REPOSITORY:表示镜像的仓库源
- TAG:镜像的标签,也就是版本
- IMAGE ID:镜像ID
- CREATED:镜像创建时间
- SIZE:镜像大小
查找镜像
docker search <镜像名>
[wztshine@localhost ~]$ docker search httpd
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
httpd The Apache HTTP Server Project 3124 [OK]
centos/httpd-24-centos7 Platform for running Apache httpd 2.4 or bui… 36
centos/httpd 30 [OK]
arm32v7/httpd The Apache HTTP Server Project 9
salim1983hoop/httpd24 Dockerfile running apache config 2 [OK]
NAME: 镜像仓库源的名称
DESCRIPTION: 镜像的描述
OFFICIAL: 是否 docker 官方发布
stars: 类似 Github 里面的 star,表示点赞、喜欢的意思。
AUTOMATED: 自动构建。
删除镜像
docker rmi <名字或id>
[wztshine@localhost ~]$ docker rmi 3e2253
Untagged: ubuntu:f6
Deleted: sha256:3e225315cacfc54f483cc732176aaf29ae460168220784ee7c630912b972ffd9
Deleted: sha256:2cd15aaedf66e78afca6f1e79923e64c1a6c7bf4eaf85523dd1d63f509d6d5e5
更新镜像
更新一个镜像文件的步骤:
- 用镜像生成一个容器
[wztshine@localhost ~]$ docker run -it ubuntu:15.10 /bin/bash
- 更新并退出:apt-get update
root@e6537aea240a:/# apt-get update
Ign http://archive.ubuntu.com wily InRelease
Ign http://archive.ubuntu.com wily-updates InRelease
Ign http://archive.ubuntu.com wily-security InRelease
......
root@e6537aea240a:/# exit
- 通过命令 docker commit 来提交容器副本
-m:描述;
-a:作者;
36537aea:容器id;
ubuntu:v2.0:版本
[wztshine@localhost ~]$ docker commit -m="has update" -a="wztshine" e6537aea ubuntu:v2.0
sha256:40f24a5226d780c80e9a8a6427c522f8ce25b7ee54d66eaeb582c2c38b7c80ca
查看一下:
[wztshine@localhost ~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu v2.0 40f24a5226d7 6 seconds ago 137MB
构建镜像
建立Dockerfile
文件
内容如下:
FROM centos:6.7
MAINTAINER Test "Test@sl.com"
RUN /bin/echo 'root:123456' |chpasswd
RUN useradd test
RUN /bin/echo 'test:123456' |chpasswd
RUN /bin/echo -e "LANG="en_US.UTF-8"" >/etc/default/local
EXPOSE 22
CMD /bin/bash
FROM:继承自那个镜像,也就是说在哪个镜像的基础上构建
MAINTAINER:指定作者
RUN:执行命令,并将命令结果打包进镜像。(也就是对镜像进行预先修改,这里进行的是修改密码,添加用户:test等操作),每个run命令,都会创建一个镜像层
EXPOSE:暴露的端口22,允许外部连接这个端口。
CMD:由此镜像创建的容器启动后,自动执行的命令。只允许有一个CMD命令
创建镜像
-t:制定镜像名
[wztshine@localhost ~]$ docker build -t centos:6.7 ./Dockerfile
Sending build context to Docker daemon 238MB
Step 1/8 : FROM centos:6.7
6.7: Pulling from library/centos
cbddbc0189a0: Pull complete
Digest: sha256:4c952fc7d30ed134109c769387313ab864711d1bd8b4660017f9d27243622df1
Status: Downloaded newer image for centos:6.7
---> 9f1de3c6ad53
Step 2/8 : MAINTAINER Test "Test@sl.com"
---> Running in 66f3d9dcaf5d
Removing intermediate container 66f3d9dcaf5d
---> 300267754ddd
Step 3/8 : RUN /bin/echo 'root:123456' |chpasswd
---> Running in 27b24b53f996
Removing intermediate container 27b24b53f996
---> 9e4ad9273e5e
Step 4/8 : RUN useradd test
---> Running in b0b830439dc4
Removing intermediate container b0b830439dc4
---> 27af3cc1c8ba
Step 5/8 : RUN /bin/echo 'test:123456' |chpasswd
---> Running in 3edb1be95fd6
Removing intermediate container 3edb1be95fd6
---> c59f2ded7165
Step 6/8 : RUN /bin/echo -e "LANG="en_US.UTF-8"" >/etc/default/local
---> Running in 88f535a9109b
Removing intermediate container 88f535a9109b
---> c46e8516fbf8
Step 7/8 : EXPOSE 22
---> Running in d84ab8e305e9
Removing intermediate container d84ab8e305e9
---> fce8cb8ef4dc
Step 8/8 : CMD /bin/bash
---> Running in 9973f4a4dccd
Removing intermediate container 9973f4a4dccd
---> d9f083b70267
Successfully built d9f083b70267
Successfully tagged centos:6.7
查看是否成功:
[wztshine@localhost ~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 6.7 d9f083b70267 43 seconds ago 191MB
创建个容器试试:
[wztshine@localhost ~]$ docker run -it centos:6.7
[root@32c3f948f232 /]# id test
uid=500(test) gid=500(test) groups=500(test)
创建标签
docker tag <镜像id> <镜像名>:<tag名>
给 id 为 d9f083 的镜像加个tag:self
[wztshine@localhost ~]$ docker tag d9f083 centos:self
[wztshine@localhost ~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 6.7 d9f083b70267 12 minutes ago 191MB
centos self d9f083b70267 12 minutes ago 191MB
容器连接
网络端口映射
-P:端口随机映射
-p:自定义端口映射
随机映射
[root@bogon ~]# docker run -d -P training/webapp python app.py
ba3f3d1bbdc8b5cc9b372ffac71dda253577e6b6c7c1c54ffd39c434e975bfe2
[root@bogon ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ba3f3d1bbdc8 training/webapp "python app.py" 5 seconds ago Up 2 seconds 0.0.0.0:32768->5000/tcp trusting_khayyam
映射到指定端口
[root@bogon ~]# docker run -d -p 5000:5000 training/webapp python app.py
16a3ef33cc1899167f406e65348a7ca7e0a514329da9bf156fe96760630ece6f
[root@bogon ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
16a3ef33cc18 training/webapp "python app.py" 3 seconds ago Up 2 seconds 0.0.0.0:5000->5000/tcp eloquent_easley
ba3f3d1bbdc8 training/webapp "python app.py" About a minute ago Up About a minute 0.0.0.0:32768->5000/tcp trusting_khayyam
指定网络地址和协议
[root@bogon ~]# docker run -d -p 127.0.0.1:5000:5000/udp training/webapp python app.py
03b53b6aba98285d514b632792424266e799ccbf4a71184c0e6624c9aed6aff0
[root@bogon ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
03b53b6aba98 training/webapp "python app.py" 3 seconds ago Up 2 seconds 5000/tcp, 127.0.0.1:5000->5000/udp nifty_lovelace
16a3ef33cc18 training/webapp "python app.py" 3 minutes ago Up 3 minutes 0.0.0.0:5000->5000/tcp eloquent_easley
ba3f3d1bbdc8 training/webapp "python app.py" 4 minutes ago Up 4 minutes 0.0.0.0:32768->5000/tcp trusting_khayyam
[root@bogon ~]#
容器互联互通
建立网络
docker network create -d <类型> <网络名字>
-d:指定类型,有 bridge、overlay。
[root@bogon ~]# docker network create -d bridge test-net
c6cdcc9908765692564dd1a01ef3db86a2a016af3e9e628827345eea2d7f113d
在网络中连接容器
建立两个容器test1
和test2
,并给它们指定网络在一个网络里
[root@bogon ~]# docker run -itd --name test1 --network test-net ubuntu:15.10 /bin/bash
2ef95f89cf6e4b401ee5045029a8c2f9ca39eaee683e8521bbb19519dd3803bd
[root@bogon ~]# docker run -itd --name test2 --network test-net ubuntu:15.10 /bin/bash
f4929e2fd89009161fe60591fc5b871880ede081523e003945d5d801983ea566
进入其中一个容器,并ping另一个容器名,能ping通。
[root@bogon ~]# docker exec -it test1 /bin/bash
root@2ef95f89cf6e:/# ping test2
PING test2 (172.18.0.3) 56(84) bytes of data.
64 bytes from test2.test-net (172.18.0.3): icmp_seq=1 ttl=64 time=0.118 ms
64 bytes from test2.test-net (172.18.0.3): icmp_seq=2 ttl=64 time=0.098 ms
64 bytes from test2.test-net (172.18.0.3): icmp_seq=3 ttl=64 time=0.093 ms
^C
--- test2 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2001ms
rtt min/avg/max/mdev = 0.093/0.103/0.118/0.010 ms
ps:找不到命令
(如果上面ping test2时,提示没有ping这个命令
root@2ef95f89cf6e:/# ping test
bash: ping: command not found
可以执行apt-get update
,然后执行apt install iputils-ping
,如果apt-get update
也执行不成功
root@2ef95f89cf6e:/# apt-get update
Ign http://archive.ubuntu.com wily InRelease
Ign http://archive.ubuntu.com wily-updates InRelease
Ign http://archive.ubuntu.com wily-security InRelease
Ign http://archive.ubuntu.com wily Release.gpg
Ign http://archive.ubuntu.com wily-updates Release.gpg
Ign http://archive.ubuntu.com wily-security Release.gpg
Ign http://archive.ubuntu.com wily Release
Ign http://archive.ubuntu.com wily-updates Release
Ign http://archive.ubuntu.com wily-security Release
30% [Waiting for headers]
Err http://archive.ubuntu.com wily/main Sources
404 Not Found [IP: 91.189.88.142 80]
Err http://archive.ubuntu.com wily/restricted Sources
404 Not Found [IP: 91.189.88.142 80]
说明你的更新源是国外的源,需要指定国内镜像源:
root@1b4671904bfa:/# mv /etc/apt/sources.list /etc/apt/sources.list.bak
root@1b4671904bfa:/# echo "deb http://mirrors.163.com/debian/ jessie main non-free contrib" >/etc/apt/sources.list
root@1b4671904bfa:/# echo "deb http://mirrors.163.com/debian/ jessie-proposed-updates main non-free contrib" >>/etc/apt/sources.list
root@1b4671904bfa:/# echo "deb-src http://mirrors.163.com/debian/ jessie main non-free contrib" >>/etc/apt/sources.list
root@1b4671904bfa:/# echo "deb-src http://mirrors.163.com/debian/ jessie-proposed-updates main non-free contrib" >>/etc/apt/sources.list
再重新apt-get update
和apt install iputils-ping
,就好了。)
配置DNS
我们可以在宿主机的 /etc/docker/daemon.json 文件中增加以下内容来设置全部容器的 DNS:
{
"dns" : [
"114.114.114.114",
"8.8.8.8"
]
}
设置后,启动容器的 DNS 会自动配置为 114.114.114.114 和 8.8.8.8。
配置完,需要重启 docker 才能生效: systemctl restart docker
查看容器的 DNS 是否生效可以使用以下命令,它会输出容器的 DNS 信息:
--rm:退出时直接删掉容器
[root@bogon ~]# docker run -it --rm ubuntu:15.10 cat etc/resolv.conf
search Home
nameserver 114.114.114.114
nameserver 8.8.8.8
给某个容器设置DNS
[root@bogon ~]# docker run -it --rm -h host_test --dns=114.114.114.114 ubuntu:15.10
root@host_test:/# cat /etc/hostname
host_test
root@host_test:/# 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.2 host_test
root@host_test:/# cat /etc/resolv.conf
search Home
nameserver 114.114.114.114
-h: HOSTNAME 或者 --hostname=HOSTNAME: 设定容器的主机名,它会被写到容器内的 /etc/hostname 和 /etc/hosts。
Docker 仓库管理
仓库(Repository)是集中存放镜像的地方。以下介绍一下 Docker Hub。当然不止 docker hub,只是远程的服务商不一样,操作都是一样的。
Docker Hub
目前 Docker 官方维护了一个公共仓库 Docker Hub。
大部分需求都可以通过在 Docker Hub 中直接下载镜像来实现。
注册
在 https://hub.docker.com 免费注册一个 Docker 账号。
登录和退出
登录需要输入用户名和密码,登录成功后,我们就可以从 docker hub 上拉取自己账号下的全部镜像。
$ docker login
退出
$ docker logout
拉取镜像
你可以通过 docker search 命令来查找官方仓库中的镜像,并利用 docker pull 命令来将它下载到本地。
以 ubuntu 为关键词进行搜索:
$ docker search ubuntu
使用 docker pull 将官方 ubuntu 镜像下载到本地:
$ docker pull ubuntu
推送镜像
用户登录后,可以通过 docker push 命令将自己的镜像推送到 Docker Hub。
以下命令中的 username 请替换为你的 Docker 账号用户名。
$ docker tag ubuntu:18.04 username/ubuntu:18.04
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED ...
ubuntu 18.04 275d79972a86 6 days ago ...
username/ubuntu 18.04 275d79972a86 6 days ago ...
$ docker push username/ubuntu:18.04
$ docker search username/ubuntu
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
username/ubuntu
Dockerfile
FROM nginx
RUN yum install wget
&& wget -O redis.tar.gz "http://download.redis.io/releases/redis-5.0.3.tar.gz"
COPY hom* /mydir/
CMD /bin/bash