简体   繁体   English

Docker 撰写服务通信

[英]Docker compose service communication

So I have a docker-compose file with 3 services: backend, react frontend and mongo.所以我有一个包含 3 个服务的 docker-compose 文件:后端、前端反应和 mongo。

backend Dockerfile:后端 Dockerfile:

FROM ubuntu:latest
WORKDIR /backend-server
COPY ./static/ ./static
COPY ./config.yml ./config.yml
COPY ./builds/backend-server_linux ./backend-server
EXPOSE 8080
CMD ["./backend-server"]

frontend Dockerfile:前端 Dockerfile:

FROM nginx:stable
WORKDIR /usr/share/nginx/html
COPY ./build .
COPY ./.env .env
EXPOSE 80
CMD  ["sh", "-c", "nginx -g \"daemon off;\""]

So nothing unusual, I guess.所以没什么不寻常的,我想。

docker-compose.yml: docker-compose.yml:

version: "3"

services:
    mongo-db:
        image: mongo:4.2.0-bionic
        container_name: mongo-db
        volumes:
            - mongo-data:/data
        network_mode: bridge

    backend:
        image: backend-linux:latest
        container_name: backend
        depends_on:
            - mongo-db
        environment:
            - DATABASE_URL=mongodb://mongo-db:27017
            ..etc
        network_mode: bridge
        # networks: 
        #     - mynetwork
        expose:
            - "8080"
        ports:
            - 8080:8080
        links:
            - mongo-db:mongo-db
        restart: always

    frontend:
        image: frontend-linux:latest
        container_name: frontend
        depends_on:
            - backend
        network_mode: bridge
        links:
            - backend:backend
        ports:
            - 80:80
        restart: always

volumes:
    mongo-data:
        driver: local

This is working.这是有效的。 My problem is that by adding ports: - 8080:8080 to the backend part, that server becomes available to the host machine.我的问题是,通过向后端添加ports: - 8080:8080 ,该服务器可用于主机。 Theoretically the network should work without these lines, as I read it in the docker docs and this question , but if I remove it, the API calls just stop working (but curl calls written in the docker-compose under the frontend service will still work).从理论上讲,网络应该在没有这些行的情况下工作,因为我在 docker docs 和这个问题中读到了它,但是如果我删除它,API 调用就会停止工作(但是在前端服务下的 docker-compose 中编写的 curl 调用仍然可以工作)。

Your react frontend is making requests from the browser.您的 React 前端正在从浏览器发出请求。 Hence the endpoint, in this case, your API needs to be accessible to the browser, not the container that is handing out static js, css and html files.因此,在这种情况下,端点需要浏览器可以访问您的 API,而不是分发静态 js、css 和 html 文件的容器。

Hope this image makes some sense.希望这张图有点意思。

在此处输入图片说明

PS If you wanted to specifically not expose the API you can get the Web Server to proxy Requests to /api/ to the API container, that will happen at the network level and mean you only need to expose the one server. PS 如果您想特别不公开 API,您可以让 Web 服务器将请求代理到 /api/ 到 API 容器,这将发生在网络级别,这意味着您只需要公开一台服务器。

I do this by serving my Angular apps out of Nginx and then proxy traffic for /app1/api/* to one container and /app2/api/* to another container etc为此,我将 Angular 应用程序从 Nginx 中提供出来,然后将 /app1/api/* 的流量代理到一个容器,将 /app2/api/* 代理到另一个容器等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM