Unable to navigate to Docker container - docker

I built a container with a react app within it.
I start the app.. docker run -p 3000:3000 blarg3/node
I can bash into the container and curl localhost:3000 and it returns the front page of my site.
When I go to the IP and port http://172.17.0.2:3000/. Nothing is returned.

By default "docker run" binds port only to local interface. If you want to bind it to another interface you need to specify it's IP address like this:
docker run -p 172.17.0.2:3000:3000 blarg3/node
You can read more about docker networking options here: https://docs.docker.com/engine/userguide/networking/default_network/dockerlinks/#connect-using-network-port-mapping

Related

Reset localhost:80 port

running containersall containers
I've recently started learning docker and after following the tutorial, I ran the following command
docker run -d -p 80:80 docker/getting-started
and open up port localhost:80 and saw the docker getting started page. However, I had to run my client's project, whose port was mapped to localhost:80 as well. On account of this, I'm unable to run my client's project on localhost:80. In addition to that, any instance I randomly open up docker and then switch to localhost:80, it redirects to docker's getting started tutorial. I want to reset this localhost:80 port so that when I run my client's project, I can map them to localhost:80. Any method to rectify the issue?
First find you container's ID using:
docker ps
Supposing it is e11d9f8bb730, you can now stop and remove the container with:
docker stop e11d9f8bb730
docker rm e11d9f8bb730
Run again your container, this time using a different port:
docker run -d -p 81:80 docker/getting-started
Now your container is running on port 81 and you will be able to run your client's App on port 80.
First search for the container to see if its open:
docker container ls
If it's not then the page is probably cached by the browser.
I found this to be especially true when using Chrome.
So, if you don't see your container then use your browsers clear cache tool.
In Chrome this is as easy as right clicking on the page and selecting inspect, then right clicking the refresh page icon and selecting hard refresh.
You can use docker to map the container port to any port you choose on your local machine. As an example, you could use your docker getting started and map the port to 8080 instead of 80 like this:
docker run -d -p 127.0.0.1:8080:80/tcp docker/getting-started
All you have to do is stop the container you just started (docker / getting-started). You can open a command prompt, then type this command:
docker container ls
You can see which containers are currently running. For example:
docker containers list
You just need to do this command for the stop container:
docker container stop *yourContainerName*

How to get the client IP from within a web service running within a docker container?

I have an application that extracts the client IP making the request. When I run the application directly on a server, it works and I could get the IP.
But when I run it within a docker containing by executing this command:
docker run --rm -d -p 4300:4300 image
All of a sudden the client IP being reported is now 172.17.0.1.
Googling around I see suggestion to pass in --net=host but doing this:
docker run --rm --net=host -p 4300:4300 image
now leads to the application not being reachable. For some strange reason it looks like the application is no longer available at the specified port.
This also does not work even when I drop the -p 4300:4300 as I got a message in the console that it is not needed when --net=host is used. That is:
docker run --rm --net=host -p image
Any suggestions on how to get this done? That is how to get the client IP from within a web service running within a docker container? I am running docker on a mac. Don't know if this has anything to do with the problem.
If you use the net=host configuration, you don't need the -p <host>:<container> setting, since this is only used to port forward when you use the bridge network configuration.
Just drop this flag and browse to whatever port your application listens on. It should work.

How to get the docker-machine IP in Play With Docker (PWD) environment?

I'm following the tutorial in https://docker-curriculum.com/. When I run a docker container with a static website (docker run -d -P --name static-site prakhar1989/static-site) I can't access the web application because I have not the IP address of the docker environment. How can I get the docker-machine IP in Play With Docker (PWD) environment?
The instance IP address is all over the place in PWD, its even part of the command line as the root ssh. Is that not what you're looking for? Whenever you run a container and expose a port, PWD lets you click a button that opens a link to the ip:port

Map container to hostname other than localhost in Docker for Mac

I am creating an Nginx container that I would like to access locally at http://api. Using Docker Machine, I assumed running docker-machine create default and docker-machine ip default to receive the IP and editing my hosts file to something like this:
# docker-machine ip default --> 192.168.99.100
192.168.99.100 api
should map requests to api\ to the Docker Machine IP and serve my content.
Two things are confusing me:
I launch Docker through the Mac App and can create Nginx containers and access content at http://localhost. However, running docker-machine ls returns no machines. This is confusing because I thought Docker had to run on a VM.
Starting from scratch and starting Docker Machine, then spinning up containers seems to have no effect. In other words, I still can access content at http://localhost but not http://api
Instead of accessing my container at http://localhost I want to access it at http://api. How do I do this?
I'm using Docker for Mac 17.12 and Docker Machine 0.14.
On the base of your this question:
Instead of accessing my container at http://localhost I want to access
it at http://api. How do I do this?
Your docker run command:
docker run -it --rm --name test --add-host api:192.168.43.8 -p 80:80 apachehttpd
1st Thing: The --add-host flag add value to /etc/hosts in your container /etc/hosts so http://api will also response inside the container if ping inside that container.
This is how will ping response inside container
2nd Thing: Edit your host etc/hosts file and add
api 192.168.43.8 [your ip]
This is how you can see in Browser.

How to connect two docker containers through localhost?

I have two services running in separate containers, one is grunt(application) and runs off port 9000 and the other is sails.js (server) which runs off port 1337. What I want to try to do is have the client app connect with the server through localhost:1337. Is this feasible? Thanks.
HOST
You won't be able to connect to the other container with localhost (as localhost is the current container) but you can connect via the container host (the host that is running your container). In your case you need boot2docker VM IP (echo $(boot2docker ip)). For this to work, you need to expose your port at the host level (which you are doing with -p 1337:1337).
LINK
Another solution that is most common and that I prefer when possible, is to link the containers.
You need to add the --name flag to the server docker run command:
--name sails_server
You need to add the --link flag to the application docker run command:
--link sails_server:sails_server
And inside your application, you will be able to access the server at sail_server:1337
You could also use environment variables to get the server IP. See documentation: https://docs.docker.com/userguide/dockerlinks/
BONUS: DOCKER-COMPOSE
Your run commands may start to be a bit long... in this case I like to use docker-compose that allows me to define my containers and their relationships (volumes, names, link, commands...) in one file.
Yes if you use docker parameter -p 1337:1337 in your docker run command, it will expose the port 1337 from inside the container to your localhost:1337

Resources