Should I use docker-compose up or run?

Is there a reason to use run to start up a docker-compose.yml file or should you just use up?

I understand that run can start up a specific container, but I am referring to the case where you use it without specifying a container so that it starts up all of your yml containers.


As mentioned in docker-compose run:

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 the docker-compose run command does not create any of the ports specified in the service configuration. This prevents the port collisions with already open ports. If you do want the service's ports created and mapped to the host, specify the --service-ports flag:

$ docker-compose run --service-ports web python shell

So unless you have those specific needs (overriding a command or running only one container on different ports), docker-compose up (even for one container) is enough.

Can you help explain why or when you would not want the ports to be created? That is why or when they might conflict with already open ports

Simply because docker-compose run is made to run one-off commands for your services. That means that, if you already did a docker-compose up, all your containers are already running on their specified ports from docker-compose.yml. Doing a docker-compose run at this stage (to execute a one-off command), if it was respecting the same port, would fail immediately. Hence the default non-creation of those ports.

Another use case (in Compose environment variables reference):

To see what environment variables are available to a service, run docker-compose run SERVICE env.

I'd like to point out that if you're using Python with the pdb debugger:

import pdb; pdb.set_trace()

It will not drop to the shell if you execute your script using:

docker-compose up

However if you use run, it will drop down to the debugger as expected:

docker-compose run

Need Your Help

Simple Android grid example using RecyclerView with GridLayoutManager (like the old GridView)

android gridview android-recyclerview gridlayoutmanager

I know that RecyclerView has replaced the functionality of the old ListView and GridView. I am looking for a very basic example that shows a minimal grid setup using RecyclerView. I am not looking ...

Where to save a JWT in a browser-based application and how to use it

javascript web-services security cookies jwt

I'm trying to implement JWT in my authentication system and I have a few questions. To store the token, I could use cookies but it's also possible to use localStorage or sessionStorage.