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.
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.
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.
In your Dockerfile , you can use the verb EXPOSE to expose multiple ports.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With