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 updatesudo apt install docker.iosudo systemctl start docker
sudo systemctl enable dockerRun the following command to check if Docker is installed correctly:
docker --versionYou should see the installed Docker version.
Run the hello-world container to verify Docker is working:
docker run hello-worldThis 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 ubuntudocker pull ubuntudocker imagesUse an image to create and run a container:
docker run -it ubuntuThis command starts a container interactively with the Ubuntu image. You can now run commands inside the container.
Common container management commands include:
docker psdocker ps -adocker stop container_iddocker rm container_idA 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-appDocker Compose simplifies multi-container applications:
version: '3'
services:
web:
image: nginx
ports:
- "80:80"
app:
build: .
depends_on:
- db
db:
image: postgresExplore 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.