Does declaring on a docker-compose.yml:
ports: - "3306:3306"
and on Dockerfile:
EXPOSE 3306
have the same effect?
Expose is defined as:Only the internal port can be specified. Ports are not exposed to host machines, only exposed to other services.
This property defines the ports that Docker Compose exposes from the container. These ports will be accessible by other services connected to the same network but won't be published on the host machine. We can expose a port by specifying its number in the services section: services: myapp1: ...
The Dockerfile is used to build images while the docker-compose. yaml file is used to run images. The Dockerfile uses the docker build command, while the docker-compose. yaml file uses the docker-compose up command.
An exposed port is a piece of metadata about the containerized application. In most cases, this shows which ports the application is listening to. Docker itself does not do anything with an exposed port. However, when we launch a container, we can use this metadata when publishing the port.
No: EXPOSE
only opens the port in the container, making it accessible by other containers.
"3306:3306"
will publish the port on the host, making the same port accessible from the host.
See Dockerfile EXPOSE
:
The
EXPOSE
instruction informs Docker that the container listens on the specified network ports at runtime.EXPOSE
does not make the ports of the container accessible to the host. To do that, you must use the-p
flag to publish a range of ports.
That is what the docker-compose.yml
ports section does. It maps container port to the host.
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