Compose: Docker-compose run command doesnt map ports

Created on 7 Apr 2015  路  16Comments  路  Source: docker/compose

Docker version 1.5.0, build a8a31ef
docker-compose 1.1.0

docker-compose.yml

nodejsserver:
  build: .
  volumes:
    - app:/app
  links:
    - redis
    - mongo
  ports:
    - "881:80"
    - "3000:3000"
  environment:
    - DOCKER_ENVIRONMENT

redis:
  image: redis

mongo:
  image: mongo

Command "docker-compose up" works as expected (volumes mounted, containers linked, portes mapped)

2c9e256f934e        nodejsserver_nodejsserver:latest   "/run.sh"              11 seconds ago      Up 9 seconds        0.0.0.0:881->80/tcp, 0.0.0.0:3000->3000/tcp   nodejsserver_nodejsserver_1  

But command "docker-compose run nodejsserver" doesnt map ports (volumes mounted, containers linked)

b6a0c7089d75        nodejsserver_nodejsserver:latest   "/run.sh"              About a minute ago   Up About a minute   3000/tcp, 80/tcp      nodejsserver_nodejsserver_run_18

Most helpful comment

docker-compose run is to execute a one-off command on a container and to _prevent_ conflicts, it will (by default) not map the ports.

For example;

# start your application (composition)
# this will expose ports 881 and 3000
docker-compose up

# then run a on-off command;
docker-compose run nodejsserver do-some-magic-here

In the above example, the docker-compose run command would have conflicted with your running application.

If you _do_ need the ports, use the --service-ports flag (see the [documentation])(https://docs.docker.com/compose/cli/#run)

docker-compose run --service-ports nodejsserver

All 16 comments

Did you use docker-compose run or docker-compose up? (The title of this issue mentions 'run', but your example 'up').

If you're using docker-compose run, then this is by design; https://github.com/docker/compose/issues/1256#issuecomment-90135857

@thaJeztah Sorry it was missprint. I updated ticket text.
Want to add that desired ports (881 and 3000) on host machine are free. So according to the docs

by default no ports will be created in case they collide with already opened ports

docker-compose run should map ports from host to container

docker-compose run is to execute a one-off command on a container and to _prevent_ conflicts, it will (by default) not map the ports.

For example;

# start your application (composition)
# this will expose ports 881 and 3000
docker-compose up

# then run a on-off command;
docker-compose run nodejsserver do-some-magic-here

In the above example, the docker-compose run command would have conflicted with your running application.

If you _do_ need the ports, use the --service-ports flag (see the [documentation])(https://docs.docker.com/compose/cli/#run)

docker-compose run --service-ports nodejsserver

@thaJeztah yes I can use --service-ports flag for run command
But I think that in my case run command should map ports.
Docs says that

One-off commands are started in new containers with the same configuration as a normal container for that service, so volumes, links, etc will all be created as expected.

In other words docs says that if required ports not collide with host machine ports this ports should be mapped
I am pretty shure that desired ports are free.
I made compose stop and rm before run

root@local:/vagrant/nodejs-server# docker-compose stop
Stopping nodejsserver_nodejsserver_1...
Stopping nodejsserver_mongo_1...
Stopping nodejsserver_redis_1...
root@local:/vagrant/nodejs-server# docker-compose rm
Going to remove nodejsserver_nodejsserver_1, nodejsserver_mongo_1, nodejsserver_redis_1
Are you sure? [yN] y
Removing nodejsserver_redis_1...
Removing nodejsserver_mongo_1...
Removing nodejsserver_nodejsserver_1...
root@local:/vagrant/nodejs-server# docker-compose run nodejsserver
Creating nodejsserver_redis_1...
Creating nodejsserver_mongo_1...
....

Also no other process use target ports

root@local:/vagrant/nodejs-server# netstat -ntlp | grep 3000
root@local:/vagrant/nodejs-server# netstat -ntlp | grep 881
root@local:/vagrant/nodejs-server# netstat -ntlp | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1137/nginx      
tcp6       0      0 :::802                  :::*                    LISTEN      5737/docker-proxy

So I think that run command in this case should map ports automatically without --service-ports flag

We could check to see if the ports are in use, but that'd involve another API call. It would also make docker-compose run do different things based on the state of the whole Docker host, which makes me nervous.

docker-compose run --service-ports works like a charm

+1 for docker-compose run --service-ports.

Really useful for setting up the machine and making many changes with docker-compose run web --service-ports /bin/bash

This is really terribly annoying.

Maybe add a notification that it's not mapping ports or something please, I'm wasting hours of my time trying to figure out why something that has every appearance of working doesn't actually work.

wasted 4 hours

I'd even be in favor of making it default. Or even better, open the ports on the first instance and not in the next ones (but this might not make sense though).

As far as I'm concerned, I installed the following aliases:

alias docker-enter="docker-compose run --rm  --service-ports app /bin/bash"
alias docker-enter-again="docker-compose run --rm  app /bin/bash"

More info

I also struggled with the ports not being forwarded by default, when "run" is used. This feels like a UI bug, since the underlying configuration (docker-compose.yml) configures such port mappings. And it is far from obvious that the configuration is only partly used if a service is started via "run" instead of "up" or "start".

It is always possible to print errors if required ports are not available. I would favor to reverse the logic and add an option like "--no-port-mappings" or "--not-mapped-ports X Y Z".

Same problem here. We need docker-compose run instead up because the debugger with up doesn't work fine when it stops the process in a breakpoint but it works ok with run. However we want at same time to map the ports. Need to pass --service-ports everytime doesn't look natural

Same for me. Spent 1 hour. This seems strange to issue an option to take in account something specified in the configuration file. Why ports aren't binded, why env vars is honored ?
I vote in favor of enabling the port binding by default, and have a "--no-service-ports" arguments.

It makes sense for us to choose to be able to add a port. Docker-compose up is too slow for development mode

I vote in favor of enabling the port binding by default, and have a "--no-service-ports" arguments.

This is a sane default. :+1:

Not exposing ports trips me anytime I need to run an interactive debugger.

This isn't a great default. What happens if I want to run parallel tests in CI and I don't want ports to be exposed? Right now, the use of docker-compose run for one-off tasks, and docker-compose up for an actual service that is accessible makes perfect sense.

Was this page helpful?
0 / 5 - 0 ratings