Docker

Getting inside a container from host OS

Getting inside a container from host OS

There are times, when we want to get “inside a container” in order to update the packages or other things like code deployed or further configuration. So, to achieve that, today we are going to look at available ways to do the same.

Prerequisites

We have two ways to get inside a container from the host operating system on which the docker engine is running.

  1. During the “docker container run” command with “-it” option
  2. When the container is already running with “docker container exec

During the “docker container run” command

In this, we can specify a shell during the “docker container run” command using a flag “-it” as shown in the screenshot below.

Notice, we have specified “bash” which is a type of shell at the last of following command, and further which helps in accessing the container.

Notice, the prompt is changed to the “root@31b575e1bfcc” which is nothing but the prompt of “nginx-test” container. Further, when we ran the “hostname” command inside the container, it is returning the container-ID as its hostname.

Using interactive args during container run command
Using interactive args during container run command

When the container is already running

In this, we are logging into the container when it is already in running state, from the picture below check that the container named as “nginx-background” is already running.

What we are doing here is, with the help of “docker container exec” command, we are executing a type of shell inside the running container. In this case, we are executing “bash” which is a type of shell inside the container “nginx-background” which is running in the background. Further, after this, we can execute any set of commands inside the container.

Using exec option
Using exec option

Notice, from the picture below, while using the “docker container exec” command, even after hitting the “exit” which is logging out of the container, does not stop the container, because the “bash” command is executed after the container has started without overriding the default command for “nginx container” which in this case is “nginx -g daemon off“, So it is still running. But this is not the case with “docker container run -it” command as with this we are overriding the default command of “nginx container” this is the reason the container has been stopped.

Using exec on exit the container is still running
Using exec on exit the container is still running

To gain more about docker, follow -> Learn Docker

Comment here