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

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.

Related

How to connect to localhost from a docker container?

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

I can't access my Docker container on GCP Compute Engine

I have my Docker container running on GCP Compute Engine. The CE server is running on CentOS 7. My Docker container has the application being served by Nginx with port 80 exposed. For some reason, I can't access it from the external IP address on my browser.
I ran the container with this command:
sudo docker run --name myapp -p 80:80 -d myapp:1.0.0
When I do sudo curl <internal_ip>:80 or sudo curl <localhost>:80 it will show that the application is running and returns back the content, but if I try to access in my browser with <external_ip>:80, it doesn't load anything. What can I do to make this accessible through the external IP address?
It seems I had to configure the firewall to open up port 80.

cannot reach port 80 from Windows to Docker image

Running on Windows 10 with Docker Installed
// DockerFile
FROM ubuntu:latest
RUN apt-get update
RUN apt-get install -y apache2
ADD index.html /var/www/html/
CMD /usr/sbin/apache2ctl -D FOREGROUND
EXPOSE 80
// building image, running container and checking IP
$ docker network inspect bridge
and getting
"IPv4Address": "172.17.0.2/16"
when trying to access via Chrome browser to http://172.17.0.2, I get "This site can’t be reached"
Expose Port: Please check have you exposed port number 80. (not only in docker file)? but In your docker run command, you need to specify "-p 80:80". Can you please share your command, how you are running your docker container?
IPAddress: 172.17.0.2 This IPAddress is docker internal docker ip address, you need to use localhost or your_machine_ip_address to access that docker container.
Please let me know, If you have any difficulty with this, share your docker run command.

Tomcat with Docker

I am testing out running a tomcat8 on my Mac. I have the following Dockerfile:
FROM tomcat:8-jre7
MAINTAINER "Sonam Lastname <sonam#mymail.com>"
When I run the Docker container with the following command:
docker run -d -P sonam/docker-webapp
I check for the docker process by
docker ps -l
and see the port mapped at:
0.0.0.0:32769->8080/tcp
I am not able to access the tomcat page with localhost:32769 (and even tried to 8080 port).
thanks
-Sonam
Docker runs in a virtual machine when on Mac. Tomcat will listen to that network interface.
You can run docker-machine ip <name of your docker machine> and access it via that IP instead of localhost.
If on boot2docker, it is similar: boot2docker ip.

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