如何使用Docker Compose实现微服务架构? [英] How to implement microservices architecture with docker compose?

查看:75
本文介绍了如何使用Docker Compose实现微服务架构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Web应用程序,该应用程序需要少量的微服务,例如URL缩短,邮件程序,分析等.这些服务中的每一个都以REST api的形式实现,因此它们可以彼此通信.这些服务中的每一个都有一个 docker-compose.yml 文件,并使用nginx,php,mysql等容器……我希望它们可以作为单个系统工作.我不想将这些REST API公开给docker网络之外的任何人

I am working on a web application that requires few microservices like a URL shorter, mailer, analytics, etc... Each of these services are implemented as REST api so they can talk to each other. Each of these services have a docker-compose.yml file and use containers like nginx, php, mysql, etc… I want these to work as a single system. I don’t want to expose these REST apis to anyone outside of the docker network

我的问题是如何合并每个服务中的多个 docker-compose.yml 文件,以便它们可以协同工作?还是应该使用一个大的 docker-compose.yml 文件来定义一个网络下的所有服务?还是我应该将每个服务仅基于一个容器(以ubuntu为基础,并通过Dockerfile安装php,mysql,nginx),然后在共享网络上通过 docker-compose.yml 运行它们?还是我误解了用Docker实现微服务?

My question is how can I combine multiple docker-compose.yml files from each service so they work together? Or should I use one big docker-compose.yml file to define all the services under one network? Or should I base each of my service only on one container (ubuntu as base and install php, mysql, nginx through Dockerfile) then run them through docker-compose.yml, on a shared network? Or have I misunderstood implementing microservices with Docker?

推荐答案

如何合并每个服务中的多个docker-compose.yml文件,以便它们可以协同工作?

how can I combine multiple docker-compose.yml files from each service so they work together?

如果选择此路径,请确保您的容器连接到相同的网络.您可以在以下位置找到有关这种情况的信息:撰写"中的网络:使用预先存在的网络.

If you choose this path, make sure that your containers attach to the same network. You can find information about this case here: Networking in Compose: Use a pre-existing network.

还是应该使用一个大的docker-compose.yml文件来定义一个网络下的所有服务?

Or should I use one big docker-compose.yml file to define all the services under one network?

还是由您决定是否应该采用这种方法.在这种情况下,您是正确的,所有容器都可以连接到将要创建的默认网络.来自同一文档:

It is again up to you to decide if this should be your approach. In this case, you are correct, all the containers can attach to the default network that is going to be created. From the same docs:

注意:为应用程序的网络提供一个基于项目名称"的名称,该名称基于其所在目录的名称.您可以使用--project-name标志或COMPOSE_PROJECT_NAME环境变量.

Note: Your app’s network is given a name based on the "project name", which is based on the name of the directory it lives in. You can override the project name with either the --project-name flag or the COMPOSE_PROJECT_NAME environment variable.

或者我应该只将每个服务基于一个容器(以ubuntu为基础,并通过Dockerfile安装php,mysql,nginx)

Or should I base each of my service only on one container (ubuntu as base and install php, mysql, nginx through Dockerfile)

我不太确定,您的意思到底是什么...

I am not quite sure, what you mean here exactly...

  • 如果您要谈论将所有这些都安装到一个容器中,请不要这样做.这样,您就可以回到整体"方法.
  • 如果您谈论以"ubuntu"为基础并通过为php,mysql,nginx等构建单独的容器来继续,则可以做到.但我建议您更好地使用 https://hub.docker.com
  • if you talk about having all these installed to one container, don't do it. This way you get back to a "monolith" approach.
  • if you talk about starting with "ubuntu" as a base and continue by building separate containers for php, mysql, nginx etc, you can do it. But I would suggest you to better use the official images provided by https://hub.docker.com

这篇关于如何使用Docker Compose实现微服务架构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆