3v-Hosting Blog

Docker Cheat Sheet: Basic Commands to Get Started

Administration

7 min read


Docker is the platform of choice for developing, shipping and running applications in isolated environments called containers. Containers are a lightweight, portable and efficient alternative to traditional virtual machines, making it easier to manage dependencies and ensure consistency across different environments.

This article serves as a comprehensive Docker cheat sheet, covering essential commands for managing containers, images, volumes, networks and more. Whether you're a beginner or an experienced developer, these commands will help you efficiently work with Docker.

 

 

 

 

Getting Started with Docker

 

Before using Docker, ensure that it is installed on your system. You can verify the installation by running:

    docker --version

To check the Docker service status, use:

    systemctl status docker

If Docker is not running, start it with:

    sudo systemctl start docker

For non-root users, Docker commands may require sudo. To avoid this, add your user to the docker group:

    sudo usermod -aG docker $USER

Then, restart your session or log out and log back in.

 

 

 

 


Working with Docker Images

 

Docker images are pre-packaged applications and dependencies required to create containers.


Search for Images

To find a specific image in Docker Hub:

    docker search <image-name>

For example:

    docker search ubuntu

 

Pull an Image

To download an image from Docker Hub:

    docker pull <image-name>

Example:

    docker pull nginx

To pull a specific version:

    docker pull nginx:1.21

 

List Downloaded Images

To view all available images on your system:

    docker images

 

Remove an Image

To delete an image:

    docker rmi <image-id>

If a container is using the image, it must be stopped and removed before deleting the image.

 

 

 

 

Managing Docker Containers

 

Containers are running instances of Docker images.


Run a Container

To create and start a container:

    docker run <image-name>

For example:

    docker run ubuntu

To run a container in interactive mode:

    docker run -it ubuntu bash

To run a container in detached mode:

    docker run -d nginx

 

List Running Containers

To see active containers:

    docker ps

To list all containers, including stopped ones:

    docker ps -a

 

Stop and Remove Containers

To stop a container:

    docker stop <container-id>

To remove a stopped container:

    docker rm <container-id>

To remove all stopped containers:

    docker container prune

 

Restart and Attach to a Running Container

To restart a container:

    docker restart <container-id>

To attach to a running container:

    docker attach <container-id>

 

Execute Commands Inside a Running Container

To run a command inside an active container:

    docker exec <container-id> <command>

Example:

    docker exec -it <container-id> bash

 


 

Other useful articles in our Blog:


    - What is a Website and How Do Websites Work?

    - How to Stop a DDoS Attack

    - How to Fix "Connection Refused" Error

    - How to Get Your Linux IP Address in Different Ways

 


 

 

Managing Docker Volumes

 

Docker volumes allow data to persist between container restarts.


Create and List Volumes

To create a volume:

    docker volume create my_volume

To list available volumes:

    docker volume ls

 

Use a Volume in a Container

To mount a volume to a container:

    docker run -d -v my_volume:/data ubuntu

 

Inspect and Remove Volumes

To inspect volume details:

    docker volume inspect my_volume

To remove a volume:

    docker volume rm my_volume

To remove all unused volumes:

    docker volume prune

 

 

 

 


Working with Docker Networks

 

Docker networks enable communication between containers.


List Networks

To see existing networks:

    docker network ls

 

Create and Remove Networks

To create a new network:

    docker network create my_network

To remove a network:

    docker network rm my_network

 

Connect and Disconnect Containers to a Network

To connect a running container to a network:

    docker network connect my_network <container-id>

To disconnect a container from a network:

    docker network disconnect my_network <container-id>

 

 

 

 

Docker Compose for Multi-Container Applications

 

Docker Compose allows defining multi-container applications with a docker-compose.yml file.


Start and Stop Services

To start all services defined in the Compose file:

    docker-compose up -d

To stop all services:

    docker-compose down

To restart services:

    docker-compose restart

 

List Running Services

To see running services:

    docker-compose ps

 

View Logs

To check logs for all services:

    docker-compose logs

For logs of a specific service:

    docker-compose logs <service-name>

 

 

 


Managing Docker System Resources

 

Docker provides several commands to manage system resources.


View Resource Usage

To check system-wide usage:

    docker system df

To view container resource consumption:

    docker stats

 

Remove Unused Resources

To clean up unused images, containers, and volumes:

    docker system prune

To remove all unused images:

    docker image prune -a

 

 

 

 

Conclusion

This Docker cheat sheet is your practical reference for essential Docker commands. It covers images, containers, volumes, networks and system management. Master these commands to improve your efficiency and control over Docker. Deploy microservices, manage cloud infrastructure or work with local development environments. Integrate these commands into your workflow to simplify application deployment and ensure consistency across different environments. For more advanced usage, learn about Docker Swarm, Kubernetes, and container security best practices.