How to Connect Docker Container to localhost [duplicate] - docker

This question already has answers here:
Exposing a port on a live Docker container
(16 answers)
Closed yesterday.
I've created a docker container with ubuntu image. In that I've created a react app and when I try to run the app I could not see the app is running on localhost but in the terminal of container it says its running on the port. How can we connect a docker container with our localhost.

If You have a docker file just pass the port to docker run with -p
3001:3000
If you have a docker compose set the port with:
ports:
- 3001:3000
and run docker-compose up -d
Finally navigate to localhost:{Port}

From official documentation : docker run -p 127.0.0.1:80:8080/tcp ubuntu bash
This binds port 8080 of the container to TCP port 80 on 127.0.0.1 of the host machine. You can also specify udp and sctp ports. The Docker User Guide explains in detail how to manipulate ports in Docker.
Then docker ps and verify its running and ports are exposed.
Also check about your firewall, it may block ports.

Related

Cannot access Docker container remotely

So I have a custom Docker image based on Debian (essentially just installs some extra packages and copies a few files to configure the web server). However, I have been unable to get access to the web server externally.
The command I use to run the image after being built is:
docker container run --publish 8080:80 --name ipcast -t -d ipcast
Of course, the first thing I assumed was that the port was not open in the Firewall, though I double checked the Firewall and it seems to be open through all hops to the Docker host.
Furthermore, I ran nmap -p 8080 ourdockerserver.com on my remote machine and it yielded:
PORT STATE SERVICE
8080/tcp closed http-proxy
Which implies that the port is open in the Firewall (since it's closed not filtered) just that nothing is listening on it.
Nevertheless, when I run netstat -tulpn on the Docker host, I can see it running on port 8080:
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 579620/docker-proxy
And running nmap -p 8080 localhost on the Docker host seems to work fine:
PORT STATE SERVICE
8080/tcp open http-proxy
I'm not particularly knowledgeable with IP Tables, however, I've never seen a port being used internally but closed externally without being filtered by a Firewall. I understand that this may be something to do with the port forwarding magic that Docker does on the host but I cannot get it to work even after scouring the internet.
I also tried creating a bridged network with docker network create -d bridge my-net and then running my container with:
docker container run --network my-net --publish 8080:80 --name ipcast -t -d ipcast
This did not work either...

How do I "point" container to port 8080?

I'm learning the ins and outs of Docker from book and I'm asked to:
"Open a web browser and navigate to the DNS name or IP address of the Docker host
that you're running the container from, and point it to port 8080."
I don't understand what I'm asked to do. I've got a container with image running on my machine
but I don't understand how do I get IP address of Docker host ? I can run docker-machine ip [instance] but I've got no instance running in the cloud and the container is up locally.
Can anyone explain to me what I'm asked to do ?
0c7d84a472ed test:latest "node ./app.js" 15 minutes ago Up 15 minutes 8080/tcp, 0.0.0.0:8080->80/tcp web1
You need to map the port when running the container by adding flags of -p <hostport>:<serviceport_inside_container>
Since the container is running locally in your machine (desktop/laptop), open the web browser to the url http://localhost:8080 or https://localhost:8080.
Container port 80 is mapped to 8080 of the node (which is the docker host - the host machine that is running the container. This docker host in your example is localhost itself.
Hence, http://localhost:8080 should work
Phrase "Point to" actually means to open the web page hosted in this context.
I don't understand how do I get the IP address of Docker host? You have use server IP and port for the specific app or service. find below example.
ex: docker run -d --name app1 -p 8080:8080 tomcat ( ServerIpaddress:8080)
docker run -d --name app2 -p 8081:8080 tomcat ( ServerIPaddress:8081)
Note: make sure you have open the port on the security group.

I cannot access proxy of a running docker container

I have a running Docker container which shows PORTS 9191/tcp. So on my browser, I tried accessing server using localhost:9191/api/.... However, browser throws an error This site can’t be reached
Here is a log to docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c214aefed15e shah "youtube-dl-server -…" 6 seconds ago Up 5 seconds 9191/tcp boring_swirles
This is what my docker file looks like
FROM mariozig/youtube-dl_server
RUN pip install --pre youtube_dl_server
EXPOSE 9191
ENTRYPOINT ["youtube-dl-server", "--host=0.0.0.0"]
You have not mapped the docker container port to host port.
The docker container runs on a host. And The host doesn't know which requests to be directed to the docker container. For that you have to to map the host port to docker container port using -p flag in docker run command as shown below:
docker run -d -p HOST_PORT:CONTAINER_PORT IMAGE_NAME
-p in this command will specify that you are forwarding your host port to the container port. In your local host in the port HOST_PORT will call the port CONTAINER_PORT of your container.
Now when you will access the HOST_IP:HOST_PORT then the host will redirect the request to corresponding container with which this HOST_PORT has been mapped.
For example I started a tomcat docker container and mapped the tomcat container's 8080 port to host's 9092 port by using the above command. When I do docker ps I can see the mapping under PORTS as 0.0.0.0:9092->8080/tcp

Docker port exposed to outside world

I've installed docker in a VM which is publicy available on internet. I've installed mongodb in a docker container in the VM.Mongodb is listening on 27017 port.
I've installed using the following steps
docker run -p 27017:27017 --name da-mongo -v ~/mongo-data:/data/db -d mongo
The port from container is redirected to the host using the -p flag. But the port 27017 is exposed on the internet. I don't want it to happen.
Is there any way to fix it?
Well, if you want it available for certain hosts then you need a firewall. But, if all you need is it working on localhost (your VM machine), then you don't need to expose/bind the port with the host. I suggest you to run the container without the -p option, then, run the following command:
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' your_container_id_or_name
After that, it will display an IP, it is the IP of the container you've just ran (Yes, docker uses somewhat an internal virtual network connecting your containers and your host machine between them).
After that, you can connect to it using the IP and port combination, something like:
172.17.0.2:27017
When you publish the port, you can select which host interface to publish on:
docker run -p 127.0.0.1:27017:27017 --name da-mongo \
-v ~/mongo-data:/data/db -d mongo
That will publish the container port 27017 to host interface 127.0.0.1 port 27017. You can only add the interface to the host port, the container itself must still bind to 0.0.0.0.

Docker container doesn't expose ports when --net=host is mentioned in the docker run command

I have a CentOS docker container on a CentOS docker host. When I use this command to run the docker image docker run -d --net=host -p 8777:8777 ceilometer:1.x the docker container get host's IP but doesn't have ports assigned to it.
If I run the same command without "--net=host" docker run -d -p 8777:8777 ceilometer:1.x docker exposes the ports but with a different IP. The docker version is 1.10.1. I want the docker container to have the same IP as the host with ports exposed. I also have mentioned in the Dockerfile the instruction EXPOSE 8777 but with no use when "--net=host" is mentioned in the docker run command.
I was confused by this answer. Apparently my docker image should be reachable on port 8080. But it wasn't. Then I read
https://docs.docker.com/network/host/
To quote
The host networking driver only works on Linux hosts, and is not supported on Docker for Mac, Docker for Windows, or Docker EE for Windows Server.
That's rather annoying as I'm on a Mac. The docker command should report an error rather than let me think it was meant to work.
Discussion on why it does not report an error
https://github.com/docker/for-mac/issues/2716
Not sure I'm convinced.
The docker version is 1.10.1. I want the docker container to have same ip as the host with ports exposed.
When you use --net=host it tells the container to use the hosts networking stack. So you can't expose ports to the host, because it is the host (as far as the network stack is concerned).
docker inspect might not show the expose ports, but if you have an application listening on a port, it will be available as if it were running on the host.
On Linux, I have always used --net=host when myapp needed to connect to an another docker container hosting PostgreSQL.
myapp reads an environment variable DATABASE in this example
Like Shane mentions this does not work on MacOS or Windows...
docker run -d -p 127.0.0.1:5432:5432 postgres:latest
So my app can't connect to my other other docker container:
docker run -e DATABASE=127.0.0.1:5432 --net=host myapp
To work around this, you can use host.docker.internal instead of 127.0.0.1 to resolve your hosts IP address.
Therefore, this works
docker run -e DATABASE=host.docker.internal:5432 -d myapp
Hope this saves someone time!

Resources