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 提供了大量的镜像,但是由于企业环境的多样性,并不是每个应用都能在 ...
随机推荐
- uvalive 5731 Qin Shi Huang’s National Road System
题意: 秦始皇要修路使得所有的城市连起来,并且花费最少:有一个人,叫徐福,他可以修一条魔法路,不花费任何的钱与劳动力. 秦始皇想让修路的费用最少,但是徐福想要受益的人最多,所以他们经过协商,决定让 A ...
- 【2017-03-20】HTML基础知识,标记,表格,表格嵌套及布局,超链接
一.HTML 网站(站点),网页基础知识 HTML是一门编程语言的名字:超文本标记语言 可以理解为:超越了文本的范畴,可以有图片.视频.音频.动画特效等其他内容,用标记的方法进行编程的计算机语言 基 ...
- 【CDH学习之二】ClouderaManager安装
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 zookeeper-3.4.11 搭建方案: serve ...
- 大数据处理框架之Strom: Storm拓扑的并行机制和通信机制
一.并行机制 Storm的并行度 ,通过提高并行度可以提高storm程序的计算能力. 1.组件关系:Supervisor node物理节点,可以运行1到多个worker,不能超过supervisor. ...
- 【javascript】获取 格式化时间
function getDate() { var myDate = new Date(); var month = myDate.getMonth() + 1; var day = myDate.ge ...
- Vue.js是什么,vue介绍
Vue.js是什么,vue介绍 Vue.js 是什么Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架.与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用. ...
- webstorm实用快捷键
webstorm实用快捷键 Ctrl+/ 或 Ctrl+Shift+/ 注释(// 或者/*…*/ ) Shift+F6 重构-重命名 Ctrl+X 删除行 Ctrl+D 复制行 Ctrl+G 查找行 ...
- 使用WebClient下载网页,用正则匹配需要的内容
WebClient是一个操作网页的类 webClient web=new WebClient(): web.DownloadString(网页的路径,可以是本地路径);--采用的本机默认的编码格式 ...
- 【shell脚本】通过遍历文件的一种批量执行shell命令的方法。
在分析数据时,经常会有许多机械重复的命令带入,作为一个半路出家的程序猿,我曾经对这种工作束手无策.不像一个熟手那样举重若轻的分析,感觉自己的生信分析完全是个体力活.为了打开这样的局面,我开始学习如何批 ...
- 每日linux命令学习-引用符号(反斜杠\,单引号'',双引号"")
引用符号在解析器中保护特殊元字符和参数扩展,其使用方法有3种:反斜杠(\),单引号(’‘),双引号(“”). 单引号和双引号必须匹配使用,均可在解析器中保护特殊元字符和通配符,但是单引号(硬转义)主要 ...