DEV Community

Cover image for Top 10 docker Commands You need To Learn
Hasan Elsherbiny
Hasan Elsherbiny

Posted on

Top 10 docker Commands You need To Learn

1- Docker Run

The docker run command is your gateway to launching containers from Docker images. It allows you to specify image names, options, and runtime configurations.

docker run -d -p 8080:80 nginx
Enter fullscreen mode Exit fullscreen mode

-d: Run the container in detached mode (in the background).
-p: Map ports from the host to the container.

2- Docker Pull

Before running a container, you often need to download the Docker image from a registry like Docker Hub. The docker pull command accomplishes this.

docker pull ubuntu:latest
Enter fullscreen mode Exit fullscreen mode

3- Docker PS

To view the list of running containers, you use the docker ps command. It provides information about container IDs, names, status, and ports. To see all containers, including stopped ones, you can use docker ps -a.

docker ps
Enter fullscreen mode Exit fullscreen mode

4- Docker Stop and Docker Start

These two commands allow you to control the state of a container. docker stop halts a running container, while docker start resumes a stopped one.

docker stop {container_name_or_id}
docker start {container_name_or_id}
Enter fullscreen mode Exit fullscreen mode

5- Docker Logs

The docker logs command is invaluable for troubleshooting and monitoring. It retrieves the logs generated by a container

docker logs {container_name_or_id}
Enter fullscreen mode Exit fullscreen mode

6- Docker Exec

You can execute commands inside a running container using docker exec. This is particularly useful for debugging or running administrative tasks.

docker exec -it {container_name_or_id} {bash}
Enter fullscreen mode Exit fullscreen mode

-it: Interactive mode with a terminal.
bash: The shell you want to use inside the container.

7- Docker Build

When you need to create a custom Docker image, the docker build command is your friend. It uses a Dockerfile to define image instructions.

docker build -t custom_image_name .
Enter fullscreen mode Exit fullscreen mode

8- Docker Images

To list locally available Docker images, use docker images. This command displays image names, sizes, and tags.

docker images
Enter fullscreen mode Exit fullscreen mode

9- Docker RMI

Removing Docker images that are no longer needed frees up disk space. docker rmi allows you to remove images by their name or ID.

docker rmi image_name_or_id
Enter fullscreen mode Exit fullscreen mode

10- Docker Network

Docker provides a networking feature to connect containers and services. The docker network command helps manage these networks.

docker network ls
docker network create my_network
Enter fullscreen mode Exit fullscreen mode

Top comments (3)

Collapse
 
apetryla profile image
Aidas Petryla

There are also some I'm using quite often:

docker system prune
Remove all unused containers, networks, images (both dangling and unreferenced), and optionally, volumes.

or --all flag, for example with
docker ps --all
Show all the containers, default is only running containers

Collapse
 
hasanelsherbiny profile image
Hasan Elsherbiny

yea sure

Collapse
 
armagondo profile image
armagondo

amazing article keep going