docker-compose连接到其他容器失败 [英] docker-compose connecting to other container fails

查看:122
本文介绍了docker-compose连接到其他容器失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个docker映像,一个是REST服务器( flaskapp ),另一个是Web服务器( web ).我正在尝试使用 docker-compose 运行它们.

I have two docker images, one rest server (flaskapp) and another web server (web). I am trying to run them using docker-compose.

但是,网络容器似乎无法联系休息容器

以下是我的 docker-compose.yml 文件:

version: '3'
services:
  flaskapp:
    build: ./rest_server
    restart: always
    networks:
      - docker_network
    expose:
      - 5001

  web:
    build: ./web
    restart: always
    ports:
      - 3002:3000
    depends_on:
      - flaskapp
    networks:
      - docker_network
    healthcheck:
      test: ["CMD", "curl", "-f", "http://flaskapp:5001/todos"]
networks:
  docker_network:
    driver: bridge

我的Web应用程序引用以下URL:

My web application refers to the following URL:

http://flaskapp:5001/todos

但是,如果我使用 docker exec -it< id>登录到Docker容器,/bin/bash 并运行以下命令,我得到了我期望的json响应.

However, If I log to docker container using docker exec -it <id> /bin/bash and run the following command I get the json response I expect.

卷曲 http://flaskapp:5001/todos

我也可以公开我的其余服务器端口,然后将其余服务器地址更改为Web服务器中的localhost,这将解决问题,但是这不是我想要的.

I can expose my rest server port as well, and then change the rest server address to localhost in web server and this will resolve the issue, however this is not what I would like.

我不想将我的其余服务器暴露给主机.

I don't want to expose my rest server to host machine.

推荐答案

您需要:

  1. 为您的容器定义一个通用网络
  2. 暴露您的 flaskapp 容器的端口 5001
  1. define a common network for your containers
  2. expose the port 5001 of your flaskapp container

实际上,根据文档EXPOSE 指令公开端口而不将其发布到主机上-只有链接的服务才能访问它们".因此,它允许公开"端口的容器与同一网络中的其他容器之间进行通信.

Indeed, according to the documentation, the EXPOSE instruction "exposes ports without publishing them to the host machine - they’ll only be accessible to linked services". So it allows communication between the container which "expose" the port, and other containers in the same network.

尝试类似的事情:

version: '3'
services:
    flaskapp:
        build: ./rest_server
        expose:
            - 5001
        networks:
            - docker_network

    web:
        build: ./web
        restart: always
        ports:
            - 3002:3000
        networks:
            - docker_network
        depends_on:
            - flaskapp

networks:
  docker_network:
    driver: bridge

这篇关于docker-compose连接到其他容器失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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