OneBite.Dev - Coding blog in a bite size

run docker compose file

Simple docker step by step how to run docker compose file with explanation

Running a Docker Compose file is a straightforward process that is useful for the deployment of multi-container applications. This article will guide you through the process in a step-by-step approach.

To start off, Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. This makes it easier to configure, launch, and manage service-oriented stacks.

Prerequisites

Before we get started, it’s necessary to ensure that the following are installed on your local machine or server:

  • Docker Engine: Compose relies on Docker Engine for any meaningful work.
  • Docker Compose: If not already installed, you can download Docker Compose here: https://docs.docker.com/compose/install/.

Note: Both Docker and Docker Compose versions should be the latest stable releases.

Step 1: Creating the Docker Compose File

Create a new file called docker-compose.yml using your text editor of choice. All of the application’s services will be configured in this file.

Step 2: Configuring the Services

In the docker-compose.yml file, start defining your application’s services. Here, each service represents a separate container. For instance, a simple Python web app would look something like this:

version: '3'
services:
  web:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
     - "5000:5000"

This configuration creates a single service named “web” that is built using the Dockerfile in the current directory and is accessible on port 5000 on the host machine.

Step 3: Building and Running Your Docker Compose File

After defining all services, save the docker-compose.yml file. In your terminal, navigate to the directory containing your YAML file and run the following command:

docker-compose up

This command will download any necessary images, build your services, and start them up. This could take a few moments depending on the complexity of your services.

Step 4: Checking Your Services

When all services are running, open a web browser and go to http://localhost:5000 (or whichever port you defined). You should see your application running.

Step 5: Stopping Your Services

Once you’re finished, you can stop your services by simply typing CTRL + C in your terminal or the following command in the terminal:

docker-compose down

Running a Docker Compose file can be an efficient way to deploy multi-container applications. Following this guide, you should now be able to create, run, and manage your services using Docker Compose.

docker