This write-up will explain the difference between the ports and expose key in Docker compose.
Difference Between Expose and Ports in Docker Compose
The “expose” and “ports” keys in Docker compose are utilized to configure the network and the exposing ports for the container. However, both keys are used for the same purpose, but the key difference between the “ports” and “expose” is that the expose key is accessible to the services that are connected to the same network but not on the host. In contrast, ports are accessible and published on the host as well as on the connected network.
Checking the Difference Between “expose” and “ports” Keys in Docker-compose Practically
To check the difference between expose and ports key practically, go through the listed examples:
Example 1: Utilize “ports” Key in Docker-Compose File
The “ports” key is utilized to publish the container on the host machine. These containers are accessible to all services that are executing on the host as well on a connected network.
To use the “ports” key in Docker compose, check out the given instructions.
Step 1: Create a “docker-compose.yml”
Make a “docker-compose.yml” file and paste the below code block into the file:
services:
web:
image: nginx:latest
ports:
- 8080:80
According to the above snippet:
- “web” service is configured in the “docker-compose.yml” file.
- “image” defines the base image for the compose container
- “ports” specify the exposing port of the container on a network and host:
Step 2: Start Containers
Next, create and fire up the compose container with the help of “docker-compose up” command:
Step 3: List Compose Container
List the container and verify the exposing port of the container. From the output, it can observe that we have published the container on the host:
Example 2: Utilize “expose” Key in Docker-Compose File
To utilize the expose key in the “docker-compose.yml” file, take a look at provided instructions.
Step 1: Create a “docker-compose.yml”
Now, configure the “web” service on exposing port 80 with the help of the “expose” key. Here, we have not defined any network for the container:
services:
web:
image: nginx:latest
expose:
- 8080:80
Step 2: Fire up the Container
Next, create and start the compose container to run web service using the provided command:
Step 3: List Compose Container
List the compose container and check the exposing port of the container. From the below output, you can observe that the container is accessible only on port 80 on a default selected network but not on host:
We have defined the distinction of “expose” and “ports” keys in Docker compose.
Conclusion
The “expose” and “ports” are both used to specify the exposing port of the container to run defined services. The major difference between these two keys is that “ports” is published and accessible on the host machine and also on the specified network, while “expose” is only published on the defined network and accessed by services that are running on the same network. This write-up demonstrated the distinction between “ports” and “expose” in Docker compose.