docker container accessed on host

Docker volumes can be used to share files between a host system and the Docker container. For example, let's say you wanted to use the official Docker Nginx image and keep a permanent copy of the Nginx's log files to analyze later. By default the nginx Docker image will log to the /var/log/nginx directory inside the Docker Nginx container. Normally it's not reachable from the host filesystem.

Running our Image

docker run --name=nginx -d -v ~/nginxlogs:/var/log/nginx -p 5000:80 nginx

Some explanations

Switch Explain
--name=nginx names the container so we can refer to it more easily
-v ~/nginxlogs:/var/log/nginx. Sets up a bindmount volume that links the /var/log/nginx directory from inside the Nginx container to the ~/nginxlogs directory on the host machine. Docker uses a : to split the host's path from the container path, and the host path always comes first.
-p 5000:80 sets up a port forward. The Nginx container is listening on port 80 by default. This flag maps the container's port 80 to port 5000 on the host system.
nginx specifies that the container should be built from the Nginx image, which issues the command nginx -g "daemon off to start Nginx.

Step 2 — Accessing Data on the Host

We now have a copy of Nginx running inside a Docker container on our machine, and our host machine's port 5000 maps directly to that copy of Nginx's port 80.

Load the address in a web browser, using the IP address or hostname of your server and the port number, http://203.0.113.0:5000. You should see:

Welcome to NGRIX

More interestingly, if we look in the ~/nginxlogs directory on the host, we'll see the access.log created by the container's nginx which will show our request:

cat ~/nginxlogs/access.log

This should display something like:

Output
203.0.113.0 - - [11/Nov/2016:00:59:11 +0000] "GET / HTTP/1.1" 200 612 "-"
"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36" "-"

Here we can see the same data tht is being shown from inside the Image.

Ports

Inside the dockerbuild To mention a port that should be shared do a

#Postgres port
EXTERNAL=5432

When you run the container, try

docker run -P  -i 6f217b0e4dae 

the -P means auto match ports.

But where is it mapped from ?

How do I see where the port is mapped ?

docker ps

You should see something like this

docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED                  STATUS              PORTS                     NAMES
3cdf6b6bccc7        6f217b0e4dae        "/usr/lib/postgresql…"   Less than a second ago   Up 10 seconds       0.0.0.0:32770->5432/tcp   modest_engelbart

The crucial info is this 0.0.0.0:32770->5432/tcp.

If I connect to host 0.0.0.0:32770 that will link me to the port 5432 in the container...

psql -d -p 32770 -h 0.0.0.0

Note -p -h are for example. I am 100% sure of the syntax

This looks better

docker run -d -p 5801:5801 -p 5802:5802 .....

DataGrip

From DataGrip it looks like