Compose: docker-compose run are open ports from dependencies services

Created on 2 Mar 2018  Â·  8Comments  Â·  Source: docker/compose

docker-compose.yaml:

version: '3'
services:
  postgres:
    image: postgres:10-alpine
    ports:
      - 5432:5432
    depends_on:
      - redis

  redis:
    image: redis:4-alpine
    ports:
      - 6379:6379

Open a shell using the postgres service.

docker-compose run --rm postgres sh

In another shell, list all containers:

CONTAINER ID        IMAGE                COMMAND                  CREATED              STATUS              PORTS                    NAMES
b60b456d7242        postgres:10-alpine   "docker-entrypoint.s…"   About a minute ago   Up About a minute   5432/tcp                 o_postgres_run_1
5ee1fb5f8942        redis:4-alpine       "docker-entrypoint.s…"   About a minute ago   Up About a minute   0.0.0.0:6379->6379/tcp   o_redis_1

IMHO, docker-compose run should not open ports.

kinquestion

Most helpful comment

@shin- can we reopen this?

The docs for docker-compose run say:

The second difference is that the docker-compose run command does not create any of the ports specified in the service configuration. This prevents port collisions with already-open ports.

So unless I'm using the --service-ports flag, none of my services in docker-compose should expose their ports with run

All 8 comments

This is working as intended. Maybe you are looking to use the --no-deps flag?

I would like to run all dependencies, but I wouldn't like to export the port of theses services. As the docker-compose run has --service-ports to export the ports, I would like something like --no-dependencies-service-ports to disable all dependencies ports.

Hi @shin- , I'd like to know if this feature makes sense to you.

Hi @robsonpeixoto

I'm not sure I understand what the use-case for it is. It also makes for odd, potentially confusing situations where services are started with only part of their configuration, which is not a precedent I want to set if avoidable.

I solved this "problem" using docker-compose.yaml override. But a use case is to use the same docker-compose.yaml to run a development and test environment, but with different project name.

@shin- can we reopen this?

The docs for docker-compose run say:

The second difference is that the docker-compose run command does not create any of the ports specified in the service configuration. This prevents port collisions with already-open ports.

So unless I'm using the --service-ports flag, none of my services in docker-compose should expose their ports with run

I ran into the same thing today. Based on the docs, I expected docker-compose run to prevent any port specified in the service configuration to be forwarded, but it only suppressed forwards for the service provided to run.

My use case is starting a second copy of my multi-container app with a --project-name prefix to run my test suite. I want it to be self-contained and isolated from my dev environment and the multi-container running there, while still using the same service definition file.

@shin- I think the problem is pretty clear here, docker-compose up should start services with deps exposing ports by default, docker-compose run should start services with deps without ports exposed by default unless --service-ports is passed.

Looking at the docs, this seems like a bug to me, what's the argument against this fix?

Was this page helpful?
0 / 5 - 0 ratings