How To Access a Service on Host From a Docker Container

+4 votes

I am new to Docker, I want to run a service running in my host machine from a Docker Container using docker-compose setup file. I tried configuring docker-compose.yml file: 

version: '3' 

services: 

nginx: 

    container_name: cont 

    image: ubuntu 

    command: sleep 10000000 

    networks: 

        - host 

networks: 

    host: 

But was unable to do that. What changes am I supposed to do in the docker-compose.yml file.

Mar 27, 2018 in Docker by shubham
• 7,340 points
34,148 views
What was the output of this? Any errors?

4 answers to this question.

+1 vote

According to docker-compose documentation you should use network_mode: "host" statement.

Just add the below lines to your docker-compose.yml file:

version: '3' 
services: 
  nginx: 
    container_name: cont 
    image: ubuntu 
    command: sleep 10000000 
    network_mode: "host"

Hope this helps!

answered Mar 27, 2018 by Atul
• 10,240 points

edited Mar 27, 2018 by Atul
+1 vote

To access the service from inside the container you need the port that particular host is listening to as no matter where the service is running we need to access it through the host node. 

Get docker container's hostname

curl 172.17.45.2:8080

If you have a service running on some other port you can access it via 172.17.42.1:5432.

answered Oct 11, 2018 by lina
• 8,220 points
0 votes

This is one of the way for doing this. Have a fixed set of IPs for both host and the containers. Set up a docker network like this with a common IP address.

docker network create -d bridge --subnet 192.168.0.0/24 --gateway 192.168.0.1 dockernet

Now each container can connect to the host under the fixed IP 192.168.0.1.

You just need to make sure, that you connect all your containers to that “dockernet” network you just created. You can do that with the --net=dockernet option for docker run.

answered Oct 16, 2018 by Kalgi
• 52,350 points
+1 vote

Adding to kalgi's answer, You can also connect to the docker net using your docker-compose file. 

Add the following in your docker-compose file(you'll be connecting to a common IP address network called dockernet).

version: '2'
services:
    db:
        image: some/image
        networks:
            - dockernet
networks:
    dockernet:
        external: true
answered Oct 16, 2018 by lina
• 8,220 points

edited Oct 16, 2018 by lina
Can you please elaborate your explanation and probably give an example with a code snippet.

hey DeanWinchester. I hope this gives a better understanding, add the following in your docker-compose file(you'll be connecting to a common IP address network called dockernet).

version: '2'
services:
    db:
        image: some/image
        networks:
            - dockernet
networks:
    dockernet:
        external: true
@lina can you post that as an answer or edit your answer with this explanation and code snippet so that's clearer for others having the same doubt.
I've updated my answer Kalgi, Hope that's good enough.

Related Questions In Docker

+1 vote
2 answers

How do I copy a file from docker container to host?

Here is a way which you can ...READ MORE

answered Aug 28, 2018 in Docker by Damon Salvatore
• 5,980 points
28,583 views
0 votes
1 answer

How to run a docker command from inside the container?

You must have come across the /var/run/docker.sock file, ...READ MORE

answered Jun 28, 2018 in Docker by Sophie may
• 10,620 points
3,523 views
+1 vote
2 answers
+2 votes
1 answer
+2 votes
1 answer

Deploy Docker Containers from Docker Cloud

To solve this problem, I followed advice ...READ MORE

answered Sep 3, 2018 in AWS by Priyaj
• 58,020 points
2,720 views
0 votes
1 answer

Different file owner inside Docker container and in host machine

Here is what you can try. Since ...READ MORE

answered Jun 27, 2018 in Docker by Atul
• 10,240 points
1,942 views
0 votes
1 answer

Docker containers as Jenkins Slave build agents

When you encapsulate the stage inside a ...READ MORE

answered Jun 16, 2018 in Docker by Atul
• 10,240 points
2,435 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP