Docker Interview Questions: Part 2

Q) How to keep the container alive, even when Docker Daemon is down?
By default, when the Docker daemon terminates, it shuts down running containers. You can configure the daemon so that containers remain running if the daemon becomes unavailable. This functionality is called live restore. The live restore option helps reduce container downtime due to daemon crashes, planned outages, or upgrades.
 
Add the configuration to the daemon configuration file. On Linux, this defaults to /etc/docker/daemon.json
{
  "live-restore": true
}
Ref Link: https://docs.docker.com/config/containers/live-restore/

Q) How to view the log for the last one hrs, logs for a particular date, tail the log of the container continuously?
To view log for last 1hr: # docker logs --since 1h <container-id>
To view log for particular date: #docker logs --until yyyy-mm-ddThh:mm:ss <container-id>         
To view log continuously:   #docker logs --follow <container-id>

Q) How to get the IP address and gateway details of the container?
#docker inspect --format '{{ .NetworkSettings.IPAddress }}' <container-id>
#docker inspect --format '{{ .NetworkSettings.Gateway }}' <container-id>
#docker inspect <container-id>|grep –wm1 "IPAddress"| cut -d '"' -f4
#docker inspect <container-id>|grep –wm1 "Gateway"| cut -d '"' -f4

Q) Explain the below command, their difference and purpose
#docker run -d --read-only -it --tmpfs /app/tmp voiptempdata

Above command will run container with read-only root file system and tmpfs mount on target directory “/app/tmp“. You can write to the directory as tmpfs creates file outside containers writeable layer. 
The --tmpfs flag does not allow you to specify any configurable options.
The --tmpfs flag cannot be used with swarm services. Its is for standalone
container. 

#docker run -d -it --name voiptempdata --mount type=tmpfs,destination=/app/tmp voipasterix

Above command will run container named “voiptempdata” with tmpfs mount on
target directory “/app/tmp“
The --mount flag allow you to specify any configurable options.It consists of
multiple key-value pairs, separated by commas.
The --mount flag is compatible with swarm services.

Ref Link: https://docs.docker.com/storage/tmpfs

Q) What is the use of tmpfs mount and where it resides? Is it possible to share them between containers?
When you don’t want to store the container’s data on the host machine and also don’t want to write data into the container's writable layer then you can use tmpfs mount option for the container.
This is useful to temporarily store sensitive files that you don’t want to persist in either the host or the container writable layer.
tmpfs mount is temporary and only persisted in the host memory. When the container stops, the tmpfs mount is removed, and files are written there won’t be persisted.
you can't share tmpfs mounts between containers.

Ref Link: http://docs.docker.oeynet.com/engine/admin/volumes/tmpfs/

Q) When to use Volume and When to use Bind Mounts?
Docker provides two options for the container to store their data on the host machine, so that data can be persisted even after the container stops and those are
Volume mounts and Bind mounts
Volumes are stored in a part of the host filesystem which is managed by Docker. The non-Docker process on Docker hosts can not modify this part of the filesystem.
Bind mounts may be stored anywhere on the host system. The non-Docker process on Docker host or docker container can modify them at any time.
The use of Volume and Bind mounts depends on your application requirements. If you want that everything should be managed by docker then use volume mount and if you want to use your own directory structure managed by you then use bind mount.
As the bind mount depends on the directory structure of the host machine, it has the potential of failure where as volume mount is managed by docker there is no chance of failure. 

Ref Link: http://docs.docker.oeynet.com/engine/admin/volumes/#choose-the-right-type-of-mount

Q) Explain the below commands and their purpose
#docker run -it –name voip1 -v voipdata:/datav voipserver

The above command will run a container with a volume that does not exist. In this case, a volume “voipdata” will be created and mounted on “/datav” inside container filesystem named “voip1”.

#docker run -it –name voip2 --volumes-from voip1 voipserver

The above command will run a container with a volume referenced from another container. In this case, a volume that is referenced from “voip1” will be mounted inside the container filesystem named “voip2”.

Ref Link: https://docs.docker.com/engine/reference/commandline/run/#mount-volumes-from-container---volumes-from

Q) How to run the containers only on manager node?
#docker service create --replicas=3 --constraint="node.role==manager" <image>

Q) Write a sample services section in Docker compose file for 3 replicas, worker node role and to restart on failure?
 
version: "3.8"
services:
  web:
    image: httpd:alpine
    ports:
      - 80:80
    deploy:
      placement:
        constraints:
          - "node.role==worker"
      mode: replicated
      replicas: 3
      restart_policy:
        condition: on-failure

Q) What are the types of logging driver available for docker? What is the default one and how to limit size of the log file?
There are different logging drivers available for docker, like none, local, json-file, syslog, journal etc. Below is the link for supported logging driver in docker.
supported-logging-drivers
 
The default logging driver of Docker for Linux distributions is “json-file”.

To limit size of log file set “max-size” value in “log-opts” configuration options in the daemon.json
 
{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "10m"
      }
}

Q) What is difference between below commands?
CMD [“/appboot.sh”] à This form is know as exec form of CMD,in this the <command> is expressed as JSON array.
CMD /appboot.sh  à This form is know as shell form of CMD,in this the <command> will execute in “/bin/sh -c “
 
Ref Link: https://docs.docker.com/engine/reference/builder/#cmd

No comments:

Post a Comment