OneBite.Dev - Coding blog in a bite size

docker-compose

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

Let’s dive into a simple yet effective guide on using Docker-compose, a tool used for defining and running multi-container Docker applications.

Understanding Docker-Compose

Firstly, let’s get to know Docker-compose. Docker-compose is a great tool that lets developers describe their multi-service application with YAML files. It allows multiple isolated environments on a single host without any extra work. This guide will take you through essentials steps on how to use Docker-compose.

Prerequisites

Before proceeding, ensure you have Docker and Docker-compose installed in your system. If not, follow the installation guide from Docker’s official documentation.

Step 1: Setting Up a Docker-Compose file

Start with setting up a Docker-compose file, usually named docker-compose.yml. This file is written in YAML format and it’s where you define your multi-container application. Below is a simple example of a Docker-compose file:

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

In this file, two services are defined - a web service and a redis service.

Step 2: Building Services

The next step is building your services. You can do that by running docker-compose build command from your terminal in the directory of your docker-compose file. This command builds the docker containers for the services defined in your docker-compose file.

Step 3: Running Services

Once the Docker containers are built, run the services using docker-compose up command from your terminal. You’ll then see Docker starting your services. These services can interact with each other through pre-defined network rules.

Step 4: Stopping Services

If you want to stop the services, use the docker-compose down command in your terminal. It will stop and remove all the containers defined in your docker-compose file.

Step 5: Adding More Services

You can always add more services or modify existing ones as per your application requirements. Simply update your Docker-compose file and rerun the build and up commands.

Docker-compose is a powerful tool in the hand of developers dealing with multi-container Docker applications. This simple guide should have given you a good starting point in understanding and using Docker-compose. Remember to always refer to official Docker documentation for advanced usage and troubleshooting.

docker