10 Useful Docker Commands Every Developer Should Know
Main Docker commands for controlling containerization.
Docker has become an indispensable tool for developers, enabling consistent environments and simplifying application deployment. Understanding key Docker commands can significantly improve your efficiency and mastery of containerization. Whether you’re just starting out or looking to perfect your skills, knowing the right commands can make all the difference.
Hello, my name is CyCoderX and in this article we will explore 10 essential Docker commands that every developer should have in their toolbox, accompanied by explanations and practical examples.
Let’s go!
I write articles for everyone to enjoy, and I would like your support by following me, clapping, and highlighting parts of my article!
1. docker run
Starting with the most used command. THE docker run
The command creates and starts a container from a specified image.
Syntax:
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Example:
docker run -d -p 8080:80 nginx
- Starts an NGINX container in detached mode (
-d
) and maps port 8080 on the host to port 80 in the container.
2. docker ps
This command lists all running containers, providing useful details such as container ID, image, status, and ports.
Syntax:
docker ps [OPTIONS]
Example:
docker ps
- Displays a list of currently running containers.
Use the -a
flag to see all containers, including stopped ones:
docker ps -a
3. docker build
Builds an image from a specified Dockerfile.
Syntax:
docker build [OPTIONS] PATH
Example:
docker build -t my-image .
- Builds a Docker image from the Dockerfile in the current directory and marks it as
my-image
.
4. docker exec
Runs a command in an existing container.
Syntax:
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Example:
docker exec -it my-container bash
- Opens an interactive terminal (
-it
) in the containermy-container
.
5. docker logs
Retrieves logs from a container, which is useful for debugging and monitoring.
Syntax:
docker logs [OPTIONS] CONTAINER
Example:
docker logs my-container
- Shows container logs
my-container
.
Use the -f
flag to follow logs in real time:
docker logs -f my-container
6. docker images
Lists all Docker images available on the host machine.
Syntax:
docker images [OPTIONS]
Example:
docker images
- Shows a list of all images, including their repository names, tags, and sizes.
Consider subscribing to my mailing list to receive weekly articles full of ideas, tips and tricks straight to your inbox!
7. docker rmi
Deletes one or more Docker images.
Syntax:
docker rmi [OPTIONS] IMAGE [IMAGE...]
Example:
docker rmi my-image
- Delete the image
my-image
of the local machine.
8. docker stop
Stops a running container.
Syntax:
docker stop [OPTIONS] CONTAINER [CONTAINER...]
Example:
docker stop my-container
- Stop the container
my-container
.
9. docker rm
Deletes one or more containers.
Syntax:
docker rm [OPTIONS] CONTAINER [CONTAINER...]
Example:
docker rm my-container
- Delete stopped container
my-container
.
To use docker rm -f
to forcefully remove running containers.
10. docker pull
Downloads an image from a Docker registry (for example, Docker Hub).
Syntax:
docker pull [OPTIONS] NAME[:TAG]
Example:
docker pull python:3.10
- Extract the Python 3.10 image from Docker Hub.