Docker is a powerful platform that enables developers to create, deploy, and manage applications inside lightweight, portable containers. Containers ensure that applications run consistently across different environments. In this tutorial, we’ll walk through Docker basics.
Install Docker on your system by following these steps:
sudo apt update
sudo apt install docker.io
sudo systemctl start docker
sudo systemctl enable docker
Run the following command to check if Docker is installed correctly:
docker --version
You should see the installed Docker version.
Run the hello-world container to verify Docker is working:
docker run hello-world
This command downloads and runs a small test container, printing a welcome message.
Docker images are templates used to create containers. Common commands include:
docker search ubuntu
docker pull ubuntu
docker images
Use an image to create and run a container:
docker run -it ubuntu
This command starts a container interactively with the Ubuntu image. You can now run commands inside the container.
Common container management commands include:
docker ps
docker ps -a
docker stop container_id
docker rm container_id
A Dockerfile automates the creation of custom images. Example:
# Use an official Python runtime as a parent image
FROM python:3.9-slim
# Set the working directory
WORKDIR /app
# Copy the current directory contents into the container
COPY . /app
# Install any needed packages
RUN pip install -r requirements.txt
# Run the application
CMD ["python", "app.py"]
Build an image from a Dockerfile:
docker build -t my-python-app .
Run a container from the custom image:
docker run my-python-app
Docker Compose simplifies multi-container applications:
version: '3'
services:
web:
image: nginx
ports:
- "80:80"
app:
build: .
depends_on:
- db
db:
image: postgres
Explore Docker Hub for pre-built images, experiment with Docker Compose, and learn about orchestration tools like Kubernetes. Docker is a foundational tool for modern DevOps practices.