top of page
Writer's picturevP

Lab 9 -Setting up a Database with Docker Compose - Day 82

Welcome back to the #90DaysOfDevOps series! Today we will be discussing about Docker Compose. It is a powerful tool that allows you to define and manage multi-container Docker applications. In this tutorial, we'll use Docker Compose to set up a simple environment with a web application and a MySQL database. By the end of this guide, you'll have a basic understanding of how to use Docker Compose for container orchestration.


Prerequisites

Docker and Docker Compose:

Ensure that Docker and Docker Compose are installed on your system. You can download and install them from the official Docker website:

Step 1: Project Structure

Create a new directory for your project and navigate into it:

mkdir docker-compose-demo
cd docker-compose-demo

Step 2: Create Docker Compose File

Create a file named docker-compose.yml in your project directory. This file will define the services, networks, and volumes required for your application.

version: '3'
services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"
  db:
    image: mysql:latest
    environment:
      MYSQL_ROOT_PASSWORD: root_password
      MYSQL_DATABASE: sample_db
      MYSQL_USER: db_user
      MYSQL_PASSWORD: db_password

In this example:

  • We define two services: web and db.

  • The web service uses the official Nginx image and maps port 8080 on the host to port 80 in the container.

  • The db service uses the official MySQL image and sets up environment variables for root password, database name, and user credentials.


Step 3: Start the Containers

Run the following command in your project directory to start the containers defined in the docker-compose.yml file:

docker-compose up -d

This command will download the necessary images (if not already downloaded) and start the containers in the background.


Step 4: Verify the Setup

Visit http://localhost:8080 in your web browser. You should see the default Nginx welcome page, indicating that the web container is running.


Step 5: Connect to the Database

Use a MySQL client or command-line tool to connect to the MySQL database. You can use the following credentials:

  • Host: localhost (or 127.0.0.1)

  • Port: 3306

  • Username: db_user

  • Password: db_password

  • Database: sample_db


Step 6: Stop and Cleanup

When you're done experimenting, stop the containers using the following command:

docker-compose down

This command stops and removes the containers, networks, and volumes defined in the docker-compose.yml file.


Congratulations! You've successfully set up a simple two-container environment with a web application and a MySQL database using Docker Compose. This example serves as a foundation for more complex multi-container applications and demonstrates the ease of container orchestration with Docker Compose.


Thank you for reading!


*** Explore | Share | Grow ***

14 views0 comments

Kommentarer

Betygsatt till 0 av 5 stjärnor.
Inga omdömen ännu

Lägg till ett betyg
bottom of page