Access service running in docker container from in

2020-04-11 09:59发布

At the moment I'm running a node.js application inside a docker container which needs to connect to camunda, which runs in another container.

I start the containers with the following command

docker run -d --restart=always --name camunda -p 8000:8080 camunda/camunda-bpm-platform:tomcat-7.4.0
docker run -d --name app -p 3000:3000 app

Both applications are now running and I can access camunda by navigating to my host's IP on port 8000, and running wget http://localhost:8000 -q -O - also returns the camunda page. When I login to my app container with docker exec -it app sh and type wget http://localhost:8000 -q -O -, I cannot access camunda. Instead I get the following error:

 wget: can't connect to remote host (127.0.0.1): Connection refused

When I link my app container to the camunda container with --link camunda:camunda, and type wget http://camunda:8000 -q -O - in my app container, I get the following error:

wget: can't connect to remote host (172.17.0.4): Connection refused`

I've seen this option, so I started my app container with --add-host camunda:my_hosts_ip and tried wget again, resulting in:

 wget: can't connect to remote host (149.210.227.191): Operation timed out

When running wget http://149.210.227.191:5001 -q -O - on my host machine however, I get a correct response immediately.

Ideally I would like to just start my app container without the need to supply the external IP in any way, and let the app container just use the camunda service via the localhost or by linking the camunda container tot my app container. What would be the easiest way to achieve this?

2条回答
叛逆
2楼-- · 2020-04-11 10:36

Original answer below. Note that link has been deprecated and the recommended replacement is network. That is explained in the answer to this question: docker-compose: difference between network and link

-- Use the --link camunda:camunda option for your app container. Then you can access camunda via http://camunda:8080/.... The link option adds a entry to the /etc/hosts file of the app container with the IP address of the camunda container. This also means you have to restart your app container if you restart the camunda container.

查看更多
做个烂人
3楼-- · 2020-04-11 10:41

Why does it not work?

Containers and host do not share their local IP stack. Thus, when you are within a container and try anything localhost:port the anything command will try to connect to the container-specific local IP stack, not the other container nor the host.

How to make it work?

Hard way: you either need to know the IP address of the other container and connect to this IP address..

Easier and cleaner way: .. either link your containers.

  --link=[]
      Add link to another container in the form of <name or id>:alias or just <name or id> in which case the alias will match the name

So you'll need to perform, assuming the camunda container is named camunda:

docker run -d --name app -p 3000:3000 --link camunda app

Then, once you docker-exec-ed into the container app you will be able to execute wget http://camunda:8080 -q -O - without error.


Note that while the linked containers graph cannot loop, e.g., camunda cannot be linked to app as you need to start a container to be able to link it, you actually do whatever you want/need playing with IP addresses. Note also that you can specify the IP address of a container using the --ip option (though it can only be used in conjunction with --net for user-defined networks).

查看更多
登录 后发表回答