Welcome back to the #90DaysOfDevOps series! Today in Lab 13, we'll explore deploying a simple application using Docker Compose on a local Kubernetes cluster. This practical guide will walk you through the steps to create a Docker file, requirement file, and Docker image for a multi-container application.
Setting the Stage
Before we begin, let's ensure you have Docker and Kubernetes installed on your local machine. If you haven't done this yet, head over to the Docker and Kubernetes official websites and follow the installation guides for your operating system.
Creating the Docker File
The first step in our journey is to create a Docker file. This file defines the environment for your application. Open your favorite text editor and create a file named Dockerfile. Here's a simple example for a Python-based application:
# Use an official Python runtime as a parent image
FROM python:3.8-slim
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME World
# Run app.py when the container launches
CMD ["python", "app.py"]
In this example, replace app.py with the main file of your application and ensure you have a requirements.txt file listing your project dependencies.
Creating the Docker Compose File
Now, let's create a docker-compose.yml file. This file defines how your application's services should run, scale, and connect. Here's a basic example:
version: '3'
services:
web:
build: .
ports:
- "5000:5000"
redis:
image: "redis:alpine"
This simple docker-compose.yml file describes two services: web and redis. The web service is built using the current directory (where your Docker file is located), and it exposes port 5000.
Building the Docker Image
Navigate to the directory containing your Dockerfile and docker-compose.yml. Open a terminal and run the following command to build your Docker image:
docker-compose build
This command reads the instructions from your Dockerfile and creates an image based on those instructions.
Running Your Multi-Container Application
Now that your Docker image is built, it's time to run your multi-container application. Execute the following command:
docker-compose up
This command starts the services defined in your docker-compose.yml file. You should see output indicating that your application is running.
Visit http://localhost:5000 in your web browser, and you should see your application in action!
Congratulations! You've successfully deployed a multi-container application using Docker Compose on a local Kubernetes cluster. This hands-on experience is invaluable for understanding how containerized applications work and interact.
Stay tuned for more labs in this series. Until then, keep exploring and experimenting with DevOps tools and practices.
Happy coding!
*** Explore | Share | Grow ***
Comments