Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does "ports" on docker-compose.yml have the same effect as EXPOSE on Dockerfile?

Tags:

docker

Does declaring on a docker-compose.yml:

ports:  - "3306:3306" 

and on Dockerfile:

EXPOSE 3306 

have the same effect?

like image 248
Victor Basso Avatar asked Feb 22 '16 08:02

Victor Basso


People also ask

What is the difference between ports and expose Docker?

Expose is defined as:Only the internal port can be specified. Ports are not exposed to host machines, only exposed to other services.

What is ports in Docker compose?

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: ...

What is the difference between Dockerfile and Docker compose yml?

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.

What does expose port do in Docker?

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.


Video Answer


1 Answers

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.

like image 150
VonC Avatar answered Sep 22 '22 06:09

VonC