Mastering Docker Commands

Unlock the Power of Docker with Essential Commands for Building, Running, and Managing Containers

Docker has revolutionized the way we develop,ship and run applications by providing lightweight, portable and scalable containerization technology. Now a days understanding docker commands can greatly enhance your efficiency in managing containerized application.

This Guide helps in understanding the essential docker commands.

Install docker through docker official site

Check docker version

Ensure Docker is installed correctly by checking the version:

docker --version

Get help with Docker Commands

If you're ever unsure about a Docker command, use:

docker help

Managing Docker images and containers

listing docker images

To list all available Docker images on your system

   docker images

list running containers

To view active containers:

docker ps

list all containers (running+stopped)

To display all containers, including stopped ones:

docker ps -a

Run a container

To start a container using a specific image:

docker run hello-world

Stop a container

To stop a running container:

docker stop <container-id>

Start a container

To restart a stopped container:

docker start <container-id>

Remove a container

To delete a stopped container:

docker rm <container-id>

#you can only remove a stopped a container.

Inspect a container

To retrieve detailed information about a container:

docker inspect <container-id>

Check logs of a container

To view logs from a container:

docker log <container-id>

Rename a container

To rename an existing container:

docker rename <container-id> <new-name>

Restart a container

To restart a running or stopped container:

docker restart <container-id>

Run a container in background

To run a container in detached mode:

docker run -d <container-id>

Run a container in foreground

To start a container and keep it attached to the terminal:

docker start -a <container-id>

Start a shell inside a running container

To get interactive access to a container:

docker exec -it <container-id> /bin/bash

Download an image from the docker registry

To pull an image from the Docker registry:

docker pull <image:tag>

Delete an image

To remove an image from your system:

docker rmi <images>

Build a docker image through Dockerfile

To create a new image using a Dockerfile:

docker build <image-name:tag> .

Show stats of running containers

To monitor resource usage of containers:

docker stats

Mapping ports

To map ports between the host and the container:

docker run -P <image_id>

Kill a running container

To forcefully stop a container:

docker kill <container-id>

Pause a running container

To temporarily suspend a container’s processes:

docker pause <container-id>

Unpause a running container

To resume a paused container:

docker pause <container-id>

Prune unused docker images

To delete unused images:

docker image prune

Prune unused docker container

To delete stopped containers:

docker container prune

History of a docker image

To check the history of an image:

docker history <image-id>

Login to docker hub through CLI

To authenticate with Docker Hub:

docker login -u <username>

Tag a docker image

To tag an image with a new repository and tag:

docker image tag old-username/old-image new-username/new-image:v1

Push a image to docker registry

To push an image to Docker Hub:

docker push my-username/my-image

Conclusion

Mastering Docker commands is essential for efficient container management and deployment. This guide has provided a structured approach to handling images, containers, and resources in Docker. With consistent practice, you'll be able to streamline your workflow and enhance your DevOps skills.

Happy Containerizing!