从Docker容器的服务访问同级服务 [英] Access sibling service from docker container's service

查看:109
本文介绍了从Docker容器的服务访问同级服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行基于Node image的Docker容器(Windows的Docker快速入门终端)

I run Node image based docker container (Docker quickstart terminal from Windows)

FROM node:7.8.0
ENV NPM_CONFIG_LOGLEVEL warn

VOLUME /tmp
#copy server source /piu contains node server and /piu/client contains react+redux client
ADD piu /piu
ADD server_start.sh /

#clean windows \r char to make the .sh file real executable
RUN sed -i -e 's/\r$//' server_start.sh

CMD ./server_start.sh

EXPOSE 3000 3009

我启动Node客户端(在端口3000上)和Node(基于Express)的服务器(在3009端口上).客户端通过AJAX调用访问REST服务器.

I start Node client (on port 3000) and Node (express based) server (on 3009 port). Client accesses REST server via AJAX call.

componentDidMount() {
    const that = this;
    console.log('SERVER_URL=' + SERVER_URL); //the output is localhost:3009
    axios
        .get(SERVER_URL + '/posts')
        .then(res => {
            that.setState({pageInfo: res.data});
        })
        .catch(err => {
            console.log(err)
        })
}

它完全可以从主机运行(客户端访问localhost:3009并返回结果).我可以拨打:3009,然后再次得到正确的结果.

It perfectly works from host (client accesses localhost:3009 and return results). And I can call :3009 and again have correct results.

但是,当我构建并运行docker image时,它会失败.

But when I build and run docker image it fails.

docker run -p 3000-3009:3000-3009 --add-host="mongodb:192.168.12.151" MyDockerImage

--add-host用于访问主机上运行的mongo db.

--add-host is used to access mongo db running on host.

服务器端口3009暴露在外,所以我有一个有效的技巧可以调用

Server port 3009 is exposed so I have a working trick to call

192.168.99.100:3009 //the docker IP and exposed port

代替localhost:3009,但是最好让客户端直接在容器内部访问服务器.

instead of localhost:3009 but would be nice to let client access server directly inside the container.

如何在docker容器内正确指定localhost来访问兄弟服务?

How to specify localhost properly inside the docker container to access sibling service?

更新

#!/bin/bash

# Start the first process (server)
npm run start_docker --prefix piu &
status=$?
if [ $status -ne 0 ]; then
  echo "Failed to start my_first_process: $status"
  exit $status
fi

# Start the second process (client)
npm run start --prefix piu/client
status=$?
if [ $status -ne 0 ]; then
  echo "Failed to start my_second_process: $status"
  exit $status
fi

推荐答案

在这里您可以做的事很少

There are few things you can do here

让其他容器在其他容器的网络上运行

docker run --net container:<id> MyDockerImage

现在您的mongodb将可在localhost上访问.但是端口需要暴露在使用网络的容器中

Now your mongodb will be accessible on localhost. But the port needs to be exposed in the container whose network is used

自己创建网络并使用它

docker network create myapps

docker run --name mongodb_service --net myapps mongodb
docker run -p 3000-3009:3000-3009 --net myapps MyDockerImage

现在,在MyDockerImage中,可以在mongodb_service上访问mongodb

Now inside your MyDockerImage, mongodb can be reached at mongodb_service

使用docker compose

您可以使用docker-compose将它们作为合成运行

You can use docker-compose to run both of them as a composition

version: '3'
services:
  mongo: 
    image: mongodb
  app:
    build:
      context: .
    ports:
      - "3000-3009:3000-3009"

现在在应用程序mongodb中将可以使用mongo名称

And now in app mongodb will be reachable with name mongo

这篇关于从Docker容器的服务访问同级服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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