What is the difference between ports
and expose
options in docker-compose.yml
?
Expose is defined as:Only the internal port can be specified. Ports are not exposed to host machines, only exposed to other services.
Docker Compose exposes all specified container ports, making them reachable internally and externally from the local machine.
We also discussed that the exposed port is metadata about the containerized application, whereas publishing a port is a way to access the application from the host.
What Is Docker Expose Port? This tells Docker your webserver will listen on port 80 for TCP connections since TCP is the default. For UDP, specify the protocol after the port. For more than one port, you can list EXPOSE more than once.
According to the docker-compose reference,
Expose ports. Either specify both ports (HOST:CONTAINER), or just the container port (a random host port will be chosen).
My docker-compose.yml
looks like:
mysql: image: mysql:5.7 ports: - "3306"
If I do docker-compose ps
, it will look like:
Name Command State Ports ------------------------------------------------------------------------------------- mysql_1 docker-entrypoint.sh mysqld Up 0.0.0.0:32769->3306/tcp
Expose ports without publishing them to the host machine - they’ll only be accessible to linked services. Only the internal port can be specified.
Ports are not exposed to host machines, only exposed to other services.
mysql: image: mysql:5.7 expose: - "3306"
If I do docker-compose ps
, it will look like:
Name Command State Ports --------------------------------------------------------------- mysql_1 docker-entrypoint.sh mysqld Up 3306/tcp
In recent versions of Dockerfile, EXPOSE
doesn't have any operational impact anymore, it is just informative. (see also)
ports:
expose:
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