expose in dockerfile and docker-compose, and ports

tags: learning docker diff-between

content

what’s the difference between EXPOSE in Dockerfile, expose in docker-compose:

  • in Dockerfile, EXPOSE does not have any actual functionality other than documentation
  • in docker-compose, expose is actually exposing a port of a container to other containers
    • e.g., expose: 80 it makes port 80 reachable to other containers

also in docker-compose, there is ports option

  • it links a port on the host machine to a port in a container
  • ports: "5173:80" means that docker maps 5173 on host machine to the container’s port 80
  • i.e., port 80 inside the container can be reached with host’s 5173
  • docker is doing port forwarding with ports

up

down

reference