Docker Commands
Introduction
In this article, I am going to present a comprehensive cheat sheet of commonly used Docker
commands
Installing Docker
Here are the commands to install Docker on different operating systems:
# Ubuntu/Debian:
sudo apt-get update
sudo apt-get install docker.io
# MacOS (using Homebrew):
brew install docker
# Windows OS (using choco)
choco install docker-desktop
Docker Install verify
To know docker is installed or not
What is the version installed on your machine
General Commands
Start the docker daemon
Get help with Docker. Can also use –help on all subcommands
Display system-wide information
Docker Image
Docker image is a lightweight, standalone, and executable package that contains everything needed to run a piece of software, including the code, runtime, system tools, libraries, and dependencies.
# List local images
docker images
# Delete an Image
docker rmi <image_name>
# Remove all unused images
docker image prune
Docker Build
Build an image from a Dockerfile
# Build an image from a Dockerfile and tag it with a specified name.
docker build -t <image_name>
# build an image and tag with naming conventions
docker build -t projectname/domainname/appname:yyyymmdd.sequence .
# Example
docker build -t sample/aspnet-api:20230226.1 .
# Build an image from a Dockerfile without the cache
docker build -t <image_name> . –no-cache
Docker Run
# Create and run a container from an image, with a custom name:
docker run --name <container_name> <image_name>
# Run a container with and publish a container’s port(s) to the host.
docker run -p <host_port>:<container_port> <image_name>
# Run a container in the background
docker run -d <image_name>
# Remove a stopped container:
docker rm <container_name>
# Example:
docker run --rm -p 8080:80 project1/domain1/app1:20230226.1
- --rm: This option automatically removes the container when it exits. It ensures that the container is cleaned up after it finishes running. This is useful for temporary or disposable containers.
- -p 8080:80: This option maps the host machine's port 8080 to the container's port 80. It establishes a network connection between the host and the container, allowing access to the containerized application via port 8080 on the host.
Exit the container
Docker Push
Docker container
A Docker container is a lightweight, standalone, and executable runtime instance of a Docker image. It represents a running process that is isolated from the host system and other containers. Docker container providing a consistent and reproducible environment for running applications. Containers are highly portable and can be easily moved and deployed across different environments, such as development, testing, staging, and production.
Docker Hub
Docker Hub is a cloud-based registry service provided by Docker that allows developers to store and share container images. It serves as a centralized repository for Docker images,
# Login into Docker
docker login -u <username>
# Publish an image to Docker Hub
docker push <username>/<image_name>
# Search Hub for an image
docker search <image_name>
# Pull an image from a Docker Hub
docker pull <image_name>
Docker network
This command creates a new bridge network named "network1" that containers can connect to for networked communication.
Clean up resources
you can use the docker system prune
command to clean up all dangling or unused resources, including images, containers, volumes, and networks that are not tagged or connected to a running container. This command is helpful for freeing up disk space and removing unnecessary resources.
# before cleaning up Docker, first check all the available resources using the following commands:
docker container ls
docker image ls
docker volume ls
docker network ls
docker info
docker system prune
# or
docker system prune -a
If you need to clean up all containers and images locally in Docker Desktop, you can use the following commands:
# To delete all containers including its volumes use,
docker rm -vf $(docker ps -aq)
# To delete all volumes use,
docker volume rm $(docker volume ls -q)
# To delete all the images,
docker rmi -f $(docker images -aq)
Docker Compose Commands
Below are some commonly used Docker Compose commands:
Starts services
Starts the services defined in your docker-compose.yml
file. It creates and starts containers as specified in the configuration.
Starts the services in the background (detached mode).
Stops services
Stops and removes containers, networks, volumes, and other services defined in yourdocker-compose.yml
file.
Stops and removes containers, networks, volumes, and other services while also removing volumes.
Stops and removes containers, networks, volumes, and other services, while also removing volumes and images.
Stops the services defined in your docker-compose.yml
file without removing them.
Lists the containers
Lists the containers that are part of your Docker Compose setup, showing their status.
Lists all containers, including stopped ones, that are part of your Docker Compose setup.Displays log
Displays log output from services. You can use the-f
option to follow the logs in real-time.
Displays logs for a specific service.
Executes a command
Executes a command in a running service container.Builds services
Builds or rebuilds services defined in yourdocker-compose.yml
file.
Restarts services
Restarts services.Displays configuration
Validates and displays the configuration of yourdocker-compose.yml
file.
Pauses services
Pauses all services. Containers remain running, but they stop processing requests. Unpauses services after they have been paused. Displays the running processes of a service.Scales service
Scales a service to the specified number of instances.Display events
docker compose config
Parse, resolve and render compose file in canonical forma
Streams real-time events from your services.
Docker commands Summary
Basic Commands
docker run [image]
: Start a new container from an imagedocker ps
: List all running containersdocker stop [container]
: Stop a running containerdocker rm [container]
: Remove a containerdocker images
: List all available imagesdocker pull [image]
: Download an image from a registrydocker push [image]
: Upload an image to a registrydocker build [options] [path]
: Build an image from a Dockerfile
Advanced Commands
docker exec [container] [command]
: Run a command inside a running containerdocker-compose up
: Start a Docker Compose applicationdocker network [subcommand]
: Manage Docker networksdocker volume [subcommand]
: Manage Docker volumesdocker logs [container]
: View the logs of a containerdocker inspect [container]
: Inspect a containerdocker diff [container]
: Show changes to the filesystem of a containerdocker commit [container] [image]
: Create a new image from a container's changesdocker save [image]
: Save an image to a tar archivedocker load
: Load an image from a tar archive