-1

Below is example code:

services:
  db:
    image: "mysql:8"
    restart: always
    environment:
      MYSQL_DATABASE: 'test'
      MYSQL_USER: 'root'
      MYSQL_PASSWORD: 'test'
      MYSQL_ROOT_PASSWORD: 'test'
    ports:
      - "3309:3306"
    expose:
      - "3309"

By definition, in docker-compose file, does expose: function on the host port or the container port?

Does ports: follow the [host_port]:[container_port] convention or [container_port]:[host_port]?

what exactly is the example code above doing with ports?

Kid_Learning_C
  • 2,605
  • 4
  • 39
  • 71
  • `expose:` does almost nothing and you can pretty much always safely delete it from a `docker-compose.yml` file. But, the port number is a container-side port and not a host-side port. – David Maze Dec 05 '19 at 01:13

1 Answers1

3

EXPOSE is simply used for documentation purposes, and not to actually publish any of the ports. Think of it as meta data, allowing other developers or admins to have some sort of documentation on the image.

When you publish ports, you're doing it on the localhost machine of that container, or host machine.

For example, if you run a redis container on your host machine and publish 6379, your localhost:6379 will be mapped to the container's port of 6379.

The convention goes: host-port:container-port

alex067
  • 3,159
  • 1
  • 11
  • 17
  • It is not often used but the `publish-all` switch will publish all 'exposed' container ports to random unused host ports. I think this is an example of usage of the documentation of the image. How else would it know which ports to publish. – emory Dec 04 '19 at 21:20
  • 1
    What if I want to localhost:6379 to map to container's port 1111? By "publish", what command are you referring to? EXPOSE or PORTS? – Kid_Learning_C Dec 05 '19 at 01:23
  • 1
    You do ports: 6367:1111 – alex067 Dec 05 '19 at 01:27