useradd docker -g docker
su - docker
docker run hello-world 第一部分-常用命令
1、查看本地镜像
docker image ls
2、查看显示消息后退出的容器
docker container ls --all
3、docker container 帮助命令
docker container --help
4、查看docker版本和详细信息
docker version 查看docker版本
docker info 查看docker详细信息
5、执行docker镜像
docker run hello-world
6、列出docker containers(running,all,all in quiet mode)
[docker@localhost ~]$ docker container ls --help
Usage: docker container ls [OPTIONS]
Options:
-a, --all 显示所有容器 (默认不加任何选项只显示运行的容器)
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print containers using a Go template
(docker container ls -af name=h #打印容器名字包含h的容器列表)
-n, --last int Show n last created containers (includes all states) (default -1)
-l, --latest 显示最新创建的容器
-q, --quiet 仅仅显示容器ID
-s, --size 显示容器信息再加上容器文件总大小 报错信息及解决
1、 containerd: /usr/bin/containerd: error while loading shared libraries: libseccomp.so.2: cannot open shared object file: No such file or directory
解决办法:yum -y install libseccomp
2、Couldn't load target DOCKER-ISOLATION':No such file or directory#012#012Tryiptables -h' or 'iptables --help' for more information.
解决办法:yum -y install iptables
[root@localhost ~]# vi /etc/docker/daemon.json
{
"dns": ["119.29.29.29", "202.106.0.20"]
}
# 设置完成后要重启docker服务
[root@localhost ~]# systemctl restart docker
# 也要重新运行以下刚才的build
su - docker
cd pythonweb/
docker build -t python-web-test .
运行应用程序
docker run -p 4000:80 python-web-test
[docker@localhost pythonweb]$ docker run -p 4000:80 python-web-test
* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://0.0.0.0:80/ (Press CTRL+C to quit)
172.16.100.1 - - [27/Aug/2018 08:13:26] "GET / HTTP/1.1" 200 -
[docker@localhost ~]$ docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: 506554897
Password:
WARNING! Your password will be stored unencrypted in /home/docker/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
给镜像指定tag
docker tag python-web-test 506554897/python-web-test:part1
# python-web-test是上边创建的container
# 506554897: 是hub.docker.com注册的用户名
# python-web-test:是仓库名(repository)
# part1 :是tag
# 查看标记完image的docker image ls
[docker@localhost ~]$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
506554897/python-web-test part1 075bace69a76 About an hour ago 132MB
python-web-test latest 075bace69a76 About an hour ago 132MB
上传镜像到[hub.docker.com]
docker push 506554897/python-web-test:part1
[docker@localhost ~]$ docker push 506554897/python-web-test:part1
The push refers to repository [docker.io/506554897/python-web-test]
03db24dfec4f: Pushed
7b4c7a03b2a4: Pushed
a0857e348458: Pushed
1ea4f6a807ba: Mounted from library/python
fda4dc055a55: Mounted from library/python
e8fc09a140cf: Mounted from library/python
cdb3f9544e4c: Mounted from library/python
part1: digest: sha256:8a5c72b23b7b581adf37f396a0cd27796c15c816d82ce813fab415c96b516191 size: 1788
到官网就可以看到已经上传的镜像了:
现在就可以在别的docker机器上下载运行这个镜像了
docker run -p 4001:80 506554897/python-web-test:part1
docker run username/repository-name:tag 第三部分-服务 services
services实际上就是“生产中的container”。services运行一个image,它编码了image的运行方式、应该使用哪些端口,应该运行多少个容器副本,以便服务具有所需的容量。
扩展服务会改变运行该软件的容器实例的数量,为流程中的服务分配更多的计算资源。
在docker平台定义、运行和扩展服务非常容易,只需编写一个docker-compose.yml 文件。
创建第一个docker-compose.yml文件
[docker@localhost ~]$ vi docker-compose.yml
version: "3"
services:
web:
#replace username/repository:tag with your name and image details.
image: 506554897/python-web-test:part1
deploy:
#允许副本数为 5
replicas: 2
resources:
limits:
#每个cpu最多使用10%(在所有核中)
cpus: "0.1"
memory: 50M
#如果容器失败,立即重新启动容器
restart_policy:
condition: on-failure
#将主机的4000端口映射到 容器的80端口
ports:
- "4000:80"
# web通过负载平衡webnet网络共享端口80
networks:
- webnet
# 定义webnet具有默认设置的网络(这是一个负载平衡的覆盖网络)
networks:
webnet: 运行新的负载平衡应用程序
启动集群模式
docker swarm init
启动群集模式,并使当前机器成为群集管理器。
[docker@localhost ~]$ docker swarm init
Swarm initialized: current node (htyhmotwa88rb2it9dkfh9c9o) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-4dc36dlf6g6ykl8k1q6hhhegi4pjmv390qlpgyj1cydf7koo7u-8n54mzvaicqtrhafmc8od3h6b 172.16.100.20:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
注意: 使用集群的时候,如果不运行该命令,会报错“this node is not a swarm manager ”
[docker@localhost ~]$ docker stack deploy -c docker-compose.yml python-test-web
Creating network python-test-web_webnet
Creating service python-test-web_web
# pyton-test-web:应用程序的名字,自定义就行。 获取应用程序ID
docker service ls
[docker@localhost ~]$ docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
zzevktay14av python-test-web_web replicated 5/5 506554897/python-web-test:part1 *:4000->80/tcp 查看应用程序里边的task
在服务中运行的单个容器成为task。任务被赋予唯一的ID,该ID在数字上递增,列出服务的task:
docker service ps python-test-web_web
[docker@localhost ~]$ docker service ps python-test-web_web
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
skvezw77ra63 python-test-web_web.1 506554897/python-web-test:part1 localhost.localdomain Running Running 10 minutes ago
vlxrofm9mbp1 python-test-web_web.2 506554897/python-web-test:part1 localhost.localdomain Running Running 10 minutes ago
ar7tr2213d92 python-test-web_web.3 506554897/python-web-test:part1 localhost.localdomain Running Running 10 minutes ago
lolilmuenas4 python-test-web_web.4 506554897/python-web-test:part1 localhost.localdomain Running Running 10 minutes ago
h9a1wrhkee1w python-test-web_web.5 506554897/python-web-test:part1 localhost.localdomain Running Running 10 minutes ago 查看系统运行的container
查看系统运行的container也可以列出应用程序的所有task。
[docker@localhost ~]$ docker container ls -q
b8d43f336d17
a4734d25f376
7ac3d4ecd07e
7946461cd7c1
95a9f0c09dfc web访问4000端口
应用程序对于每个客户端的每个请求,将以循环方式选择5个任务中的一个来响应。容器ID与前面命令的输出相匹配(docker container ls -q)
[root@docker-1 ~]# docker swarm init
Swarm initialized: current node (hzo1e9ux1qounpcdujyz10pks) is now a manager.
To add a worker to this swarm, run the following command:
# 增加主机到集群,请执行如下命令
docker swarm join --token SWMTKN-1-2yn0609winru1y5tsk00y9t54nh40thsvb7rjymfih5oz4ymy3-bgssyx8nlm0eiy0y4j8ugvsdh 172.16.100.20:2377
To add a manager to this swarm,
# 增加一个群集管理请运行如下:
run 'docker swarm join-token manager' and follow the instructions.
iptables开启swarm management port:
iptables -A INPUT -p tcp --dport 2377 -j ACCEPT
service iptables save
systemctl restart iptables
###2、增加主机到集群 swarm clusters
docker swarm join --token SWMTKN-1-2yn0609winru1y5tsk00y9t54nh40thsvb7rjymfih5oz4ymy3-bgssyx8nlm0eiy0y4j8ugvsdh 172.16.100.20:2377
[docker@docker-2 ~]$ docker swarm join --token SWMTKN-1-2yn0609winru1y5tsk00y9t54nh40thsvb7rjymfih5oz4ymy3-bgssyx8nlm0eiy0y4j8ugvsdh 172.16.100.20:2377
This node joined a swarm as a worker. 3、在管理节点上查看集群节点
docker node ls
[docker@docker-1 ~]$ docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
hzo1e9ux1qounpcdujyz10pks * docker-1 Ready Active Leader 18.09.0-ce-tp3
jr99plvxg9o8as269vq5o3gql docker-2 Ready Active 18.06.1-ce
# 当然在非管理节点查看会提示:This node is not a swarm manager.
删除节点命令
docker node rm 节点ID
节点主机删除集群
docker swarm leave
(此方法不推荐,这个方法只是在管理显示节点是down的状态,并不能直接删除列表,还得采用上一个办法彻底删除。)
强制删除集群
docker swarm leave --force
[docker@docker-1 ~]$ docker stack deploy -c docker-compose-redis.yml python-test-redis
Creating network python-test-redis_webnet
Creating service python-test-redis_visualizer
Creating service python-test-redis_redis
Creating service python-test-redis_webdocker stack deploy -c docker-compose-redis.yml python-test-redis 4、验证三个服务
docker service ls #查看是否运行有三个服务python-test-redis_redis visualizer web
docker stack ps python-test-redis #查看应用程序部署的情况
当然web界面的 可视化也可以验证:
当不断访问管理节点主页,计数器也在不断变化: