Essential Docker Commands for Developers

A whale typing docker commands on a laptop

Docker has become an essential tool in the modern developer’s toolkit. It allows developers to create, deploy, and manage containerized applications efficiently. If you’re new to Docker, learning the core commands is the first step in becoming proficient with containerized environments. This guide will introduce you to essential Docker commands that every developer should know, helping you streamline your workflows and make the most of Docker’s capabilities.


1. docker version

Usage: docker version

The docker version or docker -v command displays detailed information about the Docker version installed on your system. It’s helpful for checking your Docker client and server versions, especially when troubleshooting or ensuring compatibility.


2. docker pull

Usage: docker pull <image_name>:<tag>

This command downloads a Docker image from Docker Hub (or another registry) to your local machine. You can specify the image tag to pull a specific version, like so: docker pull nginx:latest. If no tag is specified, Docker will pull the latest version by default.


3. docker images

Usage: docker images

Use docker images to list all Docker images currently stored on your local machine. The output includes details like repository name, tag, image ID, and size, which can help you manage and clean up unused images.

docker command: screenshot of docker images command

4. docker run

Usage: docker run <options> <image_name>

The docker run command is one of the most commonly used Docker commands, used to create and start a new container from a specified image. There are several useful options:

  • -d: Runs the container in detached mode (background).
  • -p: Publishes container ports to the host, e.g., -p 8080:80.
  • --name: Assigns a custom name to the container.

Example: docker run -d -p 8080:80 --name mynginx nginx


5. docker ps

Usage: docker ps

The docker ps command lists all currently running containers. Adding the -a option shows all containers, including those that have stopped. This command helps you quickly check the status of your containers.


6. docker stop

Usage: docker stop <container_id>

To stop a running container gracefully, use docker stop. It sends a SIGTERM signal to allow the container to shut down properly. You can stop multiple containers by specifying their container IDs in a single command.


7. docker rm

Usage: docker rm <container_id>

Use docker rm to delete stopped containers and free up system resources. This command is especially useful when cleaning up multiple containers. To remove all stopped containers at once, use docker rm $(docker ps -a -q).


8. docker rmi

Usage: docker rmi <image_name>

The docker rmi command removes unused images to free up disk space. You can specify an image ID or name, but note that this command only works if no containers are using the image.


9. docker exec

Usage: docker exec -it <container_id> <command>

To execute a command inside a running container, use docker exec. The -it flag allows you to interact with the container’s terminal, making it useful for debugging or inspecting running containers. For example, docker exec -it mycontainer bash opens an interactive shell session within the container. This command is so useful that it’s included in the Docker Desktop GUI if you have that installed.

screenshot of Docker Desktop as a representation of the docker command exec

10. docker logs

Usage: docker logs <container_id>

The docker logs command retrieves the output from a container’s standard output (stdout). This is helpful for monitoring application logs and debugging and is also included in the Docker Desktop GUI. You can also add options:

  • -f: Follows the log output.
  • --tail: Shows only the last few lines of logs.

11. docker build

Usage: docker build -t <tag_name> <path>

The docker build command creates a new image from a specified Dockerfile. Using the -t option, you can tag the image for easy reference. For example, docker build -t myapp:v1 . creates an image named myapp with the tag v1 from the current directory.


12. docker-compose up / down

Usage: docker-compose up -d and docker-compose down

docker-compose up is used to start and run multiple Docker containers as defined in a docker-compose.yml file. The -d option runs the containers in detached mode. docker-compose down stops and removes all containers defined in the file. Stay tuned for another article all around docker-compose.


13. docker network

Usage: docker network <options>

Docker networking commands allow you to create, manage, and inspect custom networks for your containers. For instance:

  • docker network ls: Lists all Docker networks.
  • docker network create <network_name>: Creates a new network.

Networking is essential for multi-container applications that require inter-container communication.


14. docker inspect

Usage: docker inspect <container_id>

The docker inspect command provides detailed information about containers or images, returning a JSON object with configuration and status details. If you are not using Docker Desktop, this is valuable for debugging. It reveals IP addresses, volume mounts, environment variables, and more.


15. docker stats

Usage: docker stats

To monitor resource usage for running containers in real-time, use docker stats. It provides metrics on CPU, memory, and network usage, helping you manage performance and troubleshoot bottlenecks.


Final Thoughts

Mastering these essential Docker commands is key to becoming proficient with containerization and making the most out of Docker’s power and flexibility. As you continue to work with Docker, these commands will become second nature, helping you streamline workflows and manage containers with ease.

Whether you’re building a single application container or orchestrating a complex multi-container environment, these commands will equip you with the tools you need to succeed in any Docker-driven project. Happy containerizing!

Leave a Reply

I’m David

Welcome to my little corner of the internet that I dedicate to programming. I’m a principal software engineer at Fynix Consulting and strive to always be learning new things. I love to code and I love to write about coding!

Let’s connect

Discover more from David Boothe's Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading