Introduction to Docker on Linux

Docker has transformed the landscape of software development by allowing developers to automate the deployment of applications in lightweight, portable containers. Rather than worrying about environment discrepancies, developers can encapsulate their apps and dependencies into containers that can run consistently across any system. For Linux users, Docker offers an efficient way to utilize the operating system’s capabilities to manage and deploy containerized applications with ease. In this article, we’ll cover the fundamentals of Docker, including containers, images, and how to get started with Docker on your Linux setup.

What is Docker?

At its core, Docker is a platform designed to make it easier to create, deploy, and run applications in containers. Containers are lightweight, executable units that package an application with all the necessary dependencies, configurations, and libraries needed to run efficiently. Unlike traditional virtual machines (VMs), which require a hypervisor and an entire OS instance, Docker containers share the host system’s kernel, making them more resource-efficient and faster to boot.

Key Concepts: Containers vs. Images

Before diving into Docker commands, it’s essential to understand two fundamental concepts: images and containers.

  • Images: An image is a read-only template used to create containers. It contains the application code and all its dependencies, as well as the instructions to run the application. Images are built from a file known as a Dockerfile.

  • Containers: Containers are instances of Docker images. When you run an image, you create a container that is an isolated environment where your application runs. Containers can be started, stopped, and deleted while preserving their isolated state.

Getting Started with Docker on Linux

Now that we have a basic understanding of Docker and its key components, let’s explore how to set it up on a Linux system and start using it.

Step 1: Prerequisites

Ensure you have a Linux distribution installed. Docker supports various distributions, including Ubuntu, CentOS, Fedora, and Debian. You’ll also need to have root or sudo privileges to install Docker.

Step 2: Install Docker

Installing Docker on Linux depends on your distribution. Below are the installation instructions for some popular Linux distributions.

Ubuntu

  1. Update the package database:

    sudo apt update
    
  2. Install curl and necessary dependencies:

    sudo apt install apt-transport-https ca-certificates curl software-properties-common
    
  3. Add the Docker GPG key:

    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
    
  4. Add the Docker repository:

    sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
    
  5. Install Docker:

    sudo apt update
    sudo apt install docker-ce
    
  6. Start and enable Docker:

    sudo systemctl start docker
    sudo systemctl enable docker
    

CentOS

  1. Remove older versions:

    sudo yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-selinux docker-engine-selinux docker-engine
    
  2. Install required packages:

    sudo yum install -y yum-utils device-mapper-persistent-data lvm2
    
  3. Set up the stable repository:

    sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
    
  4. Install Docker:

    sudo yum install docker-ce
    
  5. Start Docker:

    sudo systemctl start docker
    sudo systemctl enable docker
    

After installation, you can check if Docker is running correctly using:

sudo docker run hello-world

Step 3: Basic Docker Commands

Once Docker is installed, familiarize yourself with some basic commands that are vital for managing images and containers.

  • Check Docker version:

    docker --version
    
  • List Docker images:

    docker images
    
  • Pull an image from Docker Hub: To download an image from Docker Hub (the default repository for Docker images):

    docker pull ubuntu
    
  • List running containers:

    docker ps
    
  • Run a container: To create and start a new container using an image:

    docker run -it ubuntu /bin/bash
    

    Here, -it allows you to interact with the shell of the container.

  • Stop a running container:

    docker stop <container_id>
    
  • Remove a container:

    docker rm <container_id>
    

Step 4: Creating Your Own Docker Image

Creating custom images is a core feature of Docker. To create your image, you’ll need a Dockerfile, which contains a set of instructions for building a Docker image. Here's a simple example.

  1. Create a directory for your Docker project:

    mkdir my-docker-app
    cd my-docker-app
    
  2. Create a Dockerfile:

    nano Dockerfile
    
  3. Add the following instructions:

    FROM ubuntu:latest
    RUN apt-get update && apt-get install -y python3 python3-pip
    COPY . /app
    WORKDIR /app
    CMD ["python3", "app.py"]
    

    This simple Dockerfile pulls the latest Ubuntu image, installs Python and Pip, copies the current directory’s contents into the image, and sets the command to run app.py.

  4. Build your image:

    docker build -t my-python-app .
    
  5. Run your custom container:

    docker run my-python-app
    

Step 5: Managing Docker Containers

Docker provides tools for monitoring and managing your containers effectively. Use the following commands to manage them:

  • View logs for a container:

    docker logs <container_id>
    
  • View running processes in a container:

    docker exec -it <container_id> /bin/bash
    
  • Remove unused images and containers:

    docker system prune
    

Conclusion

Docker has redefined how developers approach application deployment, making it easier to manage dependencies, isolate conflicts, and ensure consistency across environments. By running Docker on Linux, you tap into the potential of a robust operating system while enjoying the flexibility and efficiency of containers.

With this introduction, you’re now equipped to begin your journey with Docker on Linux. Experiment with different images, modify your applications, and leverage Docker to streamline your development workflow! Happy containerizing!