The Docker container is a Process and Not a Virtual Machine. So You do not need any protocol like SSH to get into the container Shell. Docker CLI has given us special commands to get into the Container like docker exec -it
and docker run -it
In this post, we are going to explore How to get access to the Container Shell or colloquially referred to as SSH into the Container. Docker SSH into the Container.
The Objective
In this post, we can see the two methods to SSH into the container or Take terminal session.
- Method1: SSH or Get into the Running Container terminal
- How to create and start the container using
docker run
- How to make sure the Container is running using
docker ps
- How to Get into the Running Container Terminal using
docker exec -it
- How to create and start the container using
- Method2: Start the Container with an interactive terminal Shell ( more like a Virtual Machine)
- How to Create and Start the Container with Interactive Shell using
docker run -it
- How to Create and Start the Container with Interactive Shell using
Method1: SSH into the Running the Container / Get into the Container terminal.
In this method, we are going to start the container in a regular way as a background process using -d
flag and the Default command of the container would not be modified.
We can SSH into the Container later when we want to do any configuration changes or check the log files etc.
So, What is The Default command of a Container
Every Container image will have a Default command and that is the command it run when you start the container or create the container by default.
That's how when you start a container, the Application/package inside the container gets started automatically
For Tomcat Container, the base command is catalina.sh run
which starts the Tomcat application server by default when you start the container
The moment the Default command of the container is completed/stopped. Your Container will stop too. Cause No Container can run without a Default Command.
if you do docker ps
you can actually see the list of running containers and what is their base command displayed in COMMAND
column
How to Create and Run the Container and Enable Terminal Access for Later use
$ docker container run -d -it --name tomcatnode1 tomcat
e3995f096b9c568a312e22aac98daf3dafbf74252de860bc8725131ae9d09132
Here
i - Enable Interactive terminal for the Container
t - Allocate a PSeudo TTY id/terminal id
d - Detach the container and run in the background
--name - name the container as alpinenode1
We have named our container here. But it is optional.
You can use the container ID as a replacement for the container name for all the docker manage commands, In fact, You don't have to use the Big Container ID you see
[e3995f096b9c568a312e22aac98daf3dafbf74252de860bc8725131ae9d09132],
the first few characters of the container, e3995 is enough for Docker to recognize the container
is the -it flag really necessary while starting the container?
Not really, You can start the container without -it flag and you can still be able to take Terminal or SSH session later
$ docker container run -d – name tomcatnode1 tomcat
b4d3b8d2b9c1a717ef3abf2dfdaf8964c0df76e020bbfd8bbc337ef93d00fdce
$ docker exec -it tomcatnode1 bash
root@b4d3b8d2b9c1:/usr/local/tomcat#
How to Make sure the Container is running?
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e3995f096b9c tomcat "catalina.sh start" 8 seconds ago Up 7 seconds tomcatnode1
docker ps should show the container which we have started and you can find the tomcatnode1 as the name in names column.
How to SSH to the Running container Or Get into the terminal
To Get into the terminal of a running container all you have to execute is a basic Shell sh or bash Command on the container using container exec command.
Since Alpine is not having bash built in. I have to use sh
as the base command to invoke the Shell (or) get into the container.
$ docker container exec -it tomcatnode1 bash
root@e399b8d2b9c1:/usr/local/tomcat#
You can use the Container ID as well instead of the Container Name
docker container exec -it e399 bash
We use the exec
command of Docker to execute a sh
or bash
command to get into the terminal. Basically we are running a Simple Unix command against a container and the command we use is to invoke the Shell. Therefore, we get the terminal
What would happen if am not using the -it flag with docker exec command
The command would still run Successfully but you would NOT get the terminal of the container
$ docker exec tomcatnode1 bash
# Just to validate the status of previous command we use $?
$ $?
-bash: 0: command not found
You can see the command is returning a Successful return code ZERO but we did not get any terminal. It is because we have not set the -it flag which takes care of assiging a terminal and interactive Session for us
so -it flag is indispensable(really necassary) with exec command here.
Method2: Start the Container with an Interactive Terminal or Shell ( More like a Virtual Machine)
We can actually start any container with a direct interactive terminal. But we must be aware it is more likely you are logging into a Container which is FRESH built or a SCRATCH container ( more like a VM with no processes on it)
Why?
This is because you have changed the Default command of the container and you have started your container with either sh
or bash
to get into the terminal
The Base/Default Command we talked earlier is what, that makes an Operating System Image as a fully functional and ready to use Application framework (or) container. Or that's what Containers are for.
If you already know how to create a Docker image, the default command I am talking about is denoted as CMD
in the Dockerfile
Don't worry if you do not know it yet. Refer this article
How to Create and Run the Container with Interactive Shell (or) terminal
If you could have guessed it already, we are going to basically change the BASE COMMAND of the container by modifying it during the invocation time.
Let us take the same Tomcat container for example.
$ docker container run -it – name tomcatnode2 tomcat bash
root@07deb9a33ba0:/usr/local/tomcat#
As shown in the preceding diagram. The command we execute here is bash
which will overwrite the base Command or CMD
of the image.
Which means that the Container would not start the Tomcat application server, though it has all the necessary packages and software bundled inside it already.
this can be useful when you are handling an OS base image and customize the Applications inside the Container and start it later
Consider you want to build your customized web server in Apache HTTPD with the static web pages and modules etc. Before you start this web server. In that case you need to SSH or Take Terminal Session to the container to perform steps like Installing Apache, Editing the Configuration files, Modifying the HTML files etc.
Hope this article helps.
Let me know your feedback and comments. Rate this article [ratings]
Thanks
Sarav AK
Follow me on Linkedin My Profile Follow DevopsJunction onFacebook orTwitter For more practical videos and tutorials. Subscribe to our channel
Signup for Exclusive "Subscriber-only" Content