Docker:Stacks
Prerequisites
- Install Docker version 1.13 or higher.
- Get Docker Compose as described in Part 3 prerequisites.(ymal)
- Get Docker Machine as described in Part 4 prerequisites.(swarm)
- Read the orientation in Part 1.
Learn how to create containers in Part 2.
Make sure you have published the
friendlyhelloimage you created by pushing it to a registry. We use that shared image here.Be sure your image works as a deployed container. Run this command, slotting in your info for
username,repo, andtag:docker run -p 80:80 username/repo:tag, then visithttp://localhost/.Have a copy of your
docker-compose.ymlfrom Part 3 handy.Make sure that the machines you set up in part 4 are running and ready. Run
docker-machine lsto verify this. If the machines are stopped, rundocker-machine start myvm1to boot the manager, followed bydocker-machine start myvm2to boot the worker.- Have the swarm you created in part 4 running and ready. Run
docker-machine ssh myvm1 "docker node ls"to verify this. If the swarm is up, both nodes report areadystatus. If not, reinitialze the swarm and join the worker as described in Set up your swarm.
Introduction
In part 4, you learned how to set up a swarm, which is a cluster of machines running Docker, and deployed an application to it, with containers running in concert on multiple machines.
Here in part 5, you reach the top of the hierarchy of distributed applications: the stack.
A stack is a group of interrelated services that share dependencies, and can be orchestrated and scaled together.
A single stack is capable of defining and coordinating the functionality of an entire application (though very complex applications may want to use multiple stacks).
Some good news is, you have technically been working with stacks since part 3, when you created a Compose file and used docker stack deploy. But that was a single service stack running on a single host, which is not usually what takes place in production.
Here, you can take what you’ve learned, make multiple services relate to each other, and run them on multiple machines.
You’re doing great, this is the home stretch!
Add a new service and redeploy
It’s easy to add services to our docker-compose.yml file. First, let’s add a free visualizer service that lets us look at how our swarm is scheduling containers.
Open up
docker-compose.ymlin an editor and replace its contents with the following. Be sure to replaceusername/repo:tagwith your image details.
version: "3"
services:
web:
# replace username/repo:tag with your name and image details
image: username/repo:tag
deploy:
replicas: 5
restart_policy:
condition: on-failure
resources:
limits:
cpus: "0.1"
memory: 50M
ports:
- "80:80"
networks:
- webnet
visualizer:
image: dockersamples/visualizer:stable
ports:
- "8080:8080"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
deploy:
placement:
constraints: [node.role == manager]
networks:
- webnet
networks:
webnet:
The only thing new here is the peer service to web, named visualizer.
Notice two new things here:
- a
volumeskey, giving the visualizer access to the host’s socket file for Docker, - and a
placementkey, ensuring that this service only ever runs on a swarm manager -- never a worker.
That’s because this container, built from an open source project created by Docker, displays Docker services running on a swarm in a diagram.
We talk more about placement constraints and volumes in a moment.
- Make sure your shell is configured to talk to
myvm1(swarm manger)(full examples are here).
- Run
docker-machine lsto list machines and make sure you are connected tomyvm1(swarm manger), as indicated by an asterisk next to it.
- Run
If needed, re-run
docker-machine env myvm1, then run the given command to configure the shell.
On Windows the command is:
& "C:\Program Files\Docker\Docker\Resources\bin\docker-machine.exe" env myvm1 | Invoke-Expression
- Re-run the
docker stack deploycommand on the manager, and whatever services need updating are updated:
$ docker stack deploy -c docker-compose.yml getstartedlab
Updating service getstartedlab_web (id: angi1bf5e4to03qu9f93trnxm)
Creating service getstartedlab_visualizer (id: l9mnwkeq2jiononb5ihz9u7a4)
- Take a look at the visualizer.
You saw in the Compose file that visualizer runs on port 8080.
Get the IP address of one of your nodes by running docker-machine ls.
Go to either IP address at port 8080 and you can see the visualizer running:

The single copy of visualizer is running on the manager as you expect, and the 5 instances of web are spread out across the swarm.
You can corroborate this visualization by running docker stack ps <stack>:
docker stack ps getstartedlab
The visualizer is a standalone service that can run in any app that includes it in the stack. It doesn’t depend on anything else.
Now let’s create a service that does have a dependency: the Redis service that provides a visitor counter.
Persist the data
Let’s go through the same workflow once more to add a Redis database for storing app data.
Save this new
docker-compose.ymlfile, which finally adds a Redis service. Be sure to replaceusername/repo:tagwith your image details.
version: "3"
services:
web:
# replace username/repo:tag with your name and image details
image: username/repo:tag
deploy:
replicas: 5
restart_policy:
condition: on-failure
resources:
limits:
cpus: "0.1"
memory: 50M
ports:
- "80:80"
networks:
- webnet
visualizer:
image: dockersamples/visualizer:stable
ports:
- "8080:8080"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
deploy:
placement:
constraints: [node.role == manager]
networks:
- webnet
redis:
image: redis
ports:
- "6379:6379"
volumes:
- "/home/docker/data:/data"
deploy:
placement:
constraints: [node.role == manager]
command: redis-server --appendonly yes
networks:
- webnet
networks:
webnet:
Redis has an official image in the Docker library and has been granted the short image name of just redis, so no username/repo notation here.
The Redis port, 6379, has been pre-configured by Redis to be exposed from the container to the host, and here in our Compose file we expose it from the host to the world, so you can actually enter the IP for any of your nodes into Redis Desktop Manager and manage this Redis instance, if you so choose.
Most importantly, there are a couple of things in the redis specification that make data persist between deployments of this stack:
redisalways runs on the manager, so it’s always using the same filesystem.redisaccesses an arbitrary directory in the host’s file system as/datainside the container, which is where Redis stores data.
Together, this is creating a “source of truth” in your host’s physical filesystem for the Redis data.
Without this, Redis would store its data in /data inside the container’s filesystem, which would get wiped out if that container were ever redeployed.
This source of truth has two components:
- The placement constraint you put on the Redis service, ensuring that it always uses the same host.
- The volume you created that lets the container access
./data(on the host) as/data(inside the Redis container). While containers come and go, the files stored on./dataon the specified host persists, enabling continuity.
You are ready to deploy your new Redis-using stack.
- Create a
./datadirectory on the manager:
docker-machine ssh myvm1 "mkdir ./data"
- Make sure your shell is configured to talk to
myvm1(full examples are here).
Run
docker-machine lsto list machines and make sure you are connected tomyvm1, as indicated by an asterisk next it.If needed, re-run
docker-machine env myvm1, then run the given command to configure the shell.
On Windows the command is:
& "C:\Program Files\Docker\Docker\Resources\bin\docker-machine.exe" env myvm1 | Invoke-Expression
- Run
docker stack deployone more time.
$ docker stack deploy -c docker-compose.yml getstartedlab
- Run
docker service lsto verify that the three services are running as expected.
$ docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
x7uij6xb4foj getstartedlab_redis replicated 1/1 redis:latest *:6379->6379/tcp
n5rvhm52ykq7 getstartedlab_visualizer replicated 1/1 dockersamples/visualizer:stable *:8080->8080/tcp
mifd433bti1d getstartedlab_web replicated 5/5 gordon/getstarted:latest *:80->80/tcp
- Check the web page at one of your nodes, such as
http://192.168.99.101, and take a look at the results of the visitor counter, which is now live and storing information on Redis.

Also, check the visualizer at port 8080 on either node’s IP address, and notice see the redis service running along with the web and visualizer services.

Recap (optional)
Here’s a terminal recording of what was covered on this page:
You learned that stacks are inter-related services all running in concert, and that -- surprise!
-- you’ve been using stacks since part three of this tutorial.
You learned that to add more services to your stack, you insert them in your Compose file.
Finally, you learned that by using a combination of placement constraints and volumes you can create a permanent home for persisting data, so that your app’s data survives when the container is torn down and redeployed.
Docker:Stacks的更多相关文章
- Docker 入门 第五部分:Stacks
目录 Docker 入门 第五部分:Stacks 先决条件 介绍 添加一个新的服务并重新部署 保存数据 回顾 Docker 入门 第五部分:Stacks 先决条件 安装 Docker 1.13 或更高 ...
- Docker入门(六):Stacks
这个<Docker入门系列>文档,是根据Docker官网(https://docs.docker.com)的帮助文档大致翻译而成.主要是作为个人学习记录.有错误的地方,Robin欢迎大家指 ...
- 【转】深入 Docker:容器和镜像
在本专栏往期的 Flux7 系列教程 里,我们已经简单地探讨了 Docker 的基本操作.而在那篇教程中,我们一直是简单地将容器当成是"正在运行的镜像",并没有深入地区分镜像和容器 ...
- 老司机实战Windows Server Docker:2 docker化现有iis应用的正确姿势
前言 上一篇老司机实战Windows Server Docker:1 初体验之各种填坑介绍了安装docker服务过程中的一些小坑.这一篇,我们来填一些稍大一些的坑:如何docker化一个现有的iis应 ...
- docker:(5)利用docker -v 和 Publish over SSH插件实现war包自动部署到docker
在 docker:(3)docker容器挂载宿主主机目录 中介绍了运行docker时的一个重要命令 -v sudo docker run -p : --name tomcat_xiao_volume ...
- docker:Dockerfile构建LNMP平台
docker:Dockerfile构建LNMP平台 1.dockerfile介绍 Dockerfile是Docker用来构建镜像的文本文件,包含自定义的指令和格式.可以通过docker buil ...
- Docker:使用Jenkins构建Docker镜像
Docker 彭东稳 1年前 (2016-12-27) 10709次浏览 已收录 0个评论 一.介绍Jenkins Jenkins是一个开源项目,提供了一种易于使用的持续集成系统,使开发者从 ...
- Docker:一个装应用的容器
一:简介:你是否经历过“我本地运行没问题啊!““哪个哥们有写死循环了““完了,服务器撑不住了“等等问题,docker就是这么帮你解决问题的工具,它可以帮你把web应用自动化打包和发布,在服务型环境下进 ...
- 操作系统-容器-Docker:如何将应用打包成为 Docker 镜像?
ylbtech-操作系统-容器-Docker:如何将应用打包成为 Docker 镜像? 1.返回顶部 1. 虽然 DockerHub 提供了大量的镜像,但是由于企业环境的多样性,并不是每个应用都能在 ...
随机推荐
- 【Linux学习十】负载均衡带来tomcat的session不一致问题
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 tomcat7 jdk7 session不一致是指web服务器(tom ...
- 写自动更新程序出现"远程服务器返回错误: (404) 未找到"
在win2003配置后,在客户端运行时能够下载exe和dll文件,但是在更新lib文件时总是报“远程服务器返回错误: (404) 未找到”错误,不明白咋会出现这个问题,去网上一查,发现以下解决办法: ...
- sqoop使用经验总结及问题汇总
问题导读1.导入数据到HDFS,需要注意什么?2.在测试sqoop语句的时候,如何限制记录数量?3.sqoop导入时什么情况下会多导入一条数据? 一.sqoop 导入数据到HDFS注意事项 分割符的方 ...
- js里用append()和appendChild有什么区别?
parentNode.append()是还在试用期的方法,有兼容问题.是在parendNode节点中最后一个子节点后插入新Node或者DOMString(字符串,插入后为Text节点). 与paren ...
- 人工智能深度学习框架MXNet实战:深度神经网络的交通标志识别训练
人工智能深度学习框架MXNet实战:深度神经网络的交通标志识别训练 MXNet 是一个轻量级.可移植.灵活的分布式深度学习框架,2017 年 1 月 23 日,该项目进入 Apache 基金会,成为 ...
- 移植并修改成功的模拟iic读写EEPROM at24c02
2010-04-24 12:58:00 注:如果要读24c128或264,地址位为16位的.现在的地址位为8位. protues仿真图 源程序如下: #include <iom16v.h> ...
- 大数据自学4-Hue集成环境中各模组说明
前面已经学习了如何将数据从关系型数据库导入到Hive/HDFS,并且在Windows客户端查询导入的数据,接下来继续学习CDH,知识点: 1.Hue环境中DB Query如何使用,DB Query这个 ...
- #mxnet# 权值共享
https://www.cnblogs.com/chenyliang/p/6847744.html Note:后记此权值共享非彼卷积共享.说的是layer实体间的参数共享. Introduction ...
- 基于 SSL 的 Nginx 反向代理
基于 SSL 的 Nginx 反向代理 描述: 线上zabbix因机房网络问题,外网接口无法对外访问,因此采用同机房的另外一台服务器做反向代理. 线上用于zabbix提供web访问的Nginx,采用h ...
- Python数据类型的显式转换
数据类型的显示转换,也称为数据类型的强制类型转换,是通过Python的内建函数来实现的类型转换. 显式转换的多种类型: int(x [,base]) ⇒ 将x转换为一个十进制的整数 long(x [, ...