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)
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;
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
works like a charmThis 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
ordocker-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-90135857wasted 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
docker-compose run
should map ports from host to containerSame problem here. We need
docker-compose run
insteadup
because the debugger withup
doesn’t work fine when it stops the process in a breakpoint but it works ok withrun
. 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 forrun
command But I think that in my caserun
command should map ports. Docs says thatIn 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
andrm
beforerun
Also no other process use target ports
So I think that
run
command in this case should map ports automatically without--service-ports
flagoh 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.
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/