How to connect to localhost from a docker container? - docker

I have a Python application to expose a REST API. The Python server is running on http://127.0.0.1:5000
I have another application written in NodeJS to wrap the API coming from Python and expose another API as a passthrough. The Node server is running on http://localhost:8080
I'm new to Docker, I'm building a docker image for the Node application (in MacOS Silicon). The problem is when I invoke the API using curl http://localhost:8080 The docker desktop says "Connection refused: /127.0.0.1:5000"
The following is the docker run command I'm using
docker run --platform linux/amd64 -d -v ./Config.toml -p 8080:8080 myApp/app:v0.1.0
I tried with the --network host flag with the docker run command, but it doesn't work since it ignores all the declared ports. And I tried with the http.server --bind 0.0.0.0 with the docker run command, but the result says "pull access denied for http.server"
How can I solve this?

curl http://127.0.0.1:5000 on host machine to see if server is actually running.
Check server accessibility from within container. Simply run the docker container on interactive it and try access the server from the container.
docker run --platform linux/amd64 -it myApp/app:v0.1.0 /bin/bash
curl http://127.0.0.1:5000
If this does not work, it's a network configuration issue in the container you should set up to allow access to host machine.
If it works, then bind NodeJs app to 0.0.0.0 to allow conns from any ip.
docker run --platform linux/amd64 -d -v ./Config.toml -p 8080:8080 myApp/app:v0.1.0 -host 0.0.0.0

Related

Could not connect from linux container(docker-desktop) to service on localhost windows

How to Connect to Host service on windows from within a Docker Container on Docker Desktop WINDOWS?
The service is running locally on localhost:9092
I tried using host.docker.internal but doesn't seems to work.
I tried this example from Docker-desktop document
https://docs.docker.com/desktop/windows/networking/#per-container-ip-addressing-is-not-possible
Run the following command to start a simple HTTP server on port 8000.
python -m http.server 8000
If you have installed Python 2.x, run python -m SimpleHTTPServer 8000.
Now, run a container, install curl, and try to connect to the host using the following commands:
docker run --rm -it alpine sh
apk add curl
curl http://host.docker.internal:8000
exit
The above doesn't seem to work on windows.
You should not start the service binded to localhost (127.0.0.1). Start the service binded to the IP of docker for desktop (192.168.x.x usually). Otherwise, start the service attached to all interfaces using 0.0.0.0, this could be insecure though.

Jenkins docker running in the container but not launching in web browser

On my Redhat7linux docker host, i have created a jenkins container by pulling the jenkins official image from docker hub and i was able to bring the jenkins container up & running by executing the command:
docker run -d -p 50000:8080 -v $PWD/jenkins:/var/lib/jenkins -t jenkins_master
and i could see the jenkins is up when i checked the logs using the docker logs {containerID} but when i try to launch it in web browser with {hostip}:50000, I couldn't access it as it throws "The site cant be reached", and since my container is running inside a company network, should I either open/enable that port 50000 or do I need to set any proxy in the docker host?
Am I missing something here?
Here are the outputs of the docker command:
The official image provide the following command :
docker run -p 8080:8080 -p 50000:50000 -v /your/home:/var/jenkins_home jenkins
It seems that both ports 8080 and 50000 have to be exposed.
Execute the docker run command to run the container, check the status of your container.
docker container run -p [YOUR PORT]:8080 -v [YOUR
VOLUME]:/var/jenkins_home
--name jenkins-local jenkins/jenkins:lts
you can then access it using localhost:[YOUR PORT]

Docker Desktop created container do not have access to macos [duplicate]

Running docker for Mac 17.06.0 I have created a docker file that creates an image of Apache server. Notice it exposes port 80.
FROM ubuntu:16.04
RUN apt-get update
RUN apt-get install -y apache2
ADD index.html /var/www/html/
CMD /usr/sbin/apache2ctl -D FOREGROUND
EXPOSE 80
In the same folder of the Dockerfile I have created a simple index.httml file.
Then I built and ran it using
docker build -t webserver .
docker run -d webserver
I took the IP address of the running container using
docker inspect [container_name] | grep -i IPAddress
and when I curl
curl 172.17.0.2
I get no answer.
I do get an answer when running -p 80:80 and using localhost in the curl command.
curl localhost
But I want to understand why can't I curl the container IP.
Questions:
How can I get an answer for my curl?
I understand I can't ping my container when using docker for Mac (link).
Can I telnet it just to verify that the port is exposed?
Can I SSH it?
On Docker for Mac the Docker engine is running inside a small VM using Hyper-V. As consequence, the ip 172.17.0.2 is valid only inside that VM and not on your host system. See https://docs.docker.com/docker-for-mac/docker-toolbox/#the-docker-for-mac-environment for more details and comparison to other VM concepts like Docker Machine.
When you run your Docker container, you need to bind a local port to the container like so:
docker run -d -p 80:80 webserver
where the first 80 is the port on the localhost and the second is the port on the container that is exposed. Just having the port exposed in the dockerfile is not enough to access it from the localhost.

Dockerized Aurelia App Is Not Visible Outside of Docker Container - Not listening on 0.0.0.0

I'm running an Aurelia app inside of the standard node docker container and it is listening on port 8080. Within the container, I have tested that it's running using curl; and it responds with the expected HTML. But I cannot reach the app via the mapped port on the host (outside the container).
I'm running the following command to start the container
$ docker run -it --rm -p 8080:8080 -v ${PWD}:/app node bash
Then inside the container, I install the cli and create a new app
# npm install -g aurelia-cli
# au new
After creating a default app, I cd into the app directory and run the app.
# au run
As I said above, I can verify the app is running using curl http://localhost:8080. However, on the host, I cannot access the app:
$ curl http://localhost:8888
curl: (52) Empty reply from server
Originally, I thought this was a docker problem. See this question. But it turns out that Aurelia is listening on localhost rather than 0.0.0.0.
Running Aurelia with the host option set allows the server to listen on 0.0.0.0, so it will map properly in a docker container.
au run --host 0.0.0.0

Docker in Docker: Port Mapping

I have found a similar thread, but failed to get it to work. So, the use case is
I start a container on my Linux host
docker run -i -t --privileged -p 8080:2375 mattgruter/doubledocker
When in that container, I want to start another one with GAE SDK devserver running.
At that, I need to access a running app from the host system browser.
When I start a container in the container as
docker run -i -t -p 2375:8080 image/name
I get an error saying that 2375 port is in use. I start the app, and can curl 0.0.0.0:8080 when inside both containers (when using another port 8080:8080 for example) but cannot preview the app from the host system, since lohalhost:8080 listens to 2375 port in the first container, and that port cannot be used when launching the second container.
I'm able to do that using the image jpetazzo/dind. The test I have done and worked (as an example):
From my host machine I run the container with docker installed:
docker run --privileged -t -i --rm -e LOG=file -p 18080:8080
jpetazzo/dind
Then inside the container I've pulled nginx image and run it with
docker run -d -p 8080:80 nginx
And from the host environment I can browse the nginx welcome page with http://localhost:18080
With the image you were using (mattgruter/doubledocker) I have some problem running it (something related to log attach).

Resources