Docker Engine’s Big Picture

Docker Engine’s Big Picture

Let’s quickly feel and taste the docker engine before we dive deep into it.
Broadly there are two areas where we operate in docker engines.
• Docker Images
• Docker containers

Images

As of now, you can think images as vagrant boxes. It’s very much different from the VM images but it will feel as same initially. Vagrant boxes are stopped state of a VM and Images and stopped state of containers. Run docker images command.
$ docker images
This command will list the downloaded images on your machine so you won’t see anything now in the output. We need to download some images, in docker world we call it Pulling image. So where does it pull the image from? Again, same analogy as vagrant boxes. We download the vagrant boxes from a vagrant cloud, docker images are downloaded from Docker Registries, the most famous docker registry is DockerHub. There are other registries as well, from Google, RedHat etc.
$ docker pull ubuntu: latest
latest: Pulling from library/ubuntu
bd97b43c27e3: Pull complete
6960dc1aba18: Pull complete
2b61829b0db5: Pull complete
1f88dc826b14: Pull complete
73b3859b1e43: Pull complete
Digest: sha256:ea1d854d38be82f54d39efe2c67000bed1b03348bcc2f3dc094f260855dff368
Status: Downloaded newer image for ubuntu: latest

Run the docker images command again to see the ubuntu: the latest image you just pulled.
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 7b9b13f7b9c0 2 weeks ago 118MB

We’ll get into the details of where the image is stored and what’s inside of it in the next chapter. For now, it’s enough to understand that it contains enough of an operating system (OS), as well as all the code to run whatever application it’s designed for. The Ubuntu image that we’ve pulled has a stripped-down version of the Ubuntu Linux OS including a few of the common Ubuntu utilities.

Containers

Now that we have an image pulled locally on our Docker host, we can use the docker run command to launch a container from it.
imran@DevOps:~$ docker run -it ubuntu:latest /bin/bash
root@b8765d3a67a9:/#
Look closely at the output from the command above. You should notice that your shell prompt has changed. This is because your shell is now attached to the shell of the new container - you are literally inside of the new container! Let’s examine that docker run command.
• docker run tells the Docker daemon to start a new container.
• The -it flags tell the daemon to make the container interactive and to attach our current shell to the shell of the container.
• Next, the command tells Docker that we want the container to be based on the ubuntu: latest image.
• We tell it to run the /bin/bash process inside the container.
Run the following ps command from inside of the container to list all running processes.

root@b8765d3a67a9:/# ps -elf
F SUID PID PPID C PRI NI ADDR SZ WCHAN STIME TTY TIME CMD
4 S root 1 0 0 80 0 - 4560 wait 19:46 ? 00:00:00 /bin/bash
0 R root 10 1 0 80 0 - 8606 - 19:50 ? 00:00:00 ps -elf
As you can see from the output of the ps command, there are only two processes running inside of the container:
• PID 1. This is the/bin/bash process that we told the container to run with the docker run command.
• PID 10. This is the ps -elf process that we ran to list the running processes.
The presence of the ps -elf process in the output above could be a bit misleading as it is a short-lived process that dies as soon as the ps command exits. This means that the only long-running process inside of the container is the /bin/bash process. Press Ctrl-PQ to exit the container. This will land you back in the shell of your Docker host. You can verify this by looking at your shell prompt. In a previous step, you pressed Ctrl-PQ to exit your shell from the container. Doing this from inside of a container will exit you from the container without killing it. You can see all the running containers on your system using the docker ps command.
imran@DevOps:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS
PORTS NAMES
b8765d3a67a9 ubuntu:latest "/bin/bash"6 minutes ago Up 6 minutes inspiring_heyrovsky
The output above shows a single running container. This is the container that you created earlier. The presence of your container in this output proves that it’s still running. You can also see that it was created 6 minutes ago and has been running for 6 minutes.

Attaching to running containers

You can attach your shell to running containers with the docker exec command. As the container from the previous steps is still running let’s connect back to it.
Note The example below references a container called “inspiring_heyrovsky”. The name of your container will be different, so remember to substitute “inspiring_heyrovsky” with the name or ID of the container running on your Docker host.
imran@DevOps:~$ docker exec -it inspiring_heyrovsky /bin/bash
root@b8765d3a67a9:/#
Notice that your shell prompt has changed again. You are back inside the container.
The format of the docker exec command is:
docker exec -options <'container-name or container-id><'command>.
In our example, we used the -it options to attach our shell to the container’s shell. We referenced the container by name and told it to run the bash shell.
Exit the container again by pressing Ctrl-PQ.
Your shell prompt should be back to your Docker host.
Run the docker ps command again to verify that your container is still running.
Stop the container and kill it using the docker stop and docker rm commands.
imran@DevOps: -$ docker stop inspiring_heyrovsky
inspiring_heyrovsky

imran@DevOps: -$ docker rm inspiring_heyrovsky
inspiring_heyrovsky
Verify that the container was successfully deleted by running another docker ps command
Now you would have got the taste of docker images and containers. We pulled an image, executed a container, stopped and removed it. In the next section, we will dig more in detail of images and then containers. 

Comments