July 12, 2019

Part 2: Docker Interview Questions And Answers (Docker Commands)

How to check for Docker Client and Docker Server version?
$ docker --version

Can a container restart on its own?
Since the default flag -reset is set to false, a container cannot restart by itself.

How do you find stored Docker volumes?
By using the command: /var/lib/docker/volumes

How do you get the number of containers running, paused and stopped?
$ docker info

From which command we can get the help?
$ docker --help

The help command lists all Docker commands. If we need help with one specific command, we can use:
$ docker < command > --help

Command to login into docker repository?
$ docker login

When we execute above command, we will be prompted for your username and password, insert those and congratulations, we are logged in.

Command to use a base image and make modifications or personalize it?
To do this we need to pull an image from docker hub onto your local system.

$ docker pull < image_name >

How do you create a docker container from an image?
We need to first pull the image from docker repository and run it to create a container.

$ docker run -it -d < image_name >

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

How can we check what all images are present in our system?
$ docker images

The above command will list down all the images present in our local repo.

How do you list all the running containers?
$ docker ps

ps stands for docker processes, this will return the list of active containers.

$ docker ps -a

Adding -a in the ps command will also display the containers which are shutdown.

Command to access a running container?
The exec command lets you get inside a container and work with it.

$ docker exec -it < container_id > bash

After executing the above command, we will go inside the container.

How to start, stop and kill a container?
$ docker start < container_id >
$ docker stop < container_id >
$ docker kill < container_id >

What is the difference between docker stop and docker kill command?
The docker stop attempts to stop the process run inside the container in the graceful way, while docker kill will send a kill signal to stop the main entrypoint process/program abruptly.


  • docker stop: Stop a running container (send SIGTERM, and then SIGKILL after grace period) [...] The main process inside the container will receive SIGTERM, and after a grace period, SIGKILL. [emphasis mine]
  • docker kill: Kill a running container (send SIGKILL, or specified signal) [...] The main process inside the container will be sent SIGKILL, or any signal specified with option --signal. [emphasis mine]

stop attempts to trigger a graceful shutdown by sending the standard POSIX signal SIGTERM, whereas kill just kills the process by default (but also allows to send any other signal)

Explain docker container commands.
$docker container my_command container_id

Here are the list of commands:
  • create : Create a container from an image.
  • start : Start an existing container.
  • run : Create a new container and start it.
  • ls : List running containers.
  • inspect : See lots of info about a container.
  • logs : Print logs.
  • stop : Gracefully stop running container.
  • kill : Stop main process in container abruptly.
  • rm : Delete a stopped container.
Can you use, edit it, and update a container? 
$ docker commit < container_id > < username/imagename >

This is create a new image. If you execute above command without login, it will first ask you to enter your credentials.

How do you push image to docker hub?
$ docker push < username/image name >

How to delete a stopped container?
$ docker rm < container_id >

How to delete an image from the local storage system?
$ docker rmi < image_id >

How to build a Dockerfile? 
Once we have written a Dockerfile, we need to build it to create an image with those specifications.

The build command is used to compile the Docker File, for building Docker Image.

$ docker build < path to docker file >

How can you tag a Docker image?
When you have many images, it becomes difficult to know which image is what. Docker provides a way to tag your images with friendly names of your choosing. This is known as tagging.

$ docker build -t yourusername/repository-name

e.g:
$ docker build -t yourusername/my-example-image
If you run the command above, you should have your image tagged already.  Running docker images again will show your image with the name you’ve chosen.

$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
scrutinybykhs/my-example-image latest be083a8e1111 7 minutes ago 26.1MB

If we put a dot at the end of build command, it means the docker file which is needed to build this image is present in the current directory where this command is executed. e.g:

$ docker build -t yourusername/repository-name .

Do you know why 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.

What is Port Mapping in Docker?
Docker allows you to map ports from containers to the hosts running the containers. The mapping can be done dynamically with the publish all flag or explicitly with the publish flag. And by default, the expose instruction in the Docker file doesn't actually perform any port mapping.

When you run a container with the -p argument, for example:

$ docker run -p 80:80 -d nginx

Docker Desktop makes whatever is running on port 80 in the container (in this case, nginx) available on port 80 of localhost. In this example, the host and container ports are the same. What if you need to specify a different host port? If, for example, you already have something running on port 80 of your host machine, you can connect the container to a different port:

$ docker run -p 8000:80 -d nginx

Now, connections to localhost:8000 are sent to port 80 in the container. The syntax for -p is HOST_PORT:CLIENT_PORT.

How to export and import containers with Docker?
Sometimes we need to move one or more of those containers from one host to another. For ease of transport, we'll be exporting the containers into a gzipped file. The command to export the containers is:

$ docker export container_name | gzip > container_name.gz

Where container_name is the name of the container to be exported. You are now ready to relocate the file and import it.

or

$ docker export --output="container_name.tar" container_name

In similar fashion to the export, we can import the container with a single command. Before this we must first move the exported file to the new server. We could do this using the scp command like so:

scp container_name.gz USERNAME@SERVER_IP:/home/USERNAME

Where container_name is the file name, USERNAME is the user name on the remote server, SERVER_IP is the IP address of the remote server, and USERNAME is (again) the name of the user on the remote server.

Once you've done that, the import can be handled with the following command:

zcat container_name.gz | docker import - container_name

OR

$ docket import home/import/container_name.gz

We can now run the newly imported container with a command similar to:

docker run -i -t container_name /bin/bash

-K Himaanshu Shuklaa..

No comments:

Post a Comment