Docker Command Cheat Sheet: A Beginner’s Guide

docker container cpynet

Docker is a powerful tool used to develop, distribute, and manage containerized applications. By isolating applications within containers, Docker ensures that they run consistently across different environments, which significantly speeds up the software development process. Developers using Docker can easily test, deploy, and scale their applications. Docker commands are essential tools for managing this process. In this article, we will explore the top 10 Docker commands you need to know, along with examples of their outputs.

1. docker --version

Description: This command shows the currently installed version of Docker.

Example Output:

Docker version 20.10.8, build 3967b7d

2. docker pull [image_name]

Description: This command pulls a Docker image from Docker Hub or another container registry. The image is downloaded to your local machine, and you can use it to run containers.

Example Output:

Using default tag: latest
latest: Pulling from library/ubuntu
a6c6e1e23548: Pull complete
Digest: sha256:5d1e7f9d39f7...
Status: Downloaded newer image for ubuntu:latest

3. docker images

Description: This command lists all Docker images currently stored locally on your system, including the image name, tag, image ID, creation date, and size.

Example Output:

REPOSITORY          TAG       IMAGE ID       CREATED         SIZE
ubuntu              latest    3c1e45b79bc2   2 weeks ago     73.9MB
nginx               latest    4c17e1e5f098   3 weeks ago     133MB

4. docker run [options] [image_name]

Description: This command starts a new container based on the specified image. It can also be used with various options to customize the container’s behavior.

Example Output:

$ docker run -it ubuntu bash
root@abcd1234efgh:/#

5. docker ps

Description: This command lists all the currently running containers on your system, showing details like container ID, image, command, status, and ports.

Example Output:

CONTAINER ID   IMAGE       COMMAND   CREATED         STATUS         PORTS     NAMES
abcd1234efgh   ubuntu      "bash"    5 minutes ago   Up 5 minutes             agitated_lamport

6. docker stop [container_id]

Description: This command stops a running container by specifying its container ID or name.

Example Output:

$ docker stop abcd1234efgh
abcd1234efgh

7. docker rm [container_id]

Description: This command removes a stopped container from your system. The container must be stopped before it can be removed.

Example Output:

$ docker rm abcd1234efgh
abcd1234efgh

8. docker exec -it [container_id] [command]

Description: This command allows you to run a command inside a running container. The -it option allows you to interact with the container’s shell.

Example Output:

$ docker exec -it abcd1234efgh ls
bin   dev   etc   home   lib   lib64   media   mnt   opt   root   run   sbin   srv   sys   tmp   usr   var

9. docker logs [container_id]

Description: This command retrieves and displays the logs of a running or stopped container. It’s useful for debugging and monitoring the behavior of your containers.

Example Output:

$ docker logs abcd1234efgh
Hello from Docker!
This message shows that your installation appears to be working correctly.

10. docker-compose up

Description: This command starts all the services defined in a docker-compose.yml file. It will build and start the containers defined in the file.

Example Output:

Starting myapp_web_1 ... done
Starting myapp_db_1  ... done

Docker Reset Commands

If you want to reset or clean up Docker, you can use the following commands to remove unused resources and reclaim disk space.

11. docker system prune

Description: This command removes unused data including stopped containers, unused images, networks, and build cache. It helps clean up your system and free up disk space.

Example Output:

$ docker system prune
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all dangling images
- all build cache
Are you sure you want to continue? [y/N] y
Deleted Containers:
abcd1234efgh

Total reclaimed space: 1.234GB

12. docker container prune

Description: This command removes all stopped containers from your system. It helps free up space that stopped containers might occupy.

Example Output:

$ docker container prune
Deleted Containers:
abcd1234efgh

Total reclaimed space: 200MB

13. docker image prune

Description: This command removes unused (dangling) images from your system. Dangling images are those that are no longer tagged or used by any containers.

Example Output:

$ docker image prune
Deleted Images:
sha256:3c1e45b79bc2

Total reclaimed space: 73.9MB

14. docker volume prune

Description: This command removes all unused volumes from your system. Volumes are used to persist data, and removing unused ones can free up significant space.

Example Output:

$ docker volume prune
Deleted Volumes:
my_volume

Total reclaimed space: 1.1GB

Conclusion

These Docker commands are essential for anyone working with Docker, whether you’re developing applications, managing containers, or maintaining a clean system. Understanding and using these commands will help you manage your containers, images, and system more efficiently. Docker’s flexibility and efficiency make it a must-have tool for developers and system administrators, ensuring applications are delivered faster and more reliably.

By mastering these commands, you can better streamline your workflow, troubleshoot effectively, and optimize your system’s performance. Whether you’re working on a single container or managing complex multi-container applications with Docker Compose, these commands will help you stay in control and keep your environment clean and efficient.

Previous Article

The Best AI Tools

Next Article

Common Google Chrome Errors and Their Solutions

Subscribe to our Newsletter! 📬

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨