Access to localhost from docker container - docker

I want to access to a local webserver that is outside the docker container.
I have a testsuite (casperjs) inside a docker container that open a browser to localhost:8002/etc, but "localhost" here refers to my local machine, not something inside the docker container. How can I achieve that ?

You would have to use the IP of the bridge to get to the host. Docker usually is deployed the other way around though. You'd have a service inside a container beint used from outside it... (thus casperJS inside the container being hit from the host)
Or you could have the original webserver in a container and --link it into the casper container and utilize the host name of the webserver container:
docker run -p 80 --name app {your image} start
docker run --link app:app {your casperjs} start
and utilize the hostname "app" for accessing the webserver.
Hope this helps!

Related

How to access a port that is already used by another Docker container

I am running a Tomcat server on one Docker container. On another docker container, i want to be able to access that Tomcat server. So, what I do is to use the -p option to map that port to the port mapped by the Docker container running the Tomcat server.
In short, I have the Tomcat container, which was run using something like this.
docker run ... -p X:8080 ...
And the other docker container like this
docker run ... -p X:X ...
However, if I try to do so, I get "Port is already allocated" error. How can I solve this problem?
When you add -p X:Y you are mapping Y port from container to X port in host machine and making it accessable in host.
Lets assume your tomcat container is running on 8080:8080
Now you have another container running
You can access tomcat container inside 2nd container by internal IP.
If both containers are on default network.
Something like this 172.0.0.2:8080
You can get assigned internal IP for container by this
docker network inspect bridge
or
docker container inspect $id
where id is container id

Allow my docker container to make network requests

I have my application running locally in a Docker container, I have published the port which I want to use to invoke its API. However, my Docker container application also needs to make other network requests to externally hosted APIs. Currently I am getting network errors when it tries to make these requests. How do I give my Docker container access to the same network that my local machine is on? Is there a Docker config I need to pass to my docker -it -p 8080:8080 command?
You need to add these options to your docker run command:
--network host
It will bind container networking directly to the Docker host’s network.
Documentation: https://docs.docker.com/network/host/

With docker --net="host" i can access the ports

When i run a container for a web-application that listens on port 8090
With
docker run -p 8090:8090 -h=%ComputerName% mycontainer
Then i can access the services on http://localhost:8090
If i started the container with
docker run --net="host" -h=%ComputerName% mycontainer
Then i can't access to the services on http://localhost:8090
Why ??
Is not supposed that with -net="host" the container shares the network of the host, then why i can't access to http://localhost:8090 with --net="host"
This is not what --net=host does.
In your first example; you are mapping the ports of the container to your host - which allows you to via the services of the container.
In the second example; you remove the -p option so no ports are now mapped.
What the --net=host does - is allows your container to view port on the host machine as if they were local to the container. So say you had a database running on port 5000 of your host machine, and it was not in a Docker container - you would be able to access this on the container via localhost:5000. (Note - there are some caveats to this; such as Docker for Mac would actually need docker.for.mac.localhost)

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 to local process from docker

I have a docker container running on my local machine(mac). I have another program which listens on localhost. How do I make code running in docker container to connect to this process?
You can do this through the port flag when running Docker.
docker run -it -p 8080:8080 myimage
-p 8080:8080 is the flag. The right hand side of the : is port that your docker container is listening to. The left hand side is where you want that to be mapped to on the local host. In this example, when I access localhost:8080, I am accessing what the Docker container is listening to on that port.

Resources