Introduction

Docker has revolutionized software development by making it easier to create, deploy, and manage applications in a consistent environment. Whether you are a developer, DevOps engineer, or IT professional, understanding the basics of Docker is essential for streamlining workflows and ensuring efficient application deployment.

In this guide, we’ll cover the fundamentals of Docker, how to install it, and how to get started with containerisation.

What is Docker?

Docker is an open-source platform that allows developers to automate the deployment of applications inside lightweight, portable containers. Unlike traditional virtual machines (VMs), Docker containers share the host OS kernel, making them faster and more efficient.

Benefits of Using Docker

  • Portability: Containers can run consistently across different environments.
  • Efficiency: Uses fewer system resources compared to VMs.
  • Scalability: Easily scale applications by adding or removing containers.
  • Isolation: Keeps applications and dependencies separate from the host system.

Installing Docker

Before using Docker, you need to install it on your system. Here’s how:

Windows & macOS

  1. Download Docker Desktop from the official Docker website.
  2. Run the installer and follow the setup instructions.
  3. Once installed, verify the installation by running:

Linux (Ubuntu/Debian)

  1. Update the package list:sudo apt update
  2. Install Docker:sudo apt install docker.io -y
  3. Start Docker and enable it to run on boot:sudo systemctl start docker sudo systemctl enable docker
  4. Verify installation:docker --version

Basic Docker Commands

1. Checking Docker Installation

To check if Docker is running correctly, execute:

docker run hello-world

This command downloads and runs a test container.

2. Pulling an Image

Docker images serve as blueprints for containers. To download an official image, such as Nginx, use:

docker pull nginx

3. Running a Container

To start a container from an image:

docker run -d -p 8080:80 nginx
  • -d: Runs the container in detached mode.
  • -p 8080:80: Maps port 80 of the container to port 8080 on the host.

4. Listing Running Containers

View active containers using:

docker ps

To see all containers (including stopped ones):

docker ps -a

5. Stopping and Removing Containers

To stop a running container:

docker stop <container_id>

To remove a container:

docker rm <container_id>

Dockerfile: Automating Container Creation

A Dockerfile is a script that defines how a Docker image should be built. Below is a simple example:

# Use an official Python image
FROM python:3.9

# Set the working directory
WORKDIR /app

# Copy application files
COPY . /app

# Install dependencies
RUN pip install -r requirements.txt

# Define the command to run the app
CMD ["python", "app.py"]

To build and run a container using this Dockerfile:

docker build -t my-python-app .
docker run -d my-python-app

Managing Docker Containers with Docker Compose

Docker Compose simplifies the management of multi-container applications. Below is an example docker-compose.yml file:

version: '3'
services:
  web:
    image: nginx
    ports:
      - "8080:80"

To run the application, use:

docker-compose up -d

Conclusion

Docker is a powerful tool that simplifies software deployment by using containers. By understanding its basic commands, images, and container management, you can efficiently build and deploy applications. As you progress, exploring advanced features like Docker networking and volumes can further enhance your development workflow.