14 docker-compose
14.1 Docker compose to launch containers
It can be fiddly and error prone to type long commands into the terminal, or a GUI every time you want to launch a container. Even if you are copy and pasting commands, it can be difficult to make changes to long commands because you cannot click to a location to make edits.
A better approach is to use Docker compose to specify how you want to launch the container. This way all the parts of your command are saved in a file. This helps document how to run the docker image, and can be version controled part of a project.
Docker compose uses a YAML file, specifically named docker-compose.yml, to record how the container should be launched. This file can include details including:
- the docker image and version to use
- how to mount volumes
- what ports to map
- what environment variables to set.
docker run commands are interchangeable the parameters specified in docker-compose.yml; the compose file makes it easier to manage all the docker run parameters. See this resource that can convert a run command to a compose file: https://www.composerize.com/
Here is an example comparing docker run with a docker-compose.yml file for use with the rocker/rstudio:4.4.2 container image.
The docker run command as a single line: docker run --rm -it -p 8787:8787 -v /$(pwd):/home/rstudio/project -e PASSWORD="apassword" rocker/rstudio:4.4.2
as a multi line command:
docker run \
--rm \
-it \
-p 8787:8787 \
-v /$(pwd):/home/rstudio/project \
-e PASSWORD="apassword" \
rocker/rstudio:4.4.2and within a docker-compose.yml file:
services:
analysis-env: # name of the service we are creating
image: rocker/rstudio:4.4.2
ports:
- "8787:8787"
volumes:
- .:/home/rstudio/project
environment:
PASSWORD: apasswordTo launch the container interactively using this file, you use the docker-compose command to run the container(s) and parameters specified in the yaml file.
docker-compose upIf you are using a web app, as in the case of the rocker/rstudio or jupyter/minimal-notebook container images, you still need to manually navigate to the web app in your browser and enter the correct URL to access it.
To stop and clean up the container, you would use Ctrl + c in the terminal where you launched the container, and then type
docker-compose downLetβs take a look at an example docker-compose.yml being used in a project: - https://github.com/ttimbers/breast_cancer_predictor_py
14.2 Running a Docker container non-interactively using the Docker Compose
We can also use Docker Compose to run containers non-interactively! We can do this by specifying that we want to run the container (instead of up to launch in interactively). We use the --rm flag with the run command to make the container ephemeral (delete it upon exit). Then we specify the name of the service from the docker-compose.yml file that we want to run (in our docker-compose.yml files so far we only have one service, the environment for running the analysis). And finally we add the command we want to run non-interactively using the container (in the example below we use make to run the data analysis pipeline script).
docker-compose run --rm analysis-env make all