How do you view logs for a container?

To view logs for running containers, we can use LOG command.

  > docker logs --follow

The --follow flag will continue streaming the new output from the container's STDOUT and STDERR onto the terminal.

What are restart policies?

docker restart policies dictate how a container should behave when it is shutted down by some factor.

There are four restart-policies:

  • No - Never restart when stopped by some means
  • Always - Always restart no matter how it is stopped
  • On-Failure - Restart only when the container stops with an erroneous code
  • Unless-Stopped - Restart always unless stopped manually with a KILL or STOP
What do you mean by port mapping in containers?

Port Mapping is a term used to link a specific port within the container where the application is presumed to be running to a port of the host system where the container is running. Since docker runs containers in an isolated virtual environment, it is not possible for the user to directly access the port within a docker container from outside. By mapping ports, we instruct docker to pass all the requests to a specific host port to a port inside the container.

> docker run -p <host_port>:<container_port> <container_id>

Example:

> docker run -p 8080:80 nginx
What are services in a docker-compose file?

The services section in a docker-compose file is an array of containers which are to be built and run together when the docker-compose file is executed. Each container under a docker-compose file is indicated with a user specific "service_name" which is used by other containers for communication among themselves. These "service names" need to be unique within a docker-compose file and are auto resolved for their respective containers by the docker.

version: '3'
services: 
    redis-server:
        build: .
        ports:
            - 8000:6379
     angular-app:
        build: .
        ports:
            - 8080:80
What is the difference between a Dockerfile and a docker-compose file?

A Dockerfile is a set of instructions about how a container for an application shall be built from the scratch using a base image. A docker-compose is a yaml script which comprises of how multiple docker containers grouped under a single subnet shall be built and run.

A docker-compose file runs on top of a Dockerfile and requires a path for the Dockerfile to be specified in the script for custom containers.

What do you mean by a Docker-Compose?

A docker-compose is a command which comes out of the box along with the docker installation, that helps in keeping instructions to run a container out of an image simple without missing out on any instruction which needs to be passed on while creating a container. A Docker-compose file is a yaml script, which contains an elaborated set of instructions about how to build and run a docker container.

How do you run a Dockerfile?

A Dockerfile can be built and run in the same way a normal container is created and run.

> docker build -t <docker_hub_user_id>/<repository_name>[:version_tag] <path_to_dockerfile>

Where -t refers to an image_tag: a unique readable name assigned to the created image by the developer for future references.

Example:

> docker build -t refebruvuser/mynpm .

And is run as:

> docker run -i -t <docker_hub_user_id>/<repository_name>[:version_tag]
How do you script a Dockerfile?

A Dockerfile is a file of same name with no extension. It typically contains three parts or steps in which a container shall be built.

  1. Specify a base Image
  2. Instructions on how to build and deploy the application onto the base Image
  3. A Startup command to specify how the container should behave once started
What is meant by a Dockerfile?

A Dockerfile is a configuration file which contains instructions for docker on how to build a customized image from an existing base image and how to get things started. It can be treated as a build script for a docker container.

What is the use of ps command?

docker PS command gives user the ability to monitor all the created containers in the system.


> docker ps

Command shows all the containers which are currently running in the system. Adding an -a flag to the command shows all the containers which are available in the system (both running and stopped).

What is the use of it flag?

The "it" flag is used in a RUN or EXEC command, which is a combination of two individual flags i and t.

i stands for interactive, which connects the user terminal to the STDIN of the container. The t flag refers to pseudo-tty which pretty prints the STDOUT channel of the container.

Example:


> docker run -it ubuntu bash

The above command runs an ubuntu container and connects the bash command output to the user terminal, which results in the ability for user to send and receive I/O onto the container.

What is the use of exec command?

EXEC command is used to run a second command in an already running docker container. When a docker container is started by means of START or RUN commands, we optionally specify a startup command which is the first command that is executed as soon as the container starts up. During scenarios where we would want to run another command on the already running container, we use the EXEC command along with the command to run.


> docker exec -it c669abe2068a bash

In the above example, the EXEC command runs a "bash" command on the running container resulting in opening a bash terminal.

What is the difference between a START and RUN command?

START command is used to start an already created container. On the other hand, a RUN command is used to CREATE and START a non-existing container from a specified image.

So basically a docker RUN command does both CREATE and START commands combined.


> docker run [options] <image_name> [initial_command]

What is the difference between KILL and STOP commands?

KILL and STOP are two commands in docker used to stop a container from execution. A docker STOP command issues a SIGTERM signal to the main process running within the container, while KILL command issues a SIGKILL signal to the process.

  • docker STOP leads to a safe exit
  • docker KILL leads to an unsafe exit
  • if a process doesn't exit for a STOP command within a specified timeout, docker issues a KILL command implicitly.

> docker stop|kill <container_id>

How do you start a docker container?

To start an already created container, we use the below command:


> docker start [options] <container_id> [<container_id> ..]

Where container_id is the unique identifier referring to a created container. This is the value returned when we run a docker create command.

We use cookies to provide you with a great user experience, analyze traffic and serve targeted promotions.   Learn More   Accept