compose: Docker-compose run command doesnt map ports

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

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 1
  • Comments: 20 (3 by maintainers)

Commits related to this issue

Most upvoted comments

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

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

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.

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

wasted 4 hours

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

@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

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

@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

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

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.

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; https://docs.docker.com/compose/reference/run/

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 app.py overrides it with python app.py.

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: