0

I am in a confusion right now. I try many things I can find on the web, but, none solved it. I have Win10 and Docker desktop installed using WSL 2 to host Linux containers. I use the following command to start the Jenkins website.

docker run --name jenkins-master-c -d -p 8080:8080 -p 50000:50000 -v jenkins_home:/var/jenkins_home jenkins/jenkins:2.282-alpine

This works fine. I can access the website using http://localhost:8080/

The problem is, I try to curl http://localhost:8080 from another alpine docker container, but, I am not getting the web page back, it said connection refused. I tried my own tiny web service on my Windows machine without docker. Same thing. I can access the web service using web browser on Windows 10. However, if I get inside a container, I couldn't access the web service on the localhost.

I know I am missing some thing really basic, because the web doesn't seem to have this topic. I am just on my own computer without anything fancy. Thus, I just want to use localhost. The web said the default is supposed to use bridge which the container should talk to each other easily, but, it is not working for me. What am I missing. Maybe I shouldn't type localhost? But, what else should I do?

thank you

Edit: just want to explain what I did to get my problem solved. The creating network --network my-network-name was what I originally did, which failed because the way I curl the webpage is wrong. I did --name jenkins-master-c only to make it easy locate my container on the docker ps. But, as pointed out in my question, I suspected the localhost is wrong, which is confirmed by the solution. Instead of using localhost, I do curl http://jenkins-master-c:8080 which worked. Thanks

BoBoDev
  • 847
  • 1
  • 8
  • 17

3 Answers3

1

You can imagine that docker containers are individual virtual machines, they have their own localhost. And they are isolated from your host pc and other containers.

Now if you want to communicate among two or more docker containers then you can use bridge network. In docker perspective, a bridge network allows containers connected to the same bridge network to communicate, while providing isolation from containers which are not connected to that bridge network. You can see the docker doc for bridge network.

On the other hand, if you want to communicate with your docker container from your host then you need to fort-forward for opening/exposing a port to connect with the container (which you did in -p 8080:8080)

Another way you can bring your containers under a local host is using kubernetes, in kuernetes you can run one or more containers in a pod and then they will share same network space. kubernetes pod

Sahadat Hossain
  • 3,583
  • 2
  • 12
  • 19
1

Probably these two containers are not in the same network, you they cannot see and talk to each other.

First of all, create a network by docker command docker network create SOMENAME, and then run containers again (both of them):

docker run --name jenkins-master-c --network SOMENAME -d -p 8080:8080 -p 50000:50000 -v jenkins_home:/var/jenkins_home jenkins/jenkins:2.282-alpine

Now it should talk to another docker container.

Saeed
  • 3,255
  • 4
  • 17
  • 36
1

localhost is always a question of perspective, it refers to the current machine. This means if you call localhost from a container it speaks to himself and not the machine you see as localhost. If you want to call a service running on this one you have to use its real IP address.

Tekki
  • 306
  • 1
  • 5
  • All the answers are helpful, thanks all. The link to duplication also helped. I am marking this as answer because this one helped me to solve my problem. The post confirmed I didn't use `localhost` correctly, which I focused on fixing that. Using the everyone's suggestions, I used the `docker network create SOMENAME` to setup the bridge, but, I use `curl http://jenkins-master-c:8080` instead of the `localhost` which finally break it out of the container without specifying an ip for it. – BoBoDev Mar 08 '21 at 02:31