G. Deward
G. Deward

Reputation: 1592

Is second value required when mapping ports in docker-compose file?

Within a docker-compose.yml file, what is the difference between - "3000" and - "3000:3000"? From the documentation, it appears they are the same. However, the first format does not open the port to the host machine.

ports:
 - "3000"
 - "3000-3005"
 - "8000:8000"
 - "9090-9091:8080-8081"
 - "49100:22"
 - "127.0.0.1:8001:8001"
 - "127.0.0.1:5000-5010:5000-5010"

I am using the official Docker docs on the site: here

Upvotes: 1

Views: 646

Answers (1)

BMitch
BMitch

Reputation: 263746

From the documentation:

Either specify both ports (HOST:CONTAINER), or just the container port (a random host port will be chosen).

When you only specify one port, you'll need to check the container with a ps or inspect to see what port was opened on the host side.

$ cat docker-compose.yml
version: '2'

services:
  web:
    image: nginx:latest
    volumes:
    - ./html:/usr/share/nginx/html
    ports:
    - "80"

$ docker-compose up -d
Creating network "test_default" with the default driver
Creating test_web_1

$ docker-compose ps
   Name            Command          State               Ports
--------------------------------------------------------------------------
test_web_1   nginx -g daemon off;   Up      443/tcp, 0.0.0.0:32769->80/tcp

$ curl http://localhost:32769
<html><body>hello world</body></html>

Upvotes: 2

Related Questions