OneBite.Dev - Coding blog in a bite size

run docker compose

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

Docker Compose is a tool that simplifies the process of managing multi-container Docker applications. Here’s a simple guide on how to run docker compose.

Introduction

Running a Docker Compose file is as simple as it can be. First, create a docker-compose.yml file, define your services and then run the compose file.

Prerequisite:

Before we begin, ensure you have Docker and Docker Compose installed in your system. The installation process varies based on the operating system you use.

Step 1: Docker Compose File

You need to create a docker-compose.yml file, in which you will list all the services to run your application. The docker compose file begins with the version number of the Docker Compose followed by a services keyword where we define all services.

Here is a sample docker-compose.yml file:

version: '3'
services:
  web:
    build: .
    ports:
      - "5000:5000"
  redis:
    image: "redis:alpine"

In this example, the ‘web’ is a service built from the current directory and ‘redis’ is another service using an existing Redis image.

Step 2: Build Services

Once you have your docker-compose.yml file in place, you can ask Docker Compose to start and build all the services defined in the file. You can do that by running a simple command:

docker-compose up

This command will build the docker image for the services if not already built and then start the services.

If docker is not able to find the image locally, it will download from the Docker Hub.

Step 3: Verify the Services

After you execute the command, Docker Compose should start all the services defined in the file. To verify that all the services have been started correctly, open a web browser and use the localhost using the port number for the service. For the web service as defined in the example, the URL would be http://localhost:5000.

Step 4: Stop the Services:

Once you have finished, you can stop the running services using the down command.

docker-compose down

This command will stop and remove the containers, networks, volumes, and images defined in the docker-compose.yml.

The process to run Docker Compose is quite simple and straightforward. With Docker Compose, managing multi-container Docker applications becomes easier and more organized.

docker