DEV Community

Cover image for Docker essential commands
Abhishek Pathak
Abhishek Pathak

Posted on • Edited on

Docker essential commands

Image Management

Pull an Image from Docker Hub

docker pull image_name:tag
Enter fullscreen mode Exit fullscreen mode

List Downloaded Images

docker images
Enter fullscreen mode Exit fullscreen mode

Remove an Image

docker rmi image_name:tag
Enter fullscreen mode Exit fullscreen mode

Container Lifecycle

Create a Container

docker run --name -d container_name image_name:tag
Enter fullscreen mode Exit fullscreen mode
  • --name -> give name to container
  • -d -> detach mode. Container will run in background. ### Container with Port Mapping
docker run -d -p 3000:5173 --name rc1 react-image
Enter fullscreen mode Exit fullscreen mode
  • -p -> Map port 3000 of host machine to 5173 port exposed by the docker container ### Start a Container
docker start container_name
Enter fullscreen mode Exit fullscreen mode

Stop a Running Container

docker stop container_name
Enter fullscreen mode Exit fullscreen mode

Restart a Container

docker restart container_name
Enter fullscreen mode Exit fullscreen mode

Remove a Stopped Container

docker rm container_name
Enter fullscreen mode Exit fullscreen mode

Container Management

List Running Containers

docker ps
Enter fullscreen mode Exit fullscreen mode

List All Containers (Including Stopped)

docker ps -a
Enter fullscreen mode Exit fullscreen mode

View Logs of a Container

docker logs container_name
Enter fullscreen mode Exit fullscreen mode

Access a Shell Inside a Container

docker exec -it container_name /bin/sh
Enter fullscreen mode Exit fullscreen mode
  • it -> runs the Docker image in interactive mode (keep Standard Input Stream)
  • /bin/bash -> shell inside the container

Networking

List Docker Networks

docker network ls
Enter fullscreen mode Exit fullscreen mode

Create a Docker Network

docker network create new_name # new bridge netwok will be created
Enter fullscreen mode Exit fullscreen mode

Container with New Network

docker run -d --network=new_name --name secure image_name
Enter fullscreen mode Exit fullscreen mode

Volume Management

Create a Volume

docker volume create volume_name
Enter fullscreen mode Exit fullscreen mode

List Volumes

`docker volume ls
Enter fullscreen mode Exit fullscreen mode

Attach a Volume to a Container

# docker run -v dirLocalDirectory:dirContainerDirectory -d -p 3000:5173 --name rc1 react-image
docker run -v $(pwd):/app/ -d -p 3000:5173 --name rc1 react-image
Enter fullscreen mode Exit fullscreen mode

Docker Compose (for Multi-Container Applications)

Start Containers Defined in a Compose File

docker-compose up
Enter fullscreen mode Exit fullscreen mode

Stop and Remove Containers Defined in a Compose File

docker-compose down
Enter fullscreen mode Exit fullscreen mode

Rebuild Images each time

docker-compose up --build
Enter fullscreen mode Exit fullscreen mode

Cleanup

Remove All Stopped Containers

docker container prune
Enter fullscreen mode Exit fullscreen mode

Remove All Unused Images

docker image prune
Enter fullscreen mode Exit fullscreen mode

There are many more commands to go with, but these are some of the most basic and important ones.

If the article helps you, leave a like, follow, or anything 🙂.

You can follow me on LinkedIn, GitHub, Dev.to and hashnode.

Bye

Top comments (0)