Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker compose port mapping

I have a docker-compose yml file as in below

version: '2' services:   nodejs:     build:       context: .       dockerfile: DockerFile     ports:       - "4000:4000"     links:       - redis     expose:       - "6379"   redis:     build:       context: .       dockerfile: Dockerfile-redis 

My goal is to forward nodejs-127.0.0.1 port 6379 to the redis host. I can already ping redis from the nodejs machine, but the ports are not mapped. Tried expose options, but no chance either.

like image 399
z.a. Avatar asked Feb 16 '16 10:02

z.a.


People also ask

What is port mapping in Docker?

Port mapping is used to access the services running inside a Docker container. We open a host port to give us access to a corresponding open port inside the Docker container. Then all the requests that are made to the host port can be redirected into the Docker container.

How do I map a port to running Docker container?

Map TCP port 80 in the container to port 8080 on the Docker host for connections to host IP 192.168. 1.100. Map UDP port 80 in the container to port 8080 on the Docker host. Map TCP port 80 in the container to TCP port 8080 on the Docker host, and map UDP port 80 in the container to UDP port 8080 on the Docker host.

Can Docker run multiple ports?

In your Dockerfile , you can use the verb EXPOSE to expose multiple ports.

What is ports in Docker compose Yml?

Ports is defined as:Either specify both ports (HOST:CONTAINER), or just the container port (a random host port will be chosen). Ports mentioned in docker-compose. yml will be shared among different services started by the docker-compose. Ports will be exposed to the host machine to a random port or a given port.


2 Answers

If you want to bind to the redis port from your nodejs container you will have to expose that port in the redis container:

version: '2' services:   nodejs:     build:       context: .       dockerfile: DockerFile     ports:       - "4000:4000"     links:       - redis    redis:     build:       context: .       dockerfile: Dockerfile-redis     expose:       - "6379" 

The expose tag will let you expose ports without publishing them to the host machine, but they will be exposed to the containers networks.

https://docs.docker.com/compose/compose-file/#expose

The ports tag will be mapping the host port with the container port HOST:CONTAINER

https://docs.docker.com/compose/compose-file/#ports

like image 104
JesusTinoco Avatar answered Sep 24 '22 00:09

JesusTinoco


It's important to point out that all of the above solutions map the port to every interface on your machine. This is less than desirable if you have a public IP address, or your machine has an IP on a large network. Your application may be exposed to a much wider audience than you'd hoped.

redis:   build:     context:     dockerfile: Dockerfile-redis     ports:     - "127.0.0.1:3901:3901" 

127.0.0.1 is the ip address that maps to the hostname localhost on your machine. So now your application is only exposed over that interface and since 127.0.0.1 is only accessible via your machine, you're not exposing your containers to the entire world.

The documentation explains this further and can be found here: https://docs.docker.com/compose/compose-file/#ports


Note: If you're using Docker for mac this will make the container listen on 127.0.0.1 on the Docker for Mac VM and will not be accessible from your localhost. If I recall correctly.

like image 31
Breedly Avatar answered Sep 23 '22 00:09

Breedly