toto'
toto'

Reputation: 1584

Difference between using "expose" in dockerfile and docker-compose file?

I am wondering what is the difference between using EXPOSE in a Dockerfile and in a Docker-Compose file?

What if it is declared in one file and not the other? Or what if it is declared in both but with different port numbers?

Upvotes: 17

Views: 3746

Answers (1)

Tarun Lalwani
Tarun Lalwani

Reputation: 146630

EXPOSE in Dockerfile is a just a metadata information. Which tells docker when someone uses docker run -P which ports need to be Exposed.

Using them in compose or docker run is a dynamic way of specifying these ports. So an image like nginx or apache which is always supposed to run on port 80 inside the container will use EXPOSE in Dockerfile itself.

While an image which has dynamic port which may be controlled using an environment variable will then use expose in docker run or compose file

docker run -e UI_PORT=5556 --expose 5556 -P ....

Upvotes: 12

Related Questions