# Converting a Docker Run Command to Docker Compose
# Introduction
Docker Compose provides a convenient and readable way to configure and run Docker containers. This guide will teach you how to convert an existing Docker run command into Docker Compose.
See docker-run-to-compose-demo for a demo of this guide.
# Prerequisites
- You have Docker installed on your machine
- You have experience using the Docker run command
- You have a basic understanding of Docker Compose
# Step 1—Understanding the Docker run Command
In this guide, we'll convert the following command into Docker compose.
docker run -dit --name docker-run-to-compose-demo -p 8080:80 --mount type=bind,src="${PWD}/src",dst=/usr/local/apache2/htdocs/ httpd:2.4
The command runs a local HTML website contained in the /src
directory where the command is run.
src/
└── index.html
src/index.html
<!doctype html>
<html class="no-js" lang="en">
<head>
<meta charset="utf-8">
<title>docker-run-to-compose-demo</title>
<meta name="description" content="docker-run-to-compose-demo">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<p>Hello world! This is docker-run-to-compose-demo.</p>
</body>
</html>
After running the command and visiting http://localhost:8080, you should see:
Hello world! This is docker-run-to-compose-demo.
Before converting the command, let's first understand each part.
# Runs the container
docker run
# detached, interactive, and tty flags (see https://docs.docker.com/engine/reference/commandline/run/#options for more info)
-dit
# Sets container name to "docker-run-to-compose-demo"
--name docker-run-to-compose-demo
# Makes site accessible at http://localhost:8080
-p 8080:80
# Mounts current /src directory to /usr/local/apache2/htdocs/ directory in the container
--mount type=bind,src="${PWD}/src",dst=/usr/local/apache2/htdocs/
# Uses httpd:2.4 image (https://hub.docker.com/_/httpd)
httpd:2.4
# Step 2—Creating the docker-compose.yml File
Now that we understand the Docker run command, let's create a docker-compose.yml
file.
touch docker-compose.yml
After creating the file, add the following code:
version: "3"
services:
serve:
image: httpd:2.4
ports:
- "8080:80"
volumes:
- ${PWD}/src:/usr/local/apache2/htdocs/
The code should look surprisingly similar to the above run command. To understand it better, let's break it down line-by-line.
# Latest Docker Compose version
version: "3"
# List of Docker container services to run (in this case only one)
services:
# Name of the service ("serve") that serves HTML site
serve:
# Use httpd:2.4 image
image: httpd:2.4
# Make site accessible at http://localhost:8080/
ports:
- "8080:80"
# Mount current /src directory to /usr/local/apache2/htdocs/ directory in the container
volumes:
- ${PWD}/src:/usr/local/apache2/htdocs/
# Step 3—Running the docker-compose Command
Now it's time to serve up the site by running the following command:
docker-compose up -d
TIP
Notice that we've added the -d
flag to the command but not the -it
flags. This is because by default docker-compose up
runs the container with those flags.
After running the command and visiting http://localhost:8080, you should see:
Hello world! This is docker-run-to-compose-demo.