The goal of this tutorial is to guide you through the process of containerizing applications as part of DevOps workflows.
By the end of this tutorial, you should be able to:
- Understand the basics of containerization
- Automate build processes
- Test within containers
- Deploy containerized applications.
Prerequisites:
- Basic knowledge of DevOps practices
- Familiarity with Docker and Kubernetes
- Basic understanding of programming languages (preferably Python)
Containerization involves packaging an application with its runtime dependencies in a 'container', which can be deployed uniformly across different environments. Docker is one of the most commonly used platforms for creating containers.
Step 1: Install Docker
First, we need to install Docker. You can download it from the official Docker website. After installation, verify it with:
docker --version
Step 2: Create a Dockerfile
A Dockerfile is a text file that contains instructions on how to build a Docker image. Here's an example of a Dockerfile for a simple Python application:
# Use an official Python runtime as a base image
FROM python:3.7-slim
# Set the working directory in the container to /app
WORKDIR /app
# Add the current directory contents into the container at /app
ADD . /app
# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 80
# Run app.py when the container launches
CMD ["python", "app.py"]
Step 3: Build the Docker Image
Once you've created the Dockerfile, you can build the image using the docker build
command.
docker build -t my-python-app .
Step 4: Run the Docker Container
Next, you can run the container with the docker run
command.
docker run -p 4000:80 my-python-app
Let's consider two examples:
Example 1: Hello World Python Application
Here's a simple Python application:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
You can containerize this application using the Dockerfile in the step-by-step guide above.
Example 2: Testing within Containers
You can also run tests within containers. Here's an example using pytest
:
# Use an official Python runtime as a base image
FROM python:3.7-slim
# Set the working directory in the container to /app
WORKDIR /app
# Add the current directory contents into the container at /app
ADD . /app
# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
# Run tests
RUN pytest
# Run app.py when the container launches
CMD ["python", "app.py"]
In this Dockerfile, we added a RUN pytest
line that runs tests before launching the application.
In this tutorial, we've covered the basics of containerizing applications for DevOps workflows. We've learned how to create a Dockerfile, build a Docker image, and run a Docker container. We also looked at how to run tests within containers.
Next steps for learning could include:
- Exploring other Docker commands
- Learning about Docker Compose
- Learning about Kubernetes for managing larger systems of containers.
Exercise 1: Containerize a simple "Hello, World!" application in a language of your choice.
Exercise 2: Add a test to the application and modify the Dockerfile to run the test before launching the application.
Exercise 3: Create a Docker Compose file to run your application along with a database in separate containers.
Here are solutions and explanations for these exercises:
Solution 1: You can follow the step-by-step guide above to containerize a "Hello, World!" application. The Dockerfile will look similar, but the CMD
line will depend on the language and framework you're using.
Solution 2: You can add a RUN
line to your Dockerfile with the command that runs your tests. For example, if you're using Python's unittest
module, you might add RUN python -m unittest discover
.
Solution 3: A Docker Compose file might look something like this:
version: '3'
services:
web:
build: .
ports:
- "5000:5000"
db:
image: "mysql:5.7"
environment:
MYSQL_ROOT_PASSWORD: password
In this file, we define two services: web
for our application and db
for a MySQL database.