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.
We have two ways to get inside a container from the host operating system on which the docker engine is running.
- During the “docker container run” command with “-it” option
- 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
Notice, the prompt is changed to the “[email protected]” 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.
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.
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.
To gain more about docker, follow -> Learn Docker