Dial D for Docker: Part 3 - Interacting with the containers
In the last blog on Dial D for Docker, we understood what are docker images and containers, how to pull, run, publish, start, stop, inspect and remove them. This time lets invest our time in interacting with the containers.
Running a nginx server with default page at port 80 or a mongo server without possible CLI shell is not something that would prove productive to any docker user. Unless we find ways to ssh or a way to interact with the containers, these running containers are like a broken watch showing the right time twice a day.
We do not need to ssh into the containers as docker provides multiple ways to interact with them.
Running a container in an interactive mode
To run a container in an interactive mode we need to take a closer look into options that docker provides for run
Ooooo! Thats a lot of options.
Understandable! Take a look at options -i
and -t
, option i provides an open STDIN connection while option t provides a pseudo TTY.
Let us use these to run an ubuntu container
docker container run --name local_ubuntu -it ubuntu
There is quite a lot happening here. Lets take one at a time. Executing the above command, gets us into a shell with the prompt root@990a2085f4a4
.You are not the root of the host machine but root of the container and unique id following that is the container id.
After that as you can see, I created a folder area51
in the home folder and checked the processes that were running before exiting the interactive mode. Immediately after that, if you do a docker container ls
, the container named local_ubuntu will not be listed down but an -a
listing will provide you that.
This is because, when you run a docker container, you ask it to run a particular command(in our case though we did not mention it explicitly, ubuntu ran bash command by default. You can see the command in the ls
output). Once that process corresponding to the command is stopped, the container is also stopped. In our case, when we exited the bash, we stopped the container.
So you might wonder how do I restart the same container, will running the above command do it?
No. It wont. Running the command would create a new container from the image. But lets confirm it(make sure you change the container name else you will end up getting an error).
As you can notice, the container has a different id and our area51 folder is missing.
So in order to start an existing container in interactive mode, we can use the following command
docker container start -ai local_ubuntu
We were able to successfully run the same container(area51 exists
), but once again the container is stopped as soon as we exit the bash. So how can we interact with a running container?
Interacting with a running container
We can interact with a running container using the exec
command and our sweet little -it
options. Lets see this with an example
So we start our ubuntu container using the docker start
command and the ls lists the local_ubuntu as a running container. Then comes our magical command
docker container exec -it local_ubuntu bash
This command executes the bash command with interactive tty options in the local_ubuntu container. Area51 still exists. I also play around with python a little bit. By the way, I love this line in The Zen of Python
Special cases aren’t special enough to break the rules.
Then I exit python and the bash. But our container is still running unlike the run command because the exec command creates a separate process for the interactive mode which is terminated on exiting while the actual process is still running.
With this, I am wrapping up this part. Play around with the interactive mode. Try running a nginx container after modifying the nginx.conf file, you can run a database container(mysql, mongo) and use exec to access the shell.
In the coming segments, I will focus more on docker networks, docker images and docker hub.
This article is a part of docker series Dial D for Docker. You can find the previous article in the following link