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.
docker restart policies dictate how a container should behave when it is shutted down by some factor.
There are four restart-policies:
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>
> docker run -p 8080:80 nginx
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
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.
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.
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.
> docker build -t refebruvuser/mynpm .
And is run as:
> docker run -i -t <docker_hub_user_id>/<repository_name>[:version_tag]
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.
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.
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).
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.
> 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.
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.
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]
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|kill <container_id>
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.
Subscribe to our Weekly Digest and get newsletters delivered directly to your inbox.