I'm trying to publish 2 ports of a simple docker container to make some tests.
Here are the steps to reproduce the issue.
My simple Dockerfile:
FROM bash:4
RUN echo ok
Built using docker build . -t essai
My first version for the docker-compose.yml file, this one works:
version: '3'
services:
essai:
image: essai
ports:
- 25432:5432
But when I try to publish a second port like this:
version: '3'
services:
essai:
image: essai
ports:
- 25022:22
- 25432:5432
I get this strange error message:
$ docker-compose up Creating network "sandbox_default" with the default driver Creating sandbox_essai_1 ... Creating sandbox_essai_1 ... error
ERROR: for sandbox_essai_1 Cannot create container for service essai: invalid port specification: "1501342"
ERROR: for essai Cannot create container for service essai: invalid port specification: "1501342" ERROR: Encountered errors while bringing up the project.
Where does it find the port 1501342
?
Funny thing is when I write my docker-compose like this:
version: '3'
services:
essai:
image: essai
ports:
- "25022:22"
- 25432:5432
It works.
What's the magic with these double quotes and the port number coming out of nowhere?
Using 'EXPOSE' inside your Dockerfile for exposing a single port. Using multiple 'EXPOSE' inside your Dockerfile for exposing more than one port. 'docker-compose. -P (Upper case) to publish all expose ports randomly.
Only the CONTAINER part is mandatory. As presented above, we can also publish multiple container ports at once using different variants of the short syntax and configuring it more precisely. Docker Compose exposes all specified container ports, making them reachable internally and externally from the local machine.
Short answer: EXPOSE is a way of documenting. --publish (or -p ) is a way of mapping a host port to a running container port.
By default, when you create or run a container using docker create or docker run , it does not publish any of its ports to the outside world. To make a port available to services outside of Docker, or to Docker containers which are not connected to the container's network, use the --publish or -p flag.
According to the docker documentation, the recommended way to specify port mapping is string declaration specially when a container port lower than 60.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With