What Is the Difference between Docker-Compose Ports and Expose?
You may have wondered what is the difference between docker-compose ports and docker-compose expose. Here is a clear explanation to help you better understand the matter.
Ports
Ports mentioned in the docker-compose.yml
file will be shared among different services started by the docker-compose.yml
. These ports will be then exposed to the host to a random port or predefined port.
This configuration will set the port to 3306
for the services and then expose this port to the host as port 32769
(this port will be chosen randomly)
mysql:
image: mysql:5.7
ports:
- "3306"
docker-compose ps
Output:
Name Command State Ports
-------------------------------------------------------------------------------------
mysql_1 docker-entrypoint.sh mysqld Up 0.0.0.0:32769->3306/tcp
Expose
On the other hand, expose ports mentioned in the docker-compose.yml file will be only accessible to linked services and won’t be exposed to the host.
The following example will set the expose the port 3306
only to the connected services and won't expose the port to the host:
mysql:
image: mysql:5.7
expose:
- "3306"
docker-compose ps
Output:
Name Command State Ports
---------------------------------------------------------------
mysql_1 docker-entrypoint.sh mysqld Up 3306/tcp
-
How to Fix Docker Permission Denied Issue?
If you are struggling with the Docker permission denied error, we have prepared a quick fix for you. Step 1 - Create a docker group The first step is you create a docker group if you haven’t done i...
Questions -
Solved: Cron missing newline before EOF
This error may also happen when a crontab file is generated automatically and the generator failed to insert a newline character at the end of the crontab file.
Questions -
How Do I Pass Environment Variables to Docker Containers?
It is always a good practice to separate the app from its configuration. It is not a good idea to have a database login credential defined as variables in the code of the application. This is why w...
Questions -
How to Copy Files from Docker Container to Host?
Before Docker 1.8, we could only copy files from the container to the host. However, as containers became even more popular, copying files to and from containers has become necessary. One of the re...
Questions
Make your mark
Join the writer's program
Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.
Write for usBuild on top of Better Stack
Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.
community@betterstack.comor submit a pull request and help us build better products for everyone.
See the full list of amazing projects on github