# 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.

└── index.html


<!doctype html>
<html class="no-js" lang="en">
    <meta charset="utf-8">
    <meta name="description" content="docker-run-to-compose-demo">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <p>Hello world! This is docker-run-to-compose-demo.</p>

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)

# 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)

# 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"
    image: httpd:2.4
      - "8080:80"
      - ${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)

  # Name of the service ("serve") that serves HTML site

    # Use httpd:2.4 image
    image: httpd:2.4

    # Make site accessible at http://localhost:8080/
      - "8080:80"

    # Mount current /src directory to /usr/local/apache2/htdocs/ directory in the container
      - ${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


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.