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:
- Containerization: Docker packages applications and their dependencies into lightweight, portable containers.
- 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.
- Isolation: Each container runs in isolation, improving security and reducing conflicts.
- Efficiency: Containers share the host OS kernel, making them more resource-efficient than virtual machines.
- Scalability: Docker makes it easy to scale applications by running multiple instances of containers.
- Portability: Containers can run on any system that supports Docker (e.g., local machines, cloud platforms).
- 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.
- Docker Engine: The runtime that allows you to build, run, and manage containers.
- 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.
- Docker Containers: These are instances of Docker images. They are lightweight, fast to start, and run isolated from each other and the underlying system.
- Dockerfile: A script that defines how to build a Docker image. It specifies the base image, dependencies, commands to run, and other configurations.
- Docker Hub: Docker Hub is a registry that simplifies the process of sharing, collaborating on, and reusing containerized applications.
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:
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.
- 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
- 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"
To run an application using Docker, follow these steps:
-
Build the Docker image:
docker build -t your-app-name:tag .
-
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. -
To stop the container:
docker stop your-app-name:tag
-
To remove the container:
docker rm your-app-name:tag
Here are some common Docker commands for viewing, creating and removing Docker resources.
Here are common Docker commands for viewing various Docker resources:
- 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}}"
- 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
- To list volumes:
docker volume ls
- To inspect a specific volume:
docker volume inspect my-volume
- To list networks:
docker network ls
- To inspect a specific network:
docker network inspect my-network
- To display Docker disk usage:
docker system df
- To show detailed information on space usage:
docker system df -v
- To view detailed information about a container:
docker inspect my-container
- To view detailed information about an image:
docker inspect my-image:tag
- 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.
Here are some common Docker commands for creating Docker resources:
- 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 .
- 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
- 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
- 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.
Here are some common Docker commands for removing Docker resources:
- To remove specific Images:
docker rmi image_id1 image_id2 ...
- To remove all images:
docker rmi -f $(docker images -aq)
- 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
- To remove specific Volumes:
docker volume rm volume_name1 volume_name2 ...
- To remove all volumes:
docker volume rm $(docker volume ls -q)
- 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 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.
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:
To build and run your application using Docker Compose, you would typically use the following command:
docker compose up
Feel free to submit issues, create pull requests, or fork the repository to help improve the project.
This project is open source and available under the MIT License.