How to expose in a network?
The below example is from the
From my understanding they want to have
redis port 6379 available in the
Why don’t they have
expose: - "6379"
version: '2' services: web: build: . ports: - "5000:5000" volumes: - .:/code networks: - front-tier - back-tier redis: image: redis volumes: - redis-data:/var/lib/redis networks: - back-tier
2 Solutions collect form web for “How to expose in a network?”
From the official Redis image:
This image includes EXPOSE 6379 (the redis port), so standard
container linking will make it automatically available to the linked
containers (as the following examples illustrate).
which is pretty much the typical way of doing things.
You don’t need links anymore now that we assign containers to docker networks. And without linking, unless you publish all ports with a
docker run -P, there’s no value to exposing a port on the container. Containers can talk to any port opened on any other container if they are on the same network (assuming default settings for ICC), so exposing a port becomes a noop.
Typically, you only expose a port via the Dockerfile as an indicator to those running your image, or to use the
-P flag. There are also some projects that look at exposed ports of other containers to know how to talk to them, specifically I’m thinking of nginx-proxy, but that’s a unique case.
However, publishing a port makes that port available from the docker host, which always needs to be done from the docker-compose.yml or run command (you don’t want image authors able to affect the docker host without some form of local admin acknowledgement). When you publish a specific port, it doesn’t need to be exposed first.