Docker Basic Commands

Q1. How to check for Docker Client and Docker Server versions?

$ docker version

Q2. How do you get the number of containers running, paused, and stopped?

$ docker info

Q3. If you vaguely remember the command and you’d like to confirm it, how will you get help on that particular command?

$ docker --help

The above command lists all Docker commands. If you need help with one specific command, you can use the following syntax:

$ docker <command> --help

Q4. How to login into the docker repository?

You can use the following command to login into hub.docker.com:

$ docker login

You’ll be prompted for your username and password, insert those, and congratulations, you’re logged in.

Q5. If you wish to use a base image and make modifications or personalize it, how do you do that?

It’s one simple command to pull an image from docker hub:

$ docker pull <image_name>

Q6. How do you create a docker container from an image?

Pull an image from the docker repository with the above command and run it to create a container. Use the following command:

$ docker run -it -d <image_name>

Most probably the next question would be, what does the ‘-d’ flag mean in the command?

-d means the container needs to start in the detached mode.

Q7. How do you list all the running containers?

The following command lists down all the running containers:

$ docker ps

Q8. Suppose you have 3 containers running and out of these, you wish to access one of them. How do you access a running container?

The following command lets us access a running container:

$ docker exec -it <container id> bash

The exec command lets you get inside a container and work with it.

Q9. How to start, stop and kill a container?

The following command is used to start a docker container:

$ docker start <container_id>

and the following for stopping a running container:

$ docker stop <container_id>

kill a container with the following command:

$ docker kill <container_id>

Q10. Can you use a container, edit it, and update it? Also, how do you make it new and store it on the local system?

Of course, you can use a container, edit it and update it. This sounds complicated but it's actually just one command.

$ docker commit <conatainer id> <username/imagename>

Q11. How do you push it to the docker hub?

$ docker push <username/image name>

Q12. How to delete a stopped container?

Use the following command to delete a stopped container:

$ docker rm <container id>

Q13. How to delete an image from the local storage system?

The following command lets you delete an image from the local system:

$ docker rmi <image-id>

Q14. How to build a Dockerfile?

Once you’ve written a Dockerfile, you need to build it to create an image with those specifications. Use the following command to build a Dockerfile:

$ docker build <path to docker file>

Q15. Do you know why the docker system prune is used? What does it do?

$ docker system prune

The above command is used to remove all the stopped containers, all the networks that are not used, all dangling images, and all build caches. It’s one of the most useful docker commands.

Q16. What command should you run to see all running containers in Docker

$ docker ps

Q17. Write the command to stop the docker container

$ sudo docker stop container name

Q18. What is the command to run the image as a container?

$ sudo docker run -i -t alpine /bin/bash

Q19. What command can you run to export a docker image as an archive?

This can be done using the docker save command and the syntax is: 

docker save -o <exported_name>.tar <container-name>

Q20. What command can be run to import a pre-exported Docker image into another Docker host?

This can be done using the docker load command and the syntax is 

docker load -i <export_image_name>.tar


Docker interview question Set -1

Docker interview question Set -2

No comments:

Post a Comment