Skip to content

Comprehensive guide to Docker installation, key concepts, examples, and common commands for Docker Engine on Linux

Notifications You must be signed in to change notification settings

bablukpik/docker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 

Repository files navigation

What is Docker?

Docker is an open-source platform for developing, shipping, and running applications in containers. Containers are lightweight, portable, and isolated units that run applications independently from the host system. Containers allow developers to package applications with all the necessary dependencies (libraries, configuration files, etc.) into a single, self-sufficient unit. This ensures the application runs consistently across different environments, such as development, testing, and production. Here's a brief overview:

  1. Containerization: Docker packages applications and their dependencies into lightweight, portable containers.
  2. Consistency: Containers ensure that applications run consistently across different environments. Since containers package everything needed to run the application, you avoid the "works on my machine" problem.
  3. Isolation: Each container runs in isolation, improving security and reducing conflicts.
  4. Efficiency: Containers share the host OS kernel, making them more resource-efficient than virtual machines.
  5. Scalability: Docker makes it easy to scale applications by running multiple instances of containers.
  6. Portability: Containers can run on any system that supports Docker (e.g., local machines, cloud platforms).
  7. DevOps: Docker facilitates DevOps practices by streamlining development, testing, and deployment processes. It is widely adopted in DevOps workflows, particularly in microservices architectures and continuous integration/continuous deployment (CI/CD) pipelines.

Key Components of Docker

  1. Docker Engine: The runtime that allows you to build, run, and manage containers.
  2. Docker Images: These are read-only templates used to create containers. An image includes everything the application needs, such as code, runtime, libraries, and settings.
  3. Docker Containers: These are instances of Docker images. They are lightweight, fast to start, and run isolated from each other and the underlying system.
  4. Dockerfile: A script that defines how to build a Docker image. It specifies the base image, dependencies, commands to run, and other configurations.
  5. Docker Hub: Docker Hub is a registry that simplifies the process of sharing, collaborating on, and reusing containerized applications.

How to install Docker

You can install either the Docker Engine or the Docker Desktop on your machine. This section will guide you on how to install the Docker Engine.

You can install the Docker Engine in two ways:

  1. By following the Docker documentation
  2. By following below step by step guide

By following Docker Documentation

You can install Docker from here Install Docker Engine on Ubuntu. If you're using other platforms instead of Ubuntu you'll get guides here Supported platforms.

By following Step by Step Guide

  • Run the following command to uninstall all conflicting packages:
for pkg in docker.io docker-doc docker-compose podman-docker containerd runc; do sudo apt-get remove $pkg; done
  • Set up Docker's Apt repository:
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

# Add the repository to Apt sources:
echo \
  "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
  • Install the Docker packages:
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
  • To check if the Docker Engine installed:
docker -v
  • To check if the Docker Compose Engine installed as a plugin with Docker:
docker compose version

Docker Engine Post-Installation Steps to Fix Permission Issues

  • Create the Docker group:
sudo groupadd docker
  • Add your user to the Docker group:
sudo usermod -aG docker $USER
  • Log out and log back in to apply the changes or run the below command:
newgrp docker
  • Change the group permission
sudo chown -R "$USER":"$USER" $HOME/.docker
sudo chmod -R g+rwx "$HOME/.docker"

Docker Setup

To run an application using Docker, follow these steps:

  1. Build the Docker image:

    docker build -t your-app-name:tag .
    
  2. Run the container:

    docker run -d -p 3000:3000 --name your-app-name:tag --network network-name your-app-name:tag
    

    This command runs the container in detached mode, maps port 3000 to the host, gives it a name, and connects it to the network-name network.

  3. To stop the container:

    docker stop your-app-name:tag
    
  4. To remove the container:

    docker rm your-app-name:tag
    

Some Common Docker Commands

Here are some common Docker commands for viewing, creating and removing Docker resources.

How to view Docker Resources

Here are common Docker commands for viewing various Docker resources:

Docker Images

  • To list all images:
docker images
  • To list all images (including intermediate images):
docker images -a
  • To list images with specific format (e.g., only ID and Repository):
docker images --format "{{.ID}}: {{.Repository}}"

Docker Containers

  • To list running containers:
docker ps
  • To list all containers (including stopped ones):
docker ps -a
  • To show the latest created container (includes all states):
docker ps -l
  • To show n last created containers (includes all states):
docker ps -n=2  # Shows last 2 containers

Docker Volumes

  • To list volumes:
docker volume ls
  • To inspect a specific volume:
docker volume inspect my-volume

Docker Networks

  • To list networks:
docker network ls
  • To inspect a specific network:
docker network inspect my-network

Docker System

  • To display Docker disk usage:
docker system df
  • To show detailed information on space usage:
docker system df -v

Inspecting Resources

  • To view detailed information about a container:
docker inspect my-container
  • To view detailed information about an image:
docker inspect my-image:tag

Viewing Logs

  • To view the logs of a container:
docker logs my-container
  • To follow log output in real-time:
docker logs -f my-container

Note: Replace my-container, my-image, my-volume, and my-network with the actual names or IDs of your Docker resources.

Additional Tip: Many of these commands support various flags for filtering and formatting output. Use docker [command] --help to see all available options for each command.

How to create Docker Resources

Here are some common Docker commands for creating Docker resources:

Docker Images

  • To create a Docker image from a Dockerfile in the current directory:
docker build -t my-image:tag .
  • To create an image from a specific Dockerfile:
docker build -t my-image:tag -f /path/to/Dockerfile .

Docker Containers

  • To create and start a Docker container from an image:
docker run --name my-container my-image:tag
  • To create a container in detached mode with port mapping:
docker run -d --name my-container -p 8080:80 my-image:tag
  • To create a container with a mounted volume:
docker run -v /host/path:/container/path --name my-container my-image:tag

Docker Volumes

  • To create a Docker volume:
docker volume create my-volume
  • To create a volume with specific driver:
docker volume create --driver local --name my-volume

Docker Networks

  • To create a Docker network:
docker network create my-network
  • To create a network with specific subnet and gateway:
docker network create --subnet 172.18.0.0/16 --gateway 172.18.0.1 my-network

Note: Remember to replace my-container, my-image, my-volume, and my-network with your own names or identifiers for your specific use case. The tag in image names is optional but recommended for version control.

Additional Tips:

  • Use docker run --help to see all available options for running containers.
  • For production environments, consider using Docker Compose or Kubernetes for managing multi-container applications.

How to remove Docker Resources

Here are some common Docker commands for removing Docker resources:

Docker Images

  • To remove specific Images:
docker rmi image_id1 image_id2 ...
  • To remove all images:
docker rmi -f $(docker images -aq)

Docker Containers

  • To remove specific Containers:
docker rm container_id1 container_id2 ...
  • To remove all containers:
docker rm -f $(docker ps -aq)
  • To remove stopped containers:
docker rm $(docker ps -aqf status=exited)
  • To stop all running containers:
docker stop $(docker ps -aq)
  • To remove all containers including their volumes:
docker rm -vf $(docker ps -aq)
  • To remove all the Containers, Images and Volumes:
docker system prune --volumes -af
  • To remove all the Containers, Images, Volumes and Networks:
docker system prune --volumes -af && docker network prune -f

Docker Volumes

  • To remove specific Volumes:
docker volume rm volume_name1 volume_name2 ...
  • To remove all volumes:
docker volume rm $(docker volume ls -q)

Docker Networks

  • To remove specific Networks:
docker network rm network_id1 network_id2 ...
  • To remove all custom Networks:
docker network rm $(docker network ls -q)

Or

docker network prune -f

Note: Use these commands with caution, especially those that remove all resources. Ensure you have backups of important data before running cleanup commands.

Docker Compose

Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application's services. Then, with a single command, you create and start all the services from your configuration.

Basic Structure of a Docker Compose File

A Docker Compose file is a YAML file that defines the services, networks, and volumes for your application. Here's a basic example:

version: "3.8"
services:
  web-service:
    image: nginx:latest
    ports:
      - "80:80"
    networks:
      - my-network

networks:
  my-network:

Building and Running with Docker Compose

To build and run your application using Docker Compose, you would typically use the following command:

docker compose up

Contributing

Feel free to submit issues, create pull requests, or fork the repository to help improve the project.

License

This project is open source and available under the MIT License.

About

Comprehensive guide to Docker installation, key concepts, examples, and common commands for Docker Engine on Linux

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published