In the world of vastly growing software applications which require faster developments and instantaneous deployments, docker provides an efficient way of deploying and maintaining application instances without having to worry about the environment requirements. Because, docker facilitates in creating a "container" hosted applications.
To get started in the world of docker way of application deployment, we require to get ourselves familiar with a few commands which do jobs like starting, running and terminating a "container" along with commands which help in monitoring the containers and executing commands over them.
A container is a virtualized set of resources and environment required for that application to run. It includes the runtime, the system and others. This eliminates the need for us to think about the various prerequisites required in deploying an application and the resources required for it to run smoothly without any issues. This kind of deployment is particularly useful in applications such as dotnet, java or node which require specific runtimes and web servers for the binaries to run.
Let’s look at a few basic docker commands which can help us get started in the world of dockerizing applications:
> docker create [options] <image_name> <initial_command>
This command creates a new container of a specified "image".
An image is a trimmed down version of operating system along with a specific set of resources pre-installed, which is downloaded from the docker repository and setup in the client machine (our machine) for use.
This downloaded copy of the specified image is assigned an id and is returned as the "container id". It is in this "container" that we deploy our application binaries and is exported for reuse and deployments. The "create" command creates a usable docker container out of the image but doesn’t start it. It will be in a stopped state unless "started".
> docker create hello-world
> docker start [options] <container_id> [<container_id> ..]
returns the started container with the initial command executed.
This command starts an already "created" container which is in a stopped state. The option flags provided along with the command specify how the container shall be started and communicated with. The container_id passed here can be a part of the container_id (from the beginning of the string) generated for the created container when the create command is issued.
> docker start -i d709792528dbeab
In this case, the flag -i represents interactive, where the containers Standard Input (STDIN) shall be open for communication. As soon as the container starts, unless a startup command to be executed is provided when creating, the default command which is set by default within the image will be executed. This can be overriden by passing the initial_command argument along with the create command.
> docker stop <container_id>
This command can be used to stop a running container. When this command is issued, the docker issues a SIGTERM signal to the main process executing within the running container. And after a specified grace period, the main process is forcefully killed by issuing a SIGKILL signal if the process is still executing. This command is particularly helpful is issuing a "safe exit" for the process within the container.
> docker stop d709792528dbeab
> docker kill <container_id>
This command is similar to stop, but instead of issuing a SIGTERM signal to the main process in the container and waiting for its termination, the kill command force kills the main process in the container by issuing a SIGKILL signal at the first place. Hence this command can be issued when we would want to force kill a container even though it might result in a non-safe exit.
> docker kill d709792528dbeab`
> docker run [options] <image_name> [initial_command]
This command can be used to create and start a non-existing container from a specified image. Once issued with necessary options, the command downloads the specified image from the docker repository and create a new container out of it with the specifications applied and then starts up the container.
> docker run -it ubuntu bash
When executed, a new container is created and started using an ubuntu image from the repository and a bash terminal is opened up for input to the user within the command prompt. The terminal can now accept input from the user and print them back because we’ve specified the -i and -t (interactive and pseudo-tty) flags along with the run command.
Run with Port Mapping:
> docker run -p <host_port>:<container_port> <container_id>
This command is a variant of the docker run command where in we will provide a port mapping between the container port and a host port outside of the container. This is used to forward requests or communications to a process running inside the container on a port specific to that container from the network outside. The command is particularly useful when running web applications within a container and we would need to access the application from outside world. In such case we run the container with port forwarding and let the web application be running on the port which is mapped to an outside port.
> docker run -it -p 8000:80 mcr.microsoft.com/dotnet/core/samples:aspnetapp`
The above command creates and runs a sample aspnetcore application in a container under the port 80 within the container, which is mapped to port 8000 for the network outside container.
When we open http://localhost:8000 in our local browser, we have the code executed from container and output is forwarded back.
> docker exec -it <container_id> <command>
This command is useful in issuing a second command onto a running container. This is particularly helpful when we would need to run a second command onto a container which is already mapped up and executing the initial command which is specified during the container creation. One must keep in mind that this command works only for containers which are in running state only.
> docker exec -it c669abe2068a bash
where the specified container_id belongs to a container which is already in a running state.
> docker ps [options]
This command shows up all the available containers which are created, running or stopped but available within the system. The command also shows up the container ids and other information regarding these containers, which can be used to start or stop these containers.
> docker ps -a
The -a flag specifies the docker ps command to show up all the available containers irrespective of their state. Otherwise only running containers are shown.
[…] the Dockerfile, we have two ways to run it for creating the application container. One the usual docker build and run commands, which goes as […]