Skip to content
Middleware Inventory
  • Home
  • About Us
  • Middleware
    • weblogic
    • WAS
    • Websphere
    • wlst
    • wsadmin scripts
    • JMS
    • ActiveMQ
    • Troubleshooting
    • Apache
    • Best Practice
    • F5-Big-IP
  • Devops
    • Ansible
    • AWS
    • Docker
    • Packer
    • Splunk
    • Terraform
    • Vagrant
    • Kubernetes
  • Newsletter
  • Book a Consultation

Docker SSH Into the Container - How to SSH to Container

Updated on: June 15, 2022 Sarav AK
  • 0

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.

How to SSH into Docker Container
Image Credits to @justinbeck

Table of Contents

  • The Objective
  • Method1: SSH into the Running the Container / Get into the Container terminal.
    • How to Create and Run the Container and Enable Terminal Access for Later use
    • is the -it flag really necessary while starting the container?
    • How to Make sure the Container is running?
    • How to SSH to the Running container Or Get into the terminal
  • Method2: Start the Container with an Interactive Terminal or Shell ( More like a Virtual Machine)
    • How to Create and Run the Container with Interactive Shell (or) terminal

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
  •  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

 

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#

How to SSH into Docker Contianer
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

Buy Me a Coffee at ko-fi.com

Signup for Exclusive "Subscriber-only" Content

Loading

More from Middleware Inventory

  • Docker Run Image as Container - Create Container From Docker Image

    In this post, we are going to see how to create an image and run the image as a container and manage it. Before we proceed further, it is indispensable, that we are aware of what is an Image and What is a Container. So let us begin from there.…

  • Docker Weblogic : Run Oracle Weblogic 12c on Docker

    The Introduction to Docker Weblogic In this post, we are going to be exploring the quick and easy option available to get started with weblogic and Docker.  In this post, we are going to see how to create a weblogic container in docker in a few easy steps. The post's…

  • Docker tomcat Container
    Docker Tomcat Example - Dockerfile for Tomcat, Docker Tomcat Image

    In this post, we are going to learn how to install a Tomcat Application Server or Web Container on Docker and Deploy web applications into the Tomcat running inside Docker.  This post is all about Docker Tomcat and deploying war web application into tomcat docker, Sample Docker Tomcat image, Dockerfile…

  • docker pull container-registry.oracle.com - access denied (or) Unauthorized

    In this article we are going to see how to fix the access denied and unauthorized errors while using the containers of container-registry.oracle.com. Most of us at times might have got this exceptions access denied and unauthorized  while downloading the image using docker pull command. This article is all about…

  • Docker NodeJS Example - Run NodeJS on Docker | Devops Junction

    In this post, we are going to see how to Design and Develop a Simple NodeJS web application and Deploy the NodeJS web application to Docker and How to create Docker Images and start the container from the images and How to manage the docker container and publish your images…

Docker SSHGet into DockerHow Docker SSH worksHow to Docker SSHHow to Get into the Docker ContainerHow to Login to the Docker ContainerHow to modify the files inside the Docker Container

Post navigation

Previous Post:Packer AWS Example with Terraform
Next Post:Docker Run Image as Container - Create Container From Docker Image

Categories

  • ActiveMQ (4)
  • Ansible (68)
  • Apache (8)
  • AWS (40)
  • Azure (1)
  • Best Practice (5)
  • BitBucket (1)
  • Boto (2)
  • Cloud (1)
  • Database (1)
  • Development (3)
  • DevOps_Beginner (13)
  • Docker (11)
  • EFK (1)
  • Elastic Search (1)
  • F5-Big-IP (6)
  • FluentD (2)
  • GCP – GoogleCloud (2)
  • Github Actions (1)
  • Grafana (1)
  • Graphite (1)
  • IBM Cloud (1)
  • IBM Websphere (2)
  • IHS and Apache (10)
  • IIS (4)
  • JavaScript (3)
  • Jenkins (4)
  • Joomla (1)
  • Kubernetes (29)
  • Middleware (3)
  • mysql (2)
  • Network Troubleshooting (1)
  • Networking (1)
  • NodeJS (2)
  • Office365 (1)
  • OpenSource (2)
  • Oracle DB (3)
  • Oracle Weblogic (2)
  • Our Products (2)
  • Packer (2)
  • Perl (1)
  • PHP (1)
  • PostgreSQL (1)
  • PowerShell (1)
  • problem/solution (8)
  • Prometheus (1)
  • Pulumi (1)
  • Python Flask (1)
  • python_scripts (7)
  • Redis (1)
  • Serverless (1)
  • Shell Scripting (6)
  • SOAP Web Services (1)
  • Splunk (1)
  • SRE (1)
  • StatsD (1)
  • Terraform (13)
  • tomcat (8)
  • tools (6)
  • Traefik (1)
  • Troubleshooting (3)
  • Uncategorized (10)
  • Unix (5)
  • Vagrant (4)
  • VueJS (2)
  • Vulnerabilities & Fixes (21)
  • weblogic (51)
  • Webserver (1)
  • Websphere (8)
  • Windows Server (5)
  • wlst (7)
  • wordpress (2)
  • wsadmin scripts (1)
Powered by MiddlewareInventory and gritfy
Copy link
CopyCopied
Powered by Social Snap
We use cookies to ensure that we give you the best experience on our website. The Cookies collected are used only to Show customized Ads. We Hope you are fine with it.OkPrivacy policy