Docker 实战
作者:hackett
微信公众号:加班猿
一、筹备工具
云服务器
我用的是腾讯云的 Centos7.5 64 位不到一百块一年的云服务器,你也能够用虚拟机
二、登录服务器工具
MobaXterm
1、之所以用这个作为 ssh 客户端,次要起因就是基于 MobaXterm 弱小且丰盛的性能,并且还都是收费的
MobaXterm 次要性能:
- 反对各种连贯 SSH,X11,RDP,VNC,FTP,MOSH
- 反对 Unix 命令 (bash,ls,cat,sed,grep,awk,rsync,…)
- 连贯 SSH 终端后反对 SFTP 传输文件
- 各种丰盛的插件 (git/dig/aria2…)
- 可运行 Windows 或软件
下载地址:https://mobaxterm.mobatek.net/download-home-edition.html
2、登录服务器
三、Centos Docker 装置
1、应用官网装置脚本装置
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
2、应用国内 daocloud 一键装置命令
curl -sSL https://get.daocloud.io/docker | sh
3、查看是否装置胜利(docker -v)
[root@VM-0-11-centos ~]# docker -v
Docker version 20.10.0, build 7287ab3
[root@VM-0-11-centos ~]#
4、启动 docker
sudo systemctl start docker
5、通过 hello world 映像验证是否正确装置 Docker
[root@VM-0-11-centos ~]# sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:1a523af650137b8accdaed439c17d684df61ee4d74feac151b5b337bd29e7eec
Status: Downloaded newer image for hello-world:latest
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/
[root@VM-0-11-centos ~]#
四、Docker 实战开始
1、Docker Hello World
Docker 容许你在容器内运行应用程序,应用 docker run 命令来在容器内运行一个应用程序
[root@VM-0-11-centos ~]# docker run ubuntu:15.10 /bin/echo "Hello world"
Unable to find image 'ubuntu:15.10' locally
15.10: Pulling from library/ubuntu
7dcf5a444392: Pull complete
759aa75f3cee: Pull complete
3fa871dc8a2b: Pull complete
224c42ae46e7: Pull complete
Digest: sha256:02521a2d079595241c6793b2044f02eecf294034f31d6e235ac4b2b54ffc41f3
Status: Downloaded newer image for ubuntu:15.10
Hello world
[root@VM-0-11-centos ~]#
各个参数解析:
- docker: Docker 的二进制执行文件。
- run: 与后面的 docker 组合来运行一个容器。
- ubuntu:15.10 指定要运行的镜像,Docker 首先从本地主机上查找镜像是否存在,如果不存在,Docker 就会从镜像仓库 Docker Hub 下载公共镜像。
- /bin/echo “Hello world”: 在启动的容器里执行的命令
以上命令残缺的意思能够解释为:Docker 以 ubuntu15.10 镜像创立一个新容器,而后在容器里执行 bin/echo “Hello world”,而后输入后果。
2、交互式容器
[root@VM-0-11-centos ~]# docker run -i -t ubuntu:15.10 /bin/bash
root@752fdf5d7764:/# cat /proc/version
Linux version 3.10.0-1127.19.1.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) ) #1 SMP Tue Aug 25 17:23:54 UTC 2020
root@752fdf5d7764:/# exit
exit
[root@VM-0-11-centos ~]#
各个参数解析:
- -t: 在新容器内指定一个伪终端或终端。
- -i: 容许你对容器内的规范输出 (STDIN) 进行交互。
第二行咱们曾经进入了一个 Ubuntu15.10 的容器
第五行能够通过 exit 或 CTRL + D 来退出容器
3、后盾运行容器
1、后盾运行
[root@VM-0-11-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@VM-0-11-centos ~]# docker run -d ubuntu:15.10 /bin/sh -c "while true; do echo hello world; sleep 1; done"
292152f399f3c2fa1fbac80edaffe2721f6948eba8c0821c652508805b82f2b3
[root@VM-0-11-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
292152f399f3 ubuntu:15.10 "/bin/sh -c'while t…" About a minute ago Up About a minute blissful_lumiere
[root@VM-0-11-centos ~]#
输入详情介绍:
CONTAINER ID: 容器 ID。
IMAGE: 应用的镜像。
COMMAND: 启动容器时运行的命令。
CREATED: 容器的创立工夫。
STATUS: 容器状态。
状态有 7 种:
- created(已创立)
- restarting(重启中)
- running 或 Up(运行中)
- removing(迁徙中)
- paused(暂停)
- exited(进行)
- dead(死亡)
PORTS: 容器的端口信息和应用的连贯类型(tcpudp)。
NAMES: 主动调配的容器名称。
2、查看容器的规范输入以及进行容器
[root@VM-0-11-centos ~]# docker logs 292152f399f3
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
[root@VM-0-11-centos ~]# docker stop 292152f399f3
292152f399f3
[root@VM-0-11-centos ~]#
4、运行一个 web 利用
在 docker 容器中运行一个 Python Flask 利用来运行一个 web 利用。
[root@VM-0-11-centos ~]# docker pull training/webapp
Using default tag: latest
latest: Pulling from training/webapp
Image docker.io/training/webapp:latest uses outdated schema1 manifest format. Please upgrade to a schema2 image for better future compatibility. More information at https://docs.docker.com/registry/spec/deprecated-schema-v1/
e190868d63f8: Pull complete
909cd34c6fd7: Pull complete
0b9bfabab7c1: Pull complete
a3ed95caeb02: Pull complete
10bbbc0fc0ff: Downloading [===================>] 7.883MB/20.71MB
10bbbc0fc0ff: Downloading [==========================>] 11.08MB/20.71MB
fca59b508e9f: Downloading [====================>] 20.85MB/50.25MB
10bbbc0fc0ff: Downloading [==============================================>] 19.38MB/20.71MB
fca59b508e9f: Downloading [==========================>] 26.43MB/50.25MB
10bbbc0fc0ff: Pull complete
fca59b508e9f: Downloading [================================>] 33.03MB/50.25MB
fca59b508e9f: Downloading [==================================>] 35.06MB/50.25MB
fca59b508e9f: Pull complete
e7ae2541b15b: Pull complete
9dd97ef58ce9: Pull complete
a4c1b0cb7af7: Pull complete
Digest: sha256:06e9c1983bd6d5db5fba376ccd63bfa529e8d02f23d5079b8f74a616308fb11d
Status: Downloaded newer image for training/webapp:latest
docker.io/training/webapp:latest
[root@VM-0-11-centos ~]#
[root@VM-0-11-centos ~]# docker run -d -p 8080:5000 training/webapp python app.py
06fc6dc03d8ecf32f34fd7a687b6b3dbba1510e7f94e0c835aa2b210bffdf5eb
[root@VM-0-11-centos ~]#
[root@VM-0-11-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
06fc6dc03d8e training/webapp "python app.py" 3 seconds ago Up 3 seconds 0.0.0.0:8080->5000/tcp busy_brahmagupta
[root@VM-0-11-centos ~]#
docker run -d -P training/webapp python app.py 参数阐明:
- -d: 让容器在后盾运行。
- -P: 将容器外部应用的网络端口随机映射到咱们应用的主机上。
Docker 凋谢了 5000 端口(默认 Python Flask 端口)用 - p 指定映射到主机端口 8080 上。
这时候咱们须要查看云服务器是否凋谢 8080 端口,没有看到端口
[root@VM-0-11-centos ~]# firewall-cmd --list-ports // 查看端口列表
You're performing an operation over default zone ('public'),
but your connections/interfaces are in zone 'docker' (see --get-active-zones)
You most likely need to use --zone=docker option.
[root@VM-0-11-centos ~]#
凋谢 8080 端口
[root@VM-0-11-centos ~]# firewall-cmd --add-port=8080/tcp --permanent // 凋谢 8081 端口
You're performing an operation over default zone ('public'),
but your connections/interfaces are in zone 'docker' (see --get-active-zones)
You most likely need to use --zone=docker option.
success
[root@VM-0-11-centos ~]# firewall-cmd --reload // 从新加载防火墙
success
[root@VM-0-11-centos ~]# firewall-cmd --list-ports// 查看端口列表
You're performing an operation over default zone ('public'),
but your connections/interfaces are in zone 'docker' (see --get-active-zones)
You most likely need to use --zone=docker option.
8080/tcp // 凋谢的端口
[root@VM-0-11-centos ~]#
这时咱们能够通过浏览器拜访 WEB 利用
5、Docker 镜像应用
1、列出镜像列表
[root@VM-0-11-centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest f643c72bc252 3 weeks ago 72.9MB
httpd latest 0a30f4c29d25 4 weeks ago 138MB
hello-world latest bf756fb1ae65 11 months ago 13.3kB
ubuntu 15.10 9b9cb95443b5 4 years ago 137MB
training/webapp latest 6fae60ef3446 5 years ago 349MB
[root@VM-0-11-centos ~]#
各个选项阐明:
- REPOSITORY: 示意镜像的仓库源
- TAG: 镜像的标签
- IMAGE ID: 镜像 ID
- CREATED: 镜像创立工夫
- SIZE: 镜像大小
2、获取镜像
docker pull + 镜像名字
3、查找镜像
docker search + 镜像名字
4、删除镜像
/***docker rmi + 镜像名字 ***/
[root@VM-0-11-centos ~]# docker rmi hello-world
Error response from daemon: conflict: unable to remove repository reference "hello-world" (must force) - container 75d905f3f65d is using its referenced image bf756fb1ae65
[root@VM-0-11-centos ~]#
删除镜像谬误须要先用 docker rm + 运行过的镜像 ID 进行删除后能力删除镜像
[root@VM-0-11-centos ~]# docker rm 75d905f3f65d
9e3a0d6881fc
[root@VM-0-11-centos ~]# docker rmi hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:1a523af650137b8accdaed439c17d684df61ee4d74feac151b5b337bd29e7eec
Deleted: sha256:bf756fb1ae65adf866bd8c456593cd24beb6a0a061dedf42b26a993176745f6b
Deleted: sha256:9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63
[root@VM-0-11-centos ~]#
6、Docker 容器连贯
1、容器命名
当咱们创立一个容器的时候,docker 会主动对它进行命名。另外,咱们也能够应用 –name 标识来命名容器
应用 docker ps 命令来查看容器名称
[root@VM-0-11-centos ~]# docker run -d -P --name hackett training/webapp python app.py
9ac60cb23bd0fb5cb9be39de648f17bc9702883cef1a42682105316c6b32d104
[root@VM-0-11-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9ac60cb23bd0 training/webapp "python app.py" 5 seconds ago Up 4 seconds 0.0.0.0:49154->5000/tcp hackett
ef0567701e23 training/webapp "python app.py" 21 minutes ago Up 21 minutes 0.0.0.0:8080->5000/tcp vigilant_cerf
[root@VM-0-11-centos ~]#
2、连贯容器
新建网络 test-net 网络
[root@VM-0-11-centos ~]# docker network create -d bridge test-ne
830af66ce29f8ab71f7d4e298a46d678a07ee8ba2abe50513d5d9000093e4990
[root@VM-0-11-centos ~]#
新建两个容器并连贯到 test-net 网络
[root@VM-0-11-centos ~]# docker run -itd --name test1 --network test-net ubuntu /bin/bash
4cb1423808b71139ae43afad21dd0333c7ac6c6524d77e6abec7bf758c2ab2b0
[root@VM-0-11-centos ~]# docker run -itd --name test2 --network test-net ubuntu /bin/bash
b11dbe9adb83985dac98f7d60aa9e8d4c0c86babbb2d22d7d52ee748fa85a2ed
[root@VM-0-11-centos ~]# docker ps
进入 test1 并测试 ping test2(须要先装置 ping 命令)
apt-get update
apt install iputils-ping
[root@VM-0-11-centos ~]# docker exec -it test1 /bin/bash
root@4cb1423808b7:/#
root@4cb1423808b7:/# ping test2
PING test2 (172.19.0.3) 56(84) bytes of data.
64 bytes from test2.test-net (172.19.0.3): icmp_seq=1 ttl=64 time=0.089 ms
64 bytes from test2.test-net (172.19.0.3): icmp_seq=2 ttl=64 time=0.071 ms
参考链接:https://www.runoob.com/docker/docker-tutorial.html
如果你感觉文章还不错,记得 ” 点赞关注 “
关注我的微信公众号【 加班猿 】能够获取更多内容