Complete Guide to Containerize Applications with Docker on Linux
In the era of automation and agility, application containerization has become essential for sysadmins and DevOps engineers. This article solves the problem of how to containerize applications efficiently and securely using Docker in a Linux environment.
Introduction to Docker
Docker is a containerization platform that allows you to develop, share, and run applications in isolation. Each Docker container encapsulates an application and all its dependencies, ensuring consistency across different environments.
Installing Docker
Before getting started, you need to install Docker. Here's how to do it on Debian-based and Red Hat-based distributions:
Debian/Ubuntu
# Atualiza o repositório de pacotes
sudo apt update
# Instala dependências
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
# Adiciona a chave GPG oficial do Docker
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
# Adiciona o repositório estável do Docker
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
# Atualiza o repositório de pacotes novamente
sudo apt update
# Instala o Docker
sudo apt install -y docker-ce
CentOS/RHEL
# Instala dependências
sudo yum install -y yum-utils
# Configura o repositório estável do Docker
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
# Instala o Docker
sudo yum install -y docker-ce
Creating a Dockerfile
A Dockerfile is a text script that contains a series of instructions for building a Docker image. Let's create a simple Dockerfile for a Python web application.
# Dockerfile
FROM python:3.8-slim-buster
WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY..
CMD ["python", "app.py"]
Explaining the Dockerfile
FROM: Specifies the base image.WORKDIR: Sets the working directory.COPY: Copies files from the host system to the container.RUN: Executes commands in the container.CMD: Defines the command to run when the container starts.
Building and Running Containers
With the Dockerfile ready, we can build and run our Docker image.
# Constrói a imagem Docker
sudo docker build -t minha-app .
# Executa o contêiner
sudo docker run -d -p 5000:5000 minha-app
Managing Containers
Docker provides commands to manage containers efficiently.
Listing Containers
# Lista todos os contêineres
sudo docker ps -a
Stopping and Removing Containers
# Para um contêiner
sudo docker stop CONTAINER_ID
# Remove um contêiner
sudo docker rm CONTAINER_ID
Why This Matters / When to Use
Containerization with Docker is crucial for ensuring application portability and consistency across different environments. Use Docker when you need to:
- Isolate applications and their dependencies.
- Facilitate deployment across different environments.
- Automate the build and deployment process.
Connecting to LPI/CompTIA Certification Theme
Mastering Docker is essential for obtaining LPI (Linux Professional Institute) and CompTIA certifications in Linux and DevOps. These certifications value not only theoretical knowledge but also the practical skill of implementing and managing containerization technologies.
That was just one piece. The course teaches the whole path — from your first command to exam day.