Essential Docker Commands

Abhishek PathakAbhishek Pathak
2 min read

Image Management

Pull an Image from Docker Hub

docker pull image_name:tag

List Downloaded Images

docker images

Remove an Image

docker rmi image_name:tag

Container Lifecycle

Create a Container

docker run --name -d container_name image_name:tag
  • --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
  • -p -> Map port 3000 of host machine to 5173 port exposed by the docker container

Start a Container

docker start container_name

Stop a Running Container

docker stop container_name

Restart a Container

docker restart container_name

Remove a Stopped Container

docker rm container_name

Container Management

List Running Containers

docker ps

List All Containers (Including Stopped)

docker ps -a

View Logs of a Container

docker logs container_name

Access a Shell Inside a Container

docker exec -it container_name /bin/sh
  • 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

Create a Docker Network

docker network create new_name # new bridge netwok will be created

Container with New Network

docker run -d --network=new_name --name secure image_name

Volume Management

Create a Volume

docker volume create volume_name

List Volumes

`docker volume ls

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

Docker Compose (for Multi-Container Applications)

Start Containers Defined in a Compose File

docker-compose up

Stop and Remove Containers Defined in a Compose File

docker-compose down

Rebuild Images each time

docker-compose up --build

Cleanup

Remove All Stopped Containers

docker container prune

Remove All Unused Images

docker image prune

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

10
Subscribe to my newsletter

Read articles from Abhishek Pathak directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Abhishek Pathak
Abhishek Pathak

A Learner. Learning things which will exist,