Docker Commands

Docker is an open source platform that is free and easy to use. It is used to create, implement, and manage containerized applications. It helps developers to bundle programs into containers that can be run and executed in any given environment. In a traditional deployment, applications were deployed on physical servers and maintenance wasn’t easy, only 20-30% of resources were utilized. But in container deployment using Docker, an application is deployed through lightweight images, reducing the need of full-blown Virtual Machines to run applications, increasing the utilization to 70-80%. Docker increases time-to-market by 300%, developer productivity by 100%, deployment velocity by 60%, issue resolution rate by 70%, and IT operational efficiency by 50%. In the containerization market, Docker has a good market share of 82.50%, growing at a CAGR of 15.7%. Top industries such as Software Development, AI and ML, Big Data use Docker for Containers And Microservices. Companies such as Google, Amazon, Ford, Red Hat hire professionals adept in Docker containerization.

What is Docker ?

Docker is an open source platform that is free and easy to use. It is used to create, implement, and manage containerized applications. Containers simplify distributed programming, making them popular as companies shift to cloud-based development and multi-cloud hybrid setups. It helps developers to bundle programs into containers that can be run and executed in any given environment. Docker is also a toolkit that enables automation by allowing developers to use just one API to create, deploy, operate, change, and pause containers using easy commands. Development of containers can also be done without Docker, but using Docker makes the containerization process simpler, faster, and reliable.

Become a Docker Certified professional by learning this Docker Training!

Why is Docker important?

With rapid changes in business, the need for a tool that can meet business requirements at that pace has also increased. Docker containers help to deploy softwares into production easily with limited human interaction. It enhances developer productivity dramatically by ensuring that if code works great in a test environment, it will almost certainly function in production. Docker also has self-healing capabilities. If a container is not responding, it restarts it or creates a new one, making troubleshooting easier and faster. Thus we can say that the advantages of Docker are two-fold: reduction in Capital Expenditure and in Operational Expenditure. In a traditional deployment, applications were deployed on physical servers and maintenance wasn’t easy, only 20-30% of resources were utilized. But in container deployment using Docker, an application is deployed through lightweight images, reducing the need of full-blown Virtual Machines to run applications, increasing the utilization to 70-80%. Docker increases time-to-market by 300%, developer productivity by 100%, deployment velocity by 60%, issue resolution rate by 70%, and IT operational efficiency by 50%.

Docker Lifecycle Commands

The container lifecycle is a series of stages, starting from the creation of the container, running of the container, updation of the container to its destruction.
1) docker - This command is used to display all docker commands

2) docker version - This command helps to find which docker version you are currently working on. It will display the current docker version and the build name.

Syntax: docker version

3) docker info  - This command will display the information about the systems.

Syntax: docker info

4) docker pull - In a docker container lifecycle, the first thing we do is pull the images. This command helps in pulling images from the repository of Docker Hub 

Syntax: docker pull 

Example: docker pull centos

5) docker build - This command is used to build an image from a Dockerfile as well as a "context". The context of a build is the set of files in the supplied URL or path. Any of the files in the context can be referenced by the build process.

Syntax: docker build    

Example: docker build -t apache_centos ( -t allots a tty and connects to stdin and stdout. Centos is the name of the docker image that created the container)

6) docker run - This command helps in running a container from a docker image. It adds a writeable layer to the picture specified in the command.

Syntax: docker run 

Example: docker run e98b6ec72f51

7) docker commit  - This command is used to create a new image from an already existing container file by committing container changes.

Syntax: docker commit  

Example: docker commit e98b6ec72f51 ubuntu-centos

8) docker ps - If you want to see the list of all the operating containers, this is the command you should use. You can use the -a flag to view the containers including the stopped and paused ones.

Syntax: docker ps

9) docker start - This command will start one or more paused/stopped docker containers

Syntax: docker start 

Example: docker start 6bfb1271fcdd

10) docker stop - This command will stop one or more running docker containers.

Syntax: docker stop 

Example: docker stop 6bfb1271fcdd

11) docker logs - Use this command to see logs of a container. Viewing logs help in debugging issues.

Syntax: docker logs 

Example: docker logs 6bfb1271fcff

12) docker rename - This command will rename docker containers.

Syntax: docker rename 

Example: docker rename happy_world myfirstUbuntuContainer

13) docker rm - This command will remove a docker container. But to remove a container, you need to stop it first.

Syntax: docker rm 

Docker Training

  • Master Your Craft
  • Lifetime LMS & Faculty Access
  • 24/7 online expert support
  • Real-world & Project Based Learning

Docker Image Commands

These commands execute docker containers containing images. We have listed some common Docker Image commands below:
1.  docker build - This command is used to build an image from a Dockerfile 

Syntax: docker build    

Example: docker build -t apache_centos (Here -t allots a tty and connects to stdin and stdout. Centos is the name of the docker image that created the container)

2. docker pull - This command pulls docker images from docker hub repository.

Syntax: docker pull 

Example: docker pull ubuntu

3. docker tag - This command will add a new tag to a docker image.

Syntax: docker tag  image/TAG

Example: docker tag reactjsdocker fosstechnix/reactjsdocker:v2.0

4. docker images - This command is used to display all the images installed on the system

Syntax: docker images

5. docker push - This command is the opposite of docker pull. It pushes docker images to the docker hub repository.

Syntax: docker push  Name

Example: docker tag reactjs docker_registry.com/reactjs:v2.0

6. docker history - If you wish to view the docker image’s history, this is the command you should type in.

Syntax: docker image history  IMAGE

Example: docker history --no-trunc

7. docker inspect: This command is used to get low-level information on any of the Docker objects. This tool displays extensive information about the Docker-controlled constructs.

Interested in learning Docker Join HKR Docker training in Hyderabad!

Syntax: docker inspect 

Example: docker inspect cbfa678479b6

8. docker save - This command is used to save a docker image.

Syntax: docker save 

Example: docker save centos_image:tag | gzip > centos_image.tar.gz 

9. docker import - This command helps in creating docker images from Tarball

Syntax: docker import file|URL|- <repository>

Example: docker import ./centos_image.tar.gz centos:latest

10. docker export - This command is used to export an already existing container.

Syntax: docker export  | gzip > 

11. docker load - This command loads all docker images from archives or any files.

Syntax: docker load < image file URL

12. docker rm - This command removes docker images as well as builds.

Syntax: docker rmi IMAGE_ID

Example: docker rmi -f $(docker images -q) (This will remove all images forcefully)

Subscribe to our youtube channel to get new updates..!

Docker Container Commands

A Docker container is a virtual environment created from Image. The following is a list of Docker container commands that you may find useful. 

1) docker start - This command starts a Docker container

Syntax: docker start  container 

Example:  docker start cbfa678479b6

2) docker stop - This command stops a running container

Syntax: docker stop container 

Example: docker stop cbfa678479b6

3) docker restart - This command will restart a stopped docker container.

Syntax: docker restart 

4) docker pause - This command is used to pause an operating container. The difference between docker stop and docker pause is that Docker pause prohibits all processes whereas Docker stop first stops the container’s main process and then the container is stopped.

Syntax: docker pause container_id

5) docker unpause - This command unpauses a running docker container.

Syntax: docker unpause 

6) docker run - This command helps in running a container from a docker image. It adds a writeable layer to the picture specified in the command.

Syntax: docker run 

Example: docker run e98b6ec72f51

7) docker ps - If you want to see all the operating containers in a list format, this is the command you should use. You can use the -a flag to view the containers including the stopped and paused ones.

Syntax: docker ps

8) docker exec - This command helps you get access to the shell or exterior of a docker container.

Syntax: docker exec 

Example: docker exec -i -t 014528e786e0 /bin/bash (to access using container ID)

9) docker logs - Use this command to see logs of a docker container. Viewing logs help in debugging issues.

Syntax: docker logs 

Example: docker logs 6bfb1271fcff 

10) docker rename - This command will rename docker containers.

Syntax: docker rename 

Example: docker rename happy_world myfirstUbuntuContainer

11) docker rm - This command will remove a docker container. But to remove a container, you need to stop it and then remove it.

Syntax: docker rm 

12) docker inspect - This command is used to get low-level information on any of the Docker objects. This tool displays extensive information about the Docker-controlled constructs.

Syntax: docker inspect 

Example: docker inspect cbfa678479b6

13) docker attach -  This command is used to connect terminals to an operating container to control operations such as input, output, and debugging.

Syntax: docker attach  container ID/container name

Example: docker attach reactjs

14) docker kill - This command stops and removes containers.

Syntax: docker kill  container <container……>

Example: docker kill $(docker ps -q)

15) docker cp - This command helps in copying files from your local system to a docker container.

Syntax: docker cp  container:source_path destination_path|-

OR,

docker cp source_path|- Container:destination_path

Example: sudo docker cp 046828e786e1:/etc/ubuntu2 .(this will copy all files from the container to the host)

Become a Nexus Certified professional by learning this Nexus Training!

Docker Compose Commands

These commands are used to execute various containers in one application. We have listed some important docker compose commands:

1) docker-compose build - As the name suggests, this command is used to build a docker compose file.

Syntax: docker-compose build

2) docker-compose up - This command executes and runs a docker compose file.

Syntax: docker-compose up

3) docker-compose ls - This command will help you see the list of docker images that are declared within the file of the docker compose.

Syntax: docker-compose ps

4) docker-compose start - This command starts already created containers in a  file.

Syntax: docker-compose start 

5) docker-compose run - This will help you to run an application within docker-compose.yml

Syntax: docker-compose run reactjs

6) docker-compose rm - This command removes containers from docker compose files.

Syntax: docker-compose rm -f

7) docker-compose ps - This command helps in checking the status of a container from the compose of the docker.

Syntax: docker-compose ps

Docker Volume Commands

These commands are used to create, remove, and inspect docker volume. Here is a short list of the basic commands:

1) docker volume create  - This command creates volume for docker.

Syntax: docker volume create 

2) docker volume inspect  - This command inspects docker volume.

Syntax: docker volume inspect 

3) docker volume rm - This command is used to remove the volume of the docker. 

Syntax: 

docker rm –f $(docker pq -ak) (This removes the docker container)

docker volume rm (This removes the volume)

Docker Networking Commands

These basic commands listed below will help you to create, view and list docker networks and network configuration details.

1) docker network create  - This command creates a network for docker.

Example: docker network create -d bridge our-network

2) docker network ls - Use this command to list all docker networks.

Syntax: docker network ls

3) docker network inspect - This command is used to view and inspect details of docker network configuration.

Syntax: docker network inspect 

Docker Logs and Monitoring Commands

To view and monitor docker containers, use the following commands:

1) docker ps -a - This command is used to display all operating as well as stopped containers.

Syntax: docker ps -a

2) docker logs - Use this command to view logs of a docker container. Viewing logs help in debugging issues.

Syntax: docker logs 

Example: docker logs 6bfb1271fcff

3) docker events - This command fetches all events related to the docker.

Syntax: docker events

4) docker top - If you wish to view the running process of a docker, use this command.

Syntax: docker top

5) docker stats - This command helps in checking and monitoring the usage of processing units, memory devices, and network input/output devices.

Syntax: docker stats 

6) docker port - Use this command to view public ports of the docker container.

Syntax: docker port 

Docker Prune Commands

These set of commands help to erase unused or hanging docker objects such as images, networks, containers, and volumes.

docker system prune - This command will clean resources which are hanging or not related to any containers.

docker system prune -a - This command will remove docker images that are not being used or have been stopped. 

docker image prune - This command will remove lingering docker images.

docker image prune -a -  This command will remove lingering docker images.

docker container prune - This command removes docker containers that have not been used.

docker volume prune - This command removes docker volumes that have not been used.

docker network prune - This command removes docker networks that have not been used.

Top 30 Frequently Asked Docker Interview Questions !

Docker Training

Weekday / Weekend Batches

Docker Hub Commands

1. docker search - This command is used to search for docker images.

2. docker pull - This command pulls the image from docker hub.

3. docker push fosstechnix/nodejsdocker - This command pushes the image to the docker hub.

4. docker logout - Use this command to log out from the docker hub repository.

Conclusion:

With rapid changes in business, the need for a tool that can meet business requirements at that pace has also increased. Using Docker makes the containerization process simpler, faster, and reliable. Docker containers help to deploy softwares into production easily with limited human interaction.  It enhances developer productivity dramatically by ensuring that if code works great in a test environment, it will almost certainly function in production.Thus we can say that the advantages of Docker are two-fold: reduction in Capital Expenditure and in Operational Expenditure. By learning Docker, you will not only stay abreast and competitive in your field but way ahead of your peers. 

Related Articles:

Find our upcoming Docker Training Online Classes

  • Batch starts on 5th Jun 2023, Weekday batch

  • Batch starts on 9th Jun 2023, Fast Track batch

  • Batch starts on 13th Jun 2023, Weekday batch

Global Promotional Image
 

Categories

Request for more information

Srivalli
Srivalli
Research Analyst
As a senior technical content writer for HRK tainings, srivalli patchava has a greater understanding of today's data-driven environment, which includes key aspects of data management and IT organizations. She manages the task of creating great content in the areas of software testing, DevOps, Robotic process automation. Connects with her on Linkedin and Twitter.