Welcome back to the #90DaysOfDevOps series! Today marks the second lab on Docker, a powerful tool that simplifies application deployment and management. In this session, we'll get into the practical aspects of containerizing a small application, understanding Docker essentials, and automating deployments using Docker Compose or other straightforward CI/CD tools.
Building a Simple Dockerfile
Let's start by creating a Dockerfile to containerize a small application. For this example, we'll use a basic Python application. Here are the steps:
Step 1: Set Up Your Project Structure
Create a project directory and navigate into it.
mkdir my_docker_app
cd my_docker_app
Step 2: Create the Python Application
Write a simple Python script, let's call it app.py. It could be as straightforward as:
# app.py
print("Hello, Docker!")
Step 3: Write the Dockerfile
Now, create a file named Dockerfile in the same directory:
# 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
# Run app.py when the container launches
CMD ["python", "app.py"]
This Dockerfile starts with the official Python 3.8 image, sets up the working directory, copies your application files into the container, and specifies the command to run your application.
Understanding Docker Basics
Before we proceed, let's revise some fundamental Docker concepts:
Image: A lightweight, standalone, executable package that includes everything needed to run a piece of software, including the code, runtime, libraries, and dependencies.
Container: An instance of a Docker image that runs a software application.
Basic Docker Commands
Now that we have our Dockerfile, let's build and run our Docker image:
# Build the Docker image
docker build -t my_docker_app .
# Run the Docker container
docker run my_docker_app
Congratulations! You've just containerized your first application using Docker.
Automate Deployments with Docker Compose
Docker Compose is a tool for defining and running multi-container Docker applications. It allows you to manage multiple containers as a single service. Here's a simple Docker Compose file for our Python application:
Create docker-compose.yml
version: '3'
services:
my_app:
build: .
ports:
- "5000:5000"
This Docker Compose file defines a service named my_app that builds from the current directory and maps port 5000 on the host to port 5000 on the container.
To run your application using Docker Compose:
docker-compose up
Automate Deployments with CI/CD
Integrating Docker into your CI/CD pipeline is crucial for efficient and consistent deployments. Most CI/CD tools support Docker seamlessly. Below is a simple example using GitHub Actions:
Create .github/workflows/docker.yml
name: Docker CI/CD
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Build Docker image
run: docker build -t my_docker_app .
- name: Push to Docker Hub (replace with your registry)
run: |
echo ${{ secrets.DOCKER_PASSWORD }} | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin
docker tag my_docker_app ${{ secrets.DOCKER_USERNAME }}/my_docker_app
docker push ${{ secrets.DOCKER_USERNAME }}/my_docker_app
This GitHub Actions workflow builds your Docker image and pushes it to a container registry when changes are pushed to the main branch.
Remember to set your Docker Hub credentials as secrets in your GitHub repository.
That's it for Day 81 of #90DaysOfDevOps! You've successfully containerized an application, understood Docker basics, and explored ways to automate deployments. Stay tuned for more hands-on DevOps insights!
*** Explore | Share | Grow ***
Comments