【容器化】Docker 使用Ⅰ
Docker Hello World
C:\Users\www49>docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
Ubuntu 下运行 Hello world 镜像
runoob@runoob:~$ docker run ubuntu:15.10 /bin/echo "Hello world"
Hello world
运行交互式的容器
我们通过 docker
的两个参数 -i -t
,让 docker 运行的容器实现 “对话” 的能力:
runoob@runoob:~$ docker run -i -t ubuntu:15.10 /bin/bash
root@0123ce188bd8:/#
注意第二行 root@0123ce188bd8:/#
,此时我们已进入一个 ubuntu15.10 系统的容器。
在容器中运行命令 cat /proc/version
和 ls
分别查看当前系统的版本信息和当前目录下的文件列表。
root@0123ce188bd8:/# cat /proc/version
Linux version 4.4.0-151-generic (buildd@lgw01-amd64-043) (gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.10) ) #178-Ubuntu SMP Tue Jun 11 08:30:22 UTC 2019
root@0123ce188bd8:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@0123ce188bd8:/#
通过运行 exit
命令或者使用 CTRL+D
来退出容器。
root@0123ce188bd8:/# exit
exit
root@runoob:~#
注意第三行中 root@runoob:~# 表明我们已经退出了当前的容器,返回到当前的主机中。
启动容器(后台模式)
使用以下命令创建一个以进程方式运行的容器
runoob@runoob:~$ docker run -d ubuntu:15.10 /bin/sh -c "while true; do echo hello world; sleep 1; done"
2b1b7a428627c51ab8810d541d759f072b4fc75487eed05812646b8534a2fe63
首先,我们需要确认容器有在运行,可以通过 docker ps 来查看:
runoob@runoob:~$ docker ps
CONTAINER ID IMAGE COMMAND ...
5917eac21c36 ubuntu:15.10 "/bin/sh -c 'while t…" ...
- PORTS:容器的端口信息和使用的连接类型
tcp/udp
。 - NAMES:自动分配的容器名称。
在宿主主机内使用 docker logs
命令,查看容器内的标准输出:
docker logs 5917eac21c36
停止容器
使用 docker stop
命令来停止容器
docker logs 5917eac21c36
通过 docker ps
查看,容器已经停止工作:
docker ps
Docker 的使用
输入 docker 命令来查看到 Docker 客户端的所有命令选项。
docker
C:\Users\www49>docker stats --help
Usage: docker stats [OPTIONS] [CONTAINER...]
Display a live stream of container(s) resource usage statistics
Aliases:
docker container stats, docker stats
Options:
-a, --all Show all containers (default shows just running)
--format string Format output using a custom template:
'table': Print output in table format
with column headers (default)
'table TEMPLATE': Print output in table format
using the given Go template
'json': Print in JSON format
'TEMPLATE': Print output using the given
Go template.
Refer to https://docs.docker.com/go/formatting/
for more information about formatting output with
templates
--no-stream Disable streaming stats and only pull the first result
--no-trunc Do not truncate output
获取镜像
如果我们本地没有 ubuntu 镜像,我们可以使用 docker pull 命令来载入 ubuntu 镜像:
$ docker pull ubuntu
启动容器
以下命令使用 ubuntu 镜像启动一个容器,参数为以命令行模式进入该容器:
$ docker run -it ubuntu /bin/bash
要退出终端,直接输入 exit:
root@ed09e4490c57:/# exit
后台运行
$ docker run -itd --name ubuntu-test ubuntu /bin/bash
注:加了 -d
参数默认不会进入容器,想要进入容器需要使用指令 docker exec
停止一个容器
停止容器的命令如下:
$ docker stop <容器 ID>
停止的容器可以通过 docker restart 重启:
$ docker restart <容器 ID>
进入容器
attach 命令
$ docker attach 1e560fca3906
注: 如果从这个容器退出,会导致容器的停止。
exec 命令
docker exec -it 243c32535da7 /bin/bash
注意: 如果从这个容器退出,容器不会停止,这就是为什么推荐大家使用 docker exec
的原因
导出和导入容器
导出容器
如果要导出本地某个容器,可以使用 docker export
命令。
$ docker export 1e560fca3906 > ubuntu.tar
导出容器 1e560fca3906 快照到本地文件 ubuntu.tar。
导入容器快照
$ cat docker/ubuntu.tar | docker import - test/ubuntu:v1
也可以通过指定 URL 或者某个目录来导入,例如:
$ docker import http://example.com/exampleimage.tgz example/imagerepo
删除容器
删除容器使用 docker rm
命令:
$ docker rm -f 1e560fca3906
清理掉所有处于终止状态的容器
$ docker container prune
运行一个 web 应用
接下来让我们尝试使用 docker 构建一个 web 应用程序。
我们将在docker容器中运行一个 Python Flask 应用来运行一个web应用。
runoob@runoob:~# docker pull training/webapp # 载入镜像
runoob@runoob:~# docker run -d -P training/webapp python app.py
查看 WEB 应用容器
使用 docker ps 来查看我们正在运行的容器:
runoob@runoob:~# docker ps
CONTAINER ID IMAGE COMMAND ... PORTS
d3d5e39ed9d3 training/webapp "python app.py" ... 0.0.0.0:32769->5000/tcp
这里多了端口信息。
PORTS
0.0.0.0:32769->5000/tcp
这时我们可以通过浏览器访问WEB应用
我们也可以通过 -p
参数来设置不一样的端口:
runoob@runoob:~$ docker run -d -p 5000:5000 training/webapp python app.py
runoob@runoob:~# docker ps
CONTAINER ID IMAGE PORTS NAMES
bf08b7f2cd89 training/webapp ... 0.0.0.0:5000->5000/tcp py_web
d3d5e39ed9d3 training/webapp ... 0.0.0.0:32769->5000/tcp xenodochial_hoov
容器内部的 5000 端口映射到我们本地主机的 5000 端口上。
网络端口的快捷方式
上面我们创建的 web 应用容器 ID 为 bf08b7f2cd89 名字为 py_web。
我可以使用 docker port bf08b7f2cd89
或 docker port py_web
来查看容器端口的映射情况。
runoob@runoob:~$ docker port bf08b7f2cd89
5000/tcp -> 0.0.0.0:5000
runoob@runoob:~$ docker port py_web
5000/tcp -> 0.0.0.0:5000
查看 WEB 应用程序日志
docker logs [ID或者名字]
可以查看容器内部的标准输出。
runoob@runoob:~$ docker logs -f bf08b7f2cd89
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
192.168.239.1 - - [09/May/2016 16:30:37] "GET / HTTP/1.1" 200 -
192.168.239.1 - - [09/May/2016 16:30:37] "GET /favicon.ico HTTP/1.1" 404 -
查看WEB应用程序容器的进程
使用 docker top
来查看容器内部运行的进程
runoob@runoob:~$ docker top py_web
UID PID PPID ... TIME CMD
root 23245 23228 ... 00:00:00 python app.py
检查 WEB 应用程序
runoob@runoob:~$ docker inspect py_web
[
{
"Id": "bf08b7f2cd897b5964943134aa6d373e355c286db9b9885b1f60b6e8f82b2b85",
"Created": "2018-09-17T01:41:26.174228707Z",
"Path": "python",
"Args": [
"app.py"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 23245,
"ExitCode": 0,
"Error": "",
"StartedAt": "2018-09-17T01:41:26.494185806Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
......
停止 WEB 应用容器
runoob@runoob:~$ docker stop py_web
py_web
重启WEB应用容器
已经停止的容器,我们可以使用命令 docker start
来启动。
runoob@runoob:~$ docker start py_web
py_web
docker ps -l
查询最后一次创建的容器:
docker ps -l
CONTAINER ID IMAGE PORTS NAMES
bf08b7f2cd89 training/webapp ... 0.0.0.0:5000->5000/tcp py_web
- 正在运行的容器,我们可以使用
docker restart
命令来重启。
移除 WEB 应用容器
使用 docker rm
命令来删除不需要的容器
runoob@runoob:~$ docker rm py_web
py_web
删除容器时,容器必须是停止状态,否则会报如下错误
unoob@runoob:~$ docker rm wizardly_chandrasekhar
Error response from daemon: You cannot remove a running container bf08b7f2cd897b5964943134aa6d373e355c286db9b9885b1f60b6e8f82b2b85. Stop the container before attempting removal or force remove
Docker 镜像使用
列出本地镜像
- 使用
docker images
来列出本地主机上的镜像。
runoob@runoob:~$ docker run -t -i ubuntu:15.10 /bin/bash
root@d77ccb2e5cca:/#
如果要使用版本为 14.04 的 ubuntu 系统镜像来运行容器时,命令如下:
runoob@runoob:~$ docker run -t -i ubuntu:14.04 /bin/bash
root@39e968165990:/#
获取一个新的镜像
Crunoob@runoob:~$ docker pull ubuntu:13.10
13.10: Pulling from library/ubuntu
6599cadaf950: Pull complete
23eda618d451: Pull complete
f0be3084efe9: Pull complete
52de432f084b: Pull complete
a3ed95caeb02: Pull complete
Digest: sha256:15b79a6654811c8d992ebacdfbd5152fcf3d165e374e264076aa435214a947a3
Status: Downloaded newer image for ubuntu:13.10
查找镜像
docker search httpd
拖取镜像
使用命令 docker pull
来下载镜像。
C:\Users\www49>docker pull httpd
Using default tag: latest
latest: Pulling from library/httpd
9e3ea8720c6d: Pull complete
35c516cd98eb: Pull complete
3050fa5900bc: Pull complete
f14b7012c455: Pull complete
014a91cb6174: Pull complete
Digest: sha256:90254ccc7352e1a5c8d1e4cdab2a032cefac9fd5d4d632ca003a2943c9a9b0a3
Status: Downloaded newer image for httpd:latest
docker.io/library/httpd:latest
下载完成后,我们就可以使用这个镜像了。
docker run httpd
删除镜像
镜像删除使用 docker rmi 命令,比如我们删除 hello-world 镜像:
$ docker rmi hello-world
创建镜像
更新镜像
更新镜像之前,我们需要使用镜像来创建一个容器。
runoob@runoob:~$ docker run -t -i ubuntu:15.10 /bin/bash
root@e218edb10161:/#
runoob@runoob:~$ docker commit -m="has update" -a="runoob" e218edb10161 runoob/ubuntu:v2
sha256:70bf1840fd7c0d2d8ef0a42a817eb29f854c1af8f7c59fc03ac7bdee9545aff8
我们可以使用 docker images 命令来查看我们的新镜像 runoob/ubuntu:v2
runoob@runoob:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
runoob/ubuntu v2 70bf1840fd7c 15 seconds ago 158.5 MB
ubuntu 14.04 90d5884b1ee0 5 days ago 188 MB
php 5.6 f40e9e0f10c8 9 days ago 444.8 MB
nginx latest 6f8d099c3adc 12 days ago 182.7 MB
mysql 5.6 f2e8d6c772c0 3 weeks ago 324.6 MB
httpd latest 02ef73cf1bc0 3 weeks ago 194.4 MB
ubuntu 15.10 4e3b13c8a266 4 weeks ago 136.3 MB
hello-world latest 690ed74de00f 6 months ago 960 B
training/webapp latest 6fae60ef3446 12 months ago 348.8 MB
构建镜像
runoob@runoob:~$ cat Dockerfile
FROM centos:6.7
MAINTAINER Fisher "fisher@sudops.com"
RUN /bin/echo 'root:123456' |chpasswd
RUN useradd runoob
RUN /bin/echo 'runoob:123456' |chpasswd
RUN /bin/echo -e "LANG=\"en_US.UTF-8\"" >/etc/default/local
EXPOSE 22
EXPOSE 80
CMD /usr/sbin/sshd -D
runoob@runoob:~$ docker build -t runoob/centos:6.7 .
Sending build context to Docker daemon 17.92 kB
Step 1 : FROM centos:6.7
---> d95b5ca17cc3
Step 2 : MAINTAINER Fisher "fisher@sudops.com"
---> Using cache
---> 0c92299c6f03
Step 3 : RUN /bin/echo 'root:123456' |chpasswd
---> Using cache
---> 0397ce2fbd0a
Step 4 : RUN useradd runoob
......
设置镜像标签
runoob@runoob:~$ docker tag 860c279d2fec runoob/centos:dev
使用 docker images
命令可以看到,ID为 860c279d2fec 的镜像多一个标签。
runoob@runoob:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
runoob/centos 6.7 860c279d2fec 5 hours ago 190.6 MB
runoob/centos dev 860c279d2fec 5 hours ago 190.6 MB
runoob/ubuntu v2 70bf1840fd7c 22 hours ago 158.5 MB
ubuntu 14.04 90d5884b1ee0 6 days ago 188 MB
php 5.6 f40e9e0f10c8 10 days ago 444.8 MB
nginx latest 6f8d099c3adc 13 days ago 182.7 MB
mysql 5.6 f2e8d6c772c0 3 weeks ago 324.6 MB
httpd latest 02ef73cf1bc0 3 weeks ago 194.4 MB
ubuntu 15.10 4e3b13c8a266 5 weeks ago 136.3 MB
hello-world latest 690ed74de00f 6 months ago 960 B
centos 6.7 d95b5ca17cc3 6 months ago 190.6 MB
training/webapp latest 6fae60ef3446 12 months ago 348.8 MB