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

Closed
terbooter opened this issue Apr 7, 2015 · 14 comments
Closed

Docker-compose run command doesnt map ports #1259

terbooter opened this issue Apr 7, 2015 · 14 comments

Comments

@terbooter
Copy link

@terbooter terbooter commented Apr 7, 2015

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

This comment has been minimized.

Copy link
Member

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

@terbooter

This comment has been minimized.

Copy link
Author

@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

@thaJeztah

This comment has been minimized.

Copy link
Member

@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])(https://docs.docker.com/compose/cli/#run)

docker-compose run --service-ports nodejsserver
@terbooter

This comment has been minimized.

Copy link
Author

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

@aanand

This comment has been minimized.

Copy link
Contributor

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

@cybertk

This comment has been minimized.

Copy link

@cybertk cybertk commented Jun 18, 2015

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

@dnephin dnephin closed this Jun 18, 2015
@augnustin

This comment has been minimized.

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

@Aeolun

This comment has been minimized.

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.

@lucidNTR

This comment has been minimized.

Copy link

@lucidNTR lucidNTR commented Jan 10, 2018

wasted 4 hours

@augnustin

This comment has been minimized.

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

@t-hofmann

This comment has been minimized.

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

@jondeandres

This comment has been minimized.

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

@gbouthenot

This comment has been minimized.

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.

@oi-eddyoj

This comment has been minimized.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
You can’t perform that action at this time.