DEV Community

Cover image for Essential Docker Commands for Developers
Suleiman Dibirov
Suleiman Dibirov

Posted on • Edited on

Essential Docker Commands for Developers

Docker has revolutionized the way we build, ship, and run applications.
In this article, we’ll explore the most useful Docker commands every developer should know.

1. Installation and Setup

Before diving into Docker commands, ensure Docker is installed on your machine. You can download Docker Desktop from the official Docker website.

2. Basic Docker Commands

docker run

Run a Docker container from an image. If the image is not present locally, Docker will pull it from the Docker Hub.

docker run hello-world
Enter fullscreen mode Exit fullscreen mode

docker pull

Download an image from a Docker registry.

docker pull nginx
Enter fullscreen mode Exit fullscreen mode

docker ps

List running Docker containers.

docker ps
Enter fullscreen mode Exit fullscreen mode

To include stopped containers, use:

docker ps -a
Enter fullscreen mode Exit fullscreen mode

docker stop

Stop a running container.

docker stop <container_id>
Enter fullscreen mode Exit fullscreen mode

docker rm

Remove a stopped container.

docker rm <container_id>
Enter fullscreen mode Exit fullscreen mode

docker rmi

Remove an image.

docker rmi <image_id>
Enter fullscreen mode Exit fullscreen mode

3. Image Management

docker build

Build an image from a Dockerfile.

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

docker images

List all Docker images on your machine.

docker images
Enter fullscreen mode Exit fullscreen mode

docker tag

Tag an image for a specific repository.

docker tag myapp myrepo/myapp:latest
Enter fullscreen mode Exit fullscreen mode

docker push

Push an image to a Docker registry.

docker push myrepo/myapp:latest
Enter fullscreen mode Exit fullscreen mode

4. Container Management

docker exec

Run a command in a running container. This is useful for debugging and inspection.

docker exec -it <container_id> /bin/bash
Enter fullscreen mode Exit fullscreen mode

docker logs

View the logs of a container.

docker logs <container_id>
Enter fullscreen mode Exit fullscreen mode

docker inspect

Inspect the details of a container or image.

docker inspect <container_id>
Enter fullscreen mode Exit fullscreen mode

docker-compose

Docker Compose is a tool for defining and running multi-container Docker applications. Define your services in a docker-compose.yml file, and then use the following commands:

docker-compose up

Start and run your entire application defined in docker-compose.yml.

docker-compose up
Enter fullscreen mode Exit fullscreen mode

docker-compose down

Stop and remove all containers defined in docker-compose.yml.

docker-compose down
Enter fullscreen mode Exit fullscreen mode

5. Networking and Volumes

docker network ls

List all Docker networks.

docker network ls
Enter fullscreen mode Exit fullscreen mode

docker network create

Create a new Docker network.

docker network create mynetwork
Enter fullscreen mode Exit fullscreen mode

docker volume ls

List all Docker volumes.

docker volume ls
Enter fullscreen mode Exit fullscreen mode

docker volume create

Create a new Docker volume.

docker volume create myvolume
Enter fullscreen mode Exit fullscreen mode

docker run with Networking and Volumes

Run a container with a specific network and volume.

docker run -d --name mycontainer --network mynetwork -v myvolume:/data myimage
Enter fullscreen mode Exit fullscreen mode

6. Cleanup Commands

docker system prune

Clean up unused Docker objects (containers, images, networks, and volumes).

docker system prune
Enter fullscreen mode Exit fullscreen mode

docker volume prune

Remove all unused volumes.

docker volume prune
Enter fullscreen mode Exit fullscreen mode

docker network prune

Remove all unused networks.

docker network prune
Enter fullscreen mode Exit fullscreen mode

Top comments (0)