Analytics Vidhya

Analytics Vidhya is a community of Generative AI and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Follow publication

Member-only story

How to understand the difference between docker-compose’s up vs run vs exec commands

Problem

You are starting to use containers and noticed Docker Compose offers multiple ways of running containers, namely up, run, and exec. What is the difference between each and when would you want to use one over the other?

Solution

Majority of the time, you will most likely want to bring up all of the services listed in your docker-compose.yml and have the containers run their default command, so you would want to use up.

There will be times when you need to run a one-off process/task to support your application. This is when you might need run or exec.

The run command will spin up a new container for you to use. The exec command will allow you to use a container that is already running.

Personally, I always try to aim to use run and use exec only if absolutely necessary. Being able to use run means the containers are stateless and/or configured to persist and share data appropriately using volumes.

It might be necessary to use exec when the container is running multiple services. Though, it is recommended to split that container’s services into separate containers.

The rest of this tutorial will go through using the 3 different commands and seeing their different behaviors.

Up vs Run vs Exec

Front Matter

This tutorial will follow from the code of this tutorial:

Here is the relevant code from that tutorial, pasted here for convenience:

# docker-compose.yml
version: '3'
services:
app:
build: .
ports:
- "8081:8080"
dependent-service1:
image: tomcat:9.0.12
volumes:
- ./index.dependent-service1.html:/usr/local/tomcat/webapps/ROOT/index.html
dependent-service2:
image: tomcat:9.0.12
volumes:

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Analytics Vidhya
Analytics Vidhya

Published in Analytics Vidhya

Analytics Vidhya is a community of Generative AI and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Zhao Li
Zhao Li

Responses (4)

Write a response