Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker-compose: publish multiple ports

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?

like image 214
StephaneM Avatar asked Jun 27 '18 07:06

StephaneM


People also ask

Can Docker expose multiple ports?

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.

Does Docker compose publish ports?

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.

What is difference between expose and publishing?

Short answer: EXPOSE is a way of documenting. --publish (or -p ) is a way of mapping a host port to a running container port.

How do I publish port Docker?

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.


1 Answers

According to the docker documentation, the recommended way to specify port mapping is string declaration specially when a container port lower than 60.

like image 176
andolsi zied Avatar answered Sep 20 '22 07:09

andolsi zied