0

I have a docker-compose.yml file, simplified below

slim-first:
  image: slim-image
  expose:
    - 9191

slim-second:
  image: slim-image
  expose:
    - 9192

The slim-image image comes from a Dockerfile that starts a service (using the PHP Slim framework) with the following command

#File: Dockerfile

# ...

CMD ["/usr/local/bin/php", "-S", "0.0.0.0:9191", "-t", "/slim/public"]

You may have already spotted the problem -- I need to provide PHP's built-in webserver with a port. However, I'm trying to start two services -- each available on a different port.

My Question: Is there a way for me, in my Dockerfile to access the exposed port values from my docker-composer.yml file?

Or is the solution to duplicate the port number into an environment variable that the Dockerfile can see.

Or is there some third path that would let me have the port value configured in one, and one place, only?

To clarify, I know I could do something like this

slim-second:
  image: slim-image
  expose:
    - 9192
  args:
    - PORT=9192

...

ARG PORT=9191
CMD ["/usr/local/bin/php", "-S", "0.0.0.0:$PORT", "-t", "/slim/public"]

but I don't want to repeat the port twice in my docker-compose.yml file.

Alana Storm
  • 164,128
  • 91
  • 395
  • 599
  • Do you mean something like this: [Pass a variable to a Dockerfile from a docker-compose.yml file](https://stackoverflow.com/questions/36738381/pass-a-variable-to-a-dockerfile-from-a-docker-compose-yml-file)? – tgogos Nov 01 '18 at 15:24
  • thank you @tgogos -- I do want something like that, but I don't want to have two places in the docker-compose.yml file that needs a port definition. I added some clarifying comments to my original post. – Alana Storm Nov 01 '18 at 15:36

1 Answers1

1

It doesn’t matter: you can run servers in multiple containers that all listen on the same port, and map them to different published ports on the host.

I would hard-code the port in the Dockerfile; say it’s always port 9191. Then in the docker-compose.yml file you can set

slim-first:
  image: slim-image
  ports: ["9191:9191"]
slim-second:
  image: slim-image
  ports: ["9192:9191"]

and the two containers will be reachable from different ports on the host.

(EXPOSE in a Dockerfile is mostly documentation; it has two rarely-relevant side effects. expose: in a Docker Compose file doesn’t do much of anything for you at all.)

David Maze
  • 130,717
  • 29
  • 175
  • 215
  • Thank you for the attention -- but is this true/accurate? It's my understanding that if two docker containers are going to talk to one another over their internal network that I need to expose a port on each one. – Alana Storm Nov 01 '18 at 16:03
  • I don’t think that’s true on the “new” Docker network setup. (One of the two things EXPOSE does is set up a port forwarding for `--link` but that’s discouraged these days; just run both containers on the same non-default network.) – David Maze Nov 01 '18 at 17:10
  • Ah ha, that makes more sense, thank you. For this particular problem I'm stuck using Docker's old style network settings but I'll definitely keep this in mind when/if I'm doing something greenfield and actually learn how Docker networking works :) – Alana Storm Nov 01 '18 at 17:21