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

查看:32
本文介绍了如何使用 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?

如果您选择此路径,请确保您的容器连接到同一网络.您可以在此处找到有关此案例的信息:Compose 中的网络:使用预先存在的网络.

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天全站免登陆