Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker-compose run command doesnt map ports #1259

terbooter opened this issue Apr 7, 2015 · 19 comments

Docker-compose run command doesnt map ports #1259

terbooter opened this issue Apr 7, 2015 · 19 comments


Copy link

@terbooter terbooter commented Apr 7, 2015

Docker version 1.5.0, build a8a31ef
docker-compose 1.1.0


  build: .
    - app:/app
    - redis
    - mongo
    - "881:80"
    - "3000:3000"

  image: redis

  image: mongo

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

2c9e256f934e        nodejsserver_nodejsserver:latest   "/"              11 seconds ago      Up 9 seconds>80/tcp,>3000/tcp   nodejsserver_nodejsserver_1  

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

b6a0c7089d75        nodejsserver_nodejsserver:latest   "/"              About a minute ago   Up About a minute   3000/tcp, 80/tcp      nodejsserver_nodejsserver_run_18
Copy link

@thaJeztah thaJeztah commented Apr 7, 2015

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; #1256 (comment)

Copy link

@terbooter terbooter commented Apr 8, 2015

@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

Copy link

@thaJeztah thaJeztah commented Apr 8, 2015

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])(

docker-compose run --service-ports nodejsserver

Copy link

@terbooter terbooter commented Apr 9, 2015

@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    *               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

Copy link

@aanand aanand commented Apr 9, 2015

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.

Copy link

@cybertk cybertk commented Jun 18, 2015

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

@dnephin dnephin closed this as completed Jun 18, 2015
Copy link

@augnustin augnustin commented Oct 8, 2015

+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

Copy link

@Aeolun Aeolun commented Oct 3, 2017

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.

Copy link

@lucidNTR lucidNTR commented Jan 10, 2018

wasted 4 hours

Copy link

@augnustin augnustin commented Jan 10, 2018

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

Copy link

@t-hofmann t-hofmann commented Feb 20, 2018

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".

Copy link

@jondeandres jondeandres commented Apr 11, 2019

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

Copy link

@gbouthenot gbouthenot commented Jun 4, 2019

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.

Copy link

@oi-eddyoj oi-eddyoj commented Aug 20, 2019

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

Copy link

@danihodovic danihodovic commented Apr 9, 2020

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

This is a sane default. 👍

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

Copy link

@rbreslow rbreslow commented Sep 22, 2020

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.

Copy link

@shayneoneill shayneoneill commented Jun 24, 2021

oh my god. why isn't this behavior explicitly stated. Two days going crazy trying to work out why it doesnt work. Horrible.

Copy link

@thaJeztah thaJeztah commented Jun 24, 2021

oh my god. why isn't this behavior explicitly stated.

Curious; where did you go looking for your problem (so that this can be more explicitly mentioned)? I see it's already mentioned in the reference docs;

Commands you use with run start in new containers with configuration defined by that of the service, including volumes, links, and other details. However, there are two important differences.

First, the command passed by run overrides the command defined in the service configuration. For example, if the web service configuration is started with bash, then docker-compose run web python overrides it with python

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. If you do want the service’s ports to be created and mapped to the host, specify the --service-ports flag:

Copy link

@BoilingSoup BoilingSoup commented Feb 28, 2022

While I appreciate the documentation, I think the default should be to map ports, and maybe use aliases too.
If I didn't want port mappings I would not define the port mappings in the first place.

Or if the default could be configured through docker-compose.yml that would be a good compromise too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
None yet

No branches or pull requests