Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between docker-compose ports vs expose

What is the difference between ports and expose options in docker-compose.yml?

like image 268
Bibek Shrestha Avatar asked Nov 25 '16 09:11

Bibek Shrestha


People also ask

What is the difference between expose and ports 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 Docker compose ports?

Docker Compose exposes all specified container ports, making them reachable internally and externally from the local machine.

What's the difference between publishing and exposing a port?

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 does expose port do in Docker?

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.


2 Answers

According to the docker-compose reference,

Ports is defined as:

Expose ports. Either specify both ports (HOST:CONTAINER), or just the container port (a random host port will be chosen).

  • Ports mentioned in docker-compose.yml will be shared among different services started by the docker-compose.
  • Ports will be exposed to the host machine to a random port or a given port.

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 is defined as:

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 

Edit

In recent versions of Dockerfile, EXPOSE doesn't have any operational impact anymore, it is just informative. (see also)

like image 55
Bibek Shrestha Avatar answered Oct 01 '22 17:10

Bibek Shrestha


ports:

  1. Activates the container to listen for specified port(s) from the world outside of the docker(can be same host machine or a different machine) AND also accessible world inside docker.
  2. More than one port can be specified (that's is why ports not port)

enter image description here

expose:

  1. Activates container to listen for a specific port only from the world inside of docker AND not accessible world outside of the docker.
  2. More than one port can be specified

enter image description here

like image 21
Mehraj Malik Avatar answered Oct 01 '22 16:10

Mehraj Malik