Docker containers opened a world of possibilities for the tech community, hassles in setting up new software were decreased unlike old times when a mess was to be sorted by a grievous format, it reduced the time to set up and use new software which eventually played a big part for techies to learn new things, roll it out in a container and scrap it when done. Things became easy, and the best thing its open source anyone and everyone can use it, comes with a little learning curve though.
Docker容器为技术社区打开了无限的可能,与以往不同的是,使用混乱的格式对混乱进行分类时,减少了安装新软件的麻烦,减少了安装和使用新软件的时间,最终使对于技术人员来说,学习新事物的重要部分是将其推出容器中并在完成后报废。 事情变得容易了,最好的东西是所有人都可以使用的开放源代码,但是学习曲线却很少。
Out of the myriad possibilities was the possibility of implementing complex technology stacks for our applications, which previously would have been the domain of experts. Today with the help of containers software engineers with sound understanding of the underlying systems can implement a complex stack and why not it’s the need of the hour, the figure of speech “Jack of all trades” got a fancy upgrade; “Master of some” based on the needs of the age. Simply put “T” shaped skills.
在无数种可能性中,有可能为我们的应用程序实现复杂的技术堆栈,而以前这些技术堆栈本来是专家的领域。 如今,在容器软件工程师的帮助下,他们对底层系统有了深刻的理解,可以实现一个复杂的堆栈,以及为什么不需要那么多的时间,因此,“万事通”的口号得到了很好的升级; 根据年龄的需要“掌握某些”。 简单地讲“ T”形技能。
The possibility of defining a complex stack in a file and running it with a single command, pretty tempting huh. The guys at Docker Inc. choose to call it Docker compose.
可以在文件中定义一个复杂的堆栈并用一个命令运行它,这很吸引人。 Docker Inc.的家伙选择将其称为Docker compose。
In this article, we will use Docker’s example Voting App and deploy it using Docker compose.
在本文中,我们将使用Docker的示例投票应用程序,并使用Docker compose进行部署。
In the words of Docker Inc.
用Docker Inc.的话来说。
Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.
Compose是用于定义和运行多容器Docker应用程序的工具。 通过Compose,您可以使用YAML文件来配置应用程序的服务。 然后,使用一个命令,就可以从配置中创建并启动所有服务。
Introducing the most favourite demonstration app for the Docker community “The Voting App”, as if it needs an introduction at all. This is a simple application based on micro-services architecture, consisting of 5 simple services.
向Docker社区介绍最喜欢的演示应用程序“ The Voting App”,好像它完全需要介绍一样。 这是一个基于微服务架构的简单应用程序,由5个简单服务组成。
Voting-App: Frontend of the application written in Python, used by users to cast their votes.
Voting-App :用Python编写的应用程序的前端,供用户用来投票。
Redis: In-memory database, used as intermediate storage.
Redis :内存数据库,用作中间存储。
Worker: .Net service, used to fetch votes from Redis and store in Postres database.
工作者 :.Net服务,用于从Redis获取投票并将其存储在Postres数据库中。
DB: PostgreSQL database, used as database.
DB :PostgreSQL数据库,用作数据库。
Result-App: Frontend of the application written in Node.js, displays the voting results.
Result-App :用Node.js编写的应用程序的前端,显示投票结果。
The Voting repo has a file called docker-compose.yml
this file contains the configuration for creating the containers, exposing ports, binding volumes and connecting containers through networks required for the voting app to work. Sounds like a lot of pretty long docker run
and docker network create
commands otherwise, docker compose allows us to put all of that stuff in a single docker-compose file in yaml format.
投票回购有一个名为docker-compose.yml
文件,该文件包含用于创建容器,暴露端口,绑定卷以及通过投票应用程序运行所需的网络连接容器的配置。 听起来很多docker run
时间很长,而docker network create
命令,否则docker compose允许我们将所有内容以yaml格式放在单个docker-compose文件中。
Git clone
and cd
into the voting app repo.
Git clone
并cd
进入投票应用程序仓库。
dockersamples/example-voting-appexample-voting-app - Example Docker Compose appgithub.com
dockersamples / example-voting-app example-voting- app- 示例Docker Compose应用程序 github.com
With all of our application defined in a single compose file we can take a sigh of relief, chill and simply run the application. The beauty of compose lies in the fact that a single command creates all the services, wires up the networks(literally), mounts all volumes and exposes the ports. Its time to welcome the up
command, its performs all of the aforementioned tasks.$ docker-compose up
将我们所有的应用程序定义在一个撰写文件中,我们可以松一口气,放松一下,然后简单地运行该应用程序。 组合的好处在于,一个命令可以创建所有服务,(从字面上看)连接网络,安装所有卷并公开端口。 是时候欢迎up
命令了,它执行所有上述任务。$ docker-compose up
After lots of “Pull complete”, hundreds of megabytes and few minutes (maybe more). . .
经过大量的“拉动”之后,需要数百兆字节和几分钟(可能更多)。 。 。
Voila, we have the voting app up and running.
瞧,我们已启动并运行了投票应用程序。
Command docker ps
lists all the running containers
命令docker ps
列出所有正在运行的容器
$ docker ps -a --format="table {{.Names}}\t{{.Image}}\t{{.Ports}}"
NAMES IMAGE PORTS
voting_worker_1 voting_worker
db postgres:9.4 5432/tcp
voting_vote_1 voting_vote 0.0.0.0:5000->80/tcp
voting_result_1 voting_result 0.0.0.0:5858->5858/tcp, 0.0.0.0:5001->80/tcp
redis redis:alpine 0.0.0.0:32768->6379/tcp
The above command displays all the running containers, respective images and the exposed port numbers.
上面的命令显示所有正在运行的容器,相应的映像和公开的端口号。
The Voting app can be accessed on http://localhost:5000
可以在http:// localhost:5000上访问投票应用程序
Likewise the Voting results app can be accessed on http://localhost:5001
同样,可以在http:// localhost:5001上访问投票结果应用程序
Each vote cast on the Voting app is first stored in the Redis in-memory database, the .Net worker service fetches the vote and stores it in the Postgres DB which is accessed by the Node.js frontend.
投票应用程序上进行的每个投票都首先存储在Redis内存数据库中,.Net辅助服务获取该投票并将其存储在可由Node.js前端访问的Postgres DB中。
Compose provide the flexibility to use a project name to isolate the environments from each other, the project name is the base name of the directory that contains the project. In our voting app this is signified by the name of the containers voting_worker_1
where voting
is the base name of the directory. We can set a custom project name using the -p
flag followed by the custom name.
Compose提供了使用项目名称将环境彼此隔离的灵活性,项目名称是包含项目的目录的基本名称。 在我们的投票应用程序中,这是由容器的名称voting_worker_1
表示的,其中voting
是目录的基本名称。 我们可以使用-p
标志和自定义名称来设置自定义项目名称。
Compose preserves all volumes used by the services defined in the compose file, thus no data is lost when the containers are recreated using docker-compose up
. Another cool feature is that only the containers which have changed are recreated, the containers whose state did not change remain untouched.
Compose保留了compose文件中定义的服务使用的所有卷,因此,使用docker-compose up
重新创建容器时,不会丢失任何数据。 另一个很酷的功能是,仅重新创建已更改的容器,状态未更改的容器保持不变。
Another cool feature is the support for variables in the compose file, we can define variables in a .env
file and use them in the docker-compose file. Here the POSTGRES_VERSION=9.4
can defined in the environment file or can be defined in the shell. It is used in the compose file in the following manner:
另一个很酷的功能是对compose文件中变量的支持,我们可以在.env
文件中定义变量,并在.env
-compose文件中使用它们。 在这里POSTGRES_VERSION=9.4
可以在环境文件中定义,也可以在外壳中定义。 通过以下方式在撰写文件中使用它:
db:
image: "postgres:${POSTGRES_VERSION}"
It's easy as breeze to start, stop and play around with compose.
轻而易举地开始,停止和在compose周围玩耍很容易。
$ docker-compose up -d
$ docker-compose down
$ docker-compose start
$ docker-compose stop
$ docker-compose build
$ docker-compose logs -f db
$ docker-compose scale db=4
$ docker-compose events
$ docker-compose exec db bash
Docker Compose is a great tool to quickly deploy and scrap containers, the compose file can run seamlessly on any machine installed with docker-compose. Experimentation and learning technologies is just a Compose file away ;).
Docker Compose是用于快速部署和废弃容器的出色工具,该compose文件可以在任何安装了docker-compose的计算机上无缝运行。 实验和学习技术只是一个撰写文件而已;)。
I hope this article helped in the understanding of Docker Compose. I’d love to hear about how you use Docker Compose in your projects. Share the knowledge, help it reach more people.
我希望本文有助于理解Docker Compose。 我很想听听您如何在项目中使用Docker Compose。 分享知识,帮助它吸引更多人。
翻译自: https://www.freecodecamp.org/news/a-practical-introduction-to-docker-compose/