How to create a Docker container in Linux?

DockerDockerBeginner
Practice Now

Introduction

Docker is a powerful containerization platform that has revolutionized the way developers and IT professionals manage and deploy applications. In this tutorial, we will guide you through the process of creating a Docker container on a Linux system, from installing and configuring Docker to building and running your own custom containers.

Understanding Docker Containers

Docker is a popular open-source platform that enables developers to build, deploy, and run applications in a containerized environment. Containers are lightweight, standalone, and executable software packages that include everything needed to run an application, including the code, runtime, system tools, and libraries.

What is a Docker Container?

A Docker container is a standardized unit of software that packages up code and all its dependencies, so the application runs quickly and reliably from one computing environment to another. Containers are created from Docker images, which are the blueprints for creating containers. Docker containers are isolated from each other and from the host operating system, providing a consistent and predictable runtime environment.

Benefits of Docker Containers

  • Portability: Docker containers can run on any machine that has Docker installed, regardless of the underlying infrastructure or operating system.
  • Consistency: Docker containers ensure that the application will run the same way, regardless of the environment.
  • Efficiency: Docker containers are lightweight and use fewer resources than virtual machines, as they share the host operating system's kernel.
  • Scalability: Docker containers can be easily scaled up or down, making it easier to manage and deploy applications.
  • Isolation: Docker containers are isolated from each other and the host operating system, improving security and stability.

Docker Container Architecture

graph TD A[Docker Host] --> B[Docker Engine] B --> C[Docker Images] B --> D[Docker Containers] D --> E[Application] D --> F[Libraries] D --> G[System Tools]

Docker Container Lifecycle

  1. Build: Create a Docker image from a Dockerfile.
  2. Ship: Store the Docker image in a registry, such as Docker Hub.
  3. Run: Create and run a Docker container from the image.

Docker Container Use Cases

  • Microservices: Docker containers are well-suited for building and deploying microservices-based applications.
  • Continuous Integration and Deployment: Docker containers can be used to automate the build, test, and deployment of applications.
  • Cloud Hosting: Docker containers can be easily deployed and scaled on cloud platforms, such as AWS, Azure, and Google Cloud.
  • Machine Learning and Data Science: Docker containers can be used to package and deploy machine learning models and data processing pipelines.

Installing and Configuring Docker on Linux

Installing Docker on Ubuntu 22.04

  1. Update the package index:
sudo apt-get update
  1. Install the necessary packages to allow apt to use a repository over HTTPS:
sudo apt-get install \
    ca-certificates \
    curl \
    gnupg \
    lsb-release
  1. Add the official Docker GPG key:
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
  1. Set up the Docker repository:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
  1. Install Docker Engine, containerd, and Docker Compose:
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin

Configuring Docker

  1. Verify the Docker installation:
sudo docker run hello-world
  1. Add your user to the Docker group to run Docker commands without sudo:
sudo usermod -aG docker $USER
  1. Configure Docker to start automatically on system boot:
sudo systemctl enable docker.service
sudo systemctl enable containerd.service
  1. (Optional) Configure Docker to use a different storage driver:
sudo vi /etc/docker/daemon.json

Add the following configuration and save the file:

{
  "storage-driver": "overlay2"
}
  1. Restart the Docker service:
sudo systemctl restart docker

Docker Compose Installation

  1. Download the latest version of Docker Compose:
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  1. Make the binary executable:
sudo chmod +x /usr/local/bin/docker-compose
  1. Verify the Docker Compose installation:
docker-compose --version

Building and Running Docker Containers

Building Docker Images

  1. Create a new directory for your project:
mkdir my-docker-project
cd my-docker-project
  1. Create a Dockerfile in the project directory:
touch Dockerfile
  1. Open the Dockerfile and add the following content:
## Use the official Ubuntu 22.04 image as the base
FROM ubuntu:22.04

## Update the package index and install necessary packages
RUN apt-get update && apt-get install -y \
    nginx \
    curl \
    && rm -rf /var/lib/apt/lists/*

## Copy the default Nginx configuration file
COPY default.conf /etc/nginx/conf.d/

## Expose port 80 for Nginx
EXPOSE 80

## Start Nginx when the container launches
CMD ["nginx", "-g", "daemon off;"]
  1. Create a default Nginx configuration file default.conf:
server {
    listen 80;
    server_name example.com;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
}
  1. Build the Docker image:
docker build -t my-nginx-app .

Running Docker Containers

  1. Run the Docker container:
docker run -d --name my-nginx-app -p 80:80 my-nginx-app
  1. Verify the container is running:
docker ps
  1. Access the Nginx web server running in the container:
curl http://localhost

Managing Docker Containers

  1. Stop the running container:
docker stop my-nginx-app
  1. Start the stopped container:
docker start my-nginx-app
  1. Remove the container:
docker rm my-nginx-app
  1. Remove the Docker image:
docker rmi my-nginx-app

Docker Compose

  1. Create a docker-compose.yml file:
version: '3'
services:
  web:
    build: .
    ports:
     - "80:80"
  1. Build and run the Docker Compose application:
docker-compose up -d
  1. Stop and remove the Docker Compose application:
docker-compose down

Summary

By the end of this tutorial, you will have a solid understanding of Docker and its capabilities, as well as the practical skills to create, manage, and run Docker containers on a Linux system. This knowledge will empower you to streamline your application deployment, improve scalability, and enhance your overall DevOps workflow.

Other Docker Tutorials you may like