Life inside a docker container

To understand how Docker works, we need to understand how the outside world interacts with it, and how it looks on the inside.

If you start an Nginx container using the following command, you get a running Nginx container.

$ docker container run -d -p 80:80 nginx
Output: 75e34a69511c075c9c9dee2011c0e475f211b0ba581611735d11f17b4c031c3a //container-id

If you open http://localhost on your browser, you should see the Nginx default page, signifying that the container is running.

We will now look at this particular container both from the outside and the inside.

From the inside

You can log into a container using the following command :

$docker exec options <<container ID>> command

Docker exec lets you run commands inside a running container.

Let’s start off with a simple command

ls command
It lists all the files and folders in a directory. Let’s run it inside the container

$ docker exec 75e34a69511c ls
Output :
bin
boot
dev
etc
home
.
.
.

Note: You can also use the name of the container instead of the container ID.

bash command
It may happen that you might require CLI while running your container, for that, we have the bash command.
The most popular usage of exec is to launch the bash terminal.

We’ll run the echo and touch commands using the exec bash commands.
1. Run the following command to open bash terminal :

$ docker exec -it 75e34a69511c /bin/bash
Output : root@75e34a69511c:/#

It starts bash process in the container

Note: Here the flag -it starts an interactive session with the terminal

2. Let’s try the echo command now

root@75e34a69511c:/# echo “I am learning Docker”
I am learning Docker

3. Now we’ll create a file using the touch command

root@75e34a69511c:/# touch demo.txt

4.After executing the ls command

$ docker exec 75e34a69511c ls
bin
boot
demo.txt
dev
etc
home
..
//we see that demo.txt has been added.

Let’s look around

For the application inside a container, the container feels like a different VM itself. That is because a container contains a packaged OS also. The Linux distribution here will be quite lightweight. It will not even contain commands such as top and a lot of Linux tools.

It also has just one running process — that of our app.

In reality, a container is not a virtual machine. But since it provides the application with this sort of abstraction, the application now always runs on the same version of the OS, and you can have the same versions of required dependencies installed.

The first process

In Linux, the process ID 1 belongs to init process which is primarily responsible for starting and shutting down the system. Try running the following command in terminal:

$ ps aux 
Ouput
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.2 0.0 225752 9524 ? Ss 11:59 0:15 /sbin/init splash
root 2 0.0 0.0 0 0 ? S 11:59 0:00 [kthreadd]
root 4 0.0 0.0 0 0 ? I< 11:59 0:00 [kworker/0:0H]

You will see that the process with PID 1 is in it and it is simply because it’s the first process invoked by the kernel.

But which is the first process to run in containers? Let us have a look

1. Run a Nginx container and exec into it using the bash command.

docker container run -d nginx
docker exec -it 27fe /bin/bash

It opens an interactive bash shell in the container

2. Install procps to use the ps command. Once you have installed it run the following command:

root@27fea4131f35:/# ps aux
Output :
root 1 0.0 0.1 10628 5232 ? Ss 10:47 0:00 nginx: master process nginx -g daemon off;
nginx 6 0.0 0.0 11084 2444 ? S 10:48 0:00 nginx: worker process
root 7 0.1 0.0 3864 3188 pts/0 Ss 10:51 0:00 /bin/bash
root 334 0.0 0.0 7636 2664 pts/0 R+ 10:53 0:00 ps -aux

You see that the first process the container runs is the Nginx, this tells us that the container will always run our application as the first process and if you kill this process the container will die too.

Let’s see how that goes

root@27fea4131f35:/# kill 1

Now run the docker container ls command. You will see that Nginx container no longer exists.

Difference between exec and run command

Docker run creates a container and executes the command init. The container keeps running until the process inside it keeps running.

Whereas docker exec executes commands on an already running container.

From the Outside

The container is not a virtual machine. If you have 4 containers running, they’re all hosted on the same Docker host, and share the same underlying OS.

In reality, one docker container is just a process. If you kill that process, the container will die.

Conclusion

As a result of this containerization, docker is able to provide an environment to an application where it is running in an isolated environment, but at the same time is not as heavy as a virtual machine.

This allows us to package and distribute our application as a container, and that container will be able to run as long as the system has docker.

Happy Learning:)

Follow us on Twitter 🐦 and Facebook 👥 and Instagram 📷 and join our Facebook and Linkedin Groups 💬.

To join our community Slack team chat 🗣️ read our weekly Faun topics 🗞️, and connect with the community 📣 click here⬇

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author! ⬇

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Omkar Birade

Omkar Birade

Co-Founder at Interleap. I write to learn more.