{"id":294,"date":"2023-01-28T08:32:16","date_gmt":"2023-01-28T08:32:16","guid":{"rendered":"https:\/\/www.dedicatedcore.com\/blog\/?p=294"},"modified":"2024-10-15T08:59:29","modified_gmt":"2024-10-15T08:59:29","slug":"ssh-into-docker-container","status":"publish","type":"post","link":"https:\/\/www.dedicatedcore.com\/blog\/ssh-into-docker-container\/","title":{"rendered":"SSH into Docker Container"},"content":{"rendered":"<p>A container can be made using the tool Docker to run apps. A fully contained virtual computer is a Docker container. Three different ways to docker SSH containers and commands are explained in this guide. Enabling SSH on Ubuntu allows remote access to the system via a secure <a href=\"https:\/\/www.dedicatedcore.com\/blog\/enable-ssh-ubuntu\/\" target=\"_blank\" rel=\"noopener\">encrypted connection<\/a>, also to securely manage and control your Ubuntu server from another location.<\/p>\n<p>For installing and maintaining applications. Docker has taken over the containerization industry as the de facto standard. Secure Shell (SSH) access to running containers is one of Docker&#8217;s significant features. We will go into the subject of SSH access to Docker containers. Here, you will discover how to connect to an active container and perform commands inside of it. Besides this, changing the SSH Linux port other than 22 is a reason that is to <a href=\"https:\/\/www.dedicatedcore.com\/blog\/change-ssh-linux-port-other-than-22\/\" rel=\"ugc\">improve protection<\/a> and avoid unnecessary invasions.<\/p>\n<h3>Understanding Docker and SSH:<\/h3>\n<ul>\n<li>Please give a brief explanation of SSH and its importance in secure remote access.<\/li>\n<li>Introduce Docker and its advantages for managing and deploying applications.<\/li>\n<li>Stress the importance of SSH access to Docker containers for administrative and debugging purposes.<\/li>\n<\/ul>\n<h3>SSH into an Active Docker Container:<\/h3>\n<ul>\n<li>As noted in the previous response, describe the procedures for gaining SSH access to a Docker container that is currently executing.<\/li>\n<li>Knowing the container ID or name for SSH access should be emphasized.<\/li>\n<li>Give an example command that will list active containers and show which one is the target container.<\/li>\n<\/ul>\n<h2>Option 1: Run Commands in a Docker Container Using Docker exec<\/h2>\n<p>The docker exec command executes a specified run command inside the docker container that is already operating. Through the creation of a bash shell (a shell where you can type commands), you can use it to SSH into a Docker container.<\/p>\n<p>To run a command in a container using docker exec, use the following basic syntax:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">docker exec [options] [container] [command]<\/pre>\n<p>If you haven&#8217;t already, start by downloading a Docker image. You could, for instance, load Nginx:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sudo docker pull nginx<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-295\" src=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/pull-docker-image-load-nginx.png\" alt=\"docker pull image in command line\" width=\"800\" height=\"270\" srcset=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/pull-docker-image-load-nginx.png 800w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/pull-docker-image-load-nginx-300x101.png 300w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/pull-docker-image-load-nginx-150x51.png 150w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/pull-docker-image-load-nginx-768x259.png 768w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/pull-docker-image-load-nginx-100x34.png 100w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/pull-docker-image-load-nginx-700x236.png 700w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/p>\n<p>Then run the image,<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sudo docker run \u2013\u2013name nginx\u2013test \u2013d nginx<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-296\" src=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/run-docker-image-command.png\" alt=\"docker run image command line\" width=\"800\" height=\"306\" srcset=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/run-docker-image-command.png 800w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/run-docker-image-command-300x115.png 300w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/run-docker-image-command-150x57.png 150w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/run-docker-image-command-768x294.png 768w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/run-docker-image-command-100x38.png 100w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/run-docker-image-command-700x268.png 700w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/p>\n<p>To confirm, list all active containers:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sudo docker ps<\/pre>\n<p>Your loaded nginx-test image should now be visible.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-297\" src=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/list-docker-running-container.png\" alt=\"command to display list of all docker running containers\" width=\"800\" height=\"215\" srcset=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/list-docker-running-container.png 800w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/list-docker-running-container-300x81.png 300w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/list-docker-running-container-150x40.png 150w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/list-docker-running-container-768x206.png 768w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/list-docker-running-container-100x27.png 100w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/list-docker-running-container-700x188.png 700w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/p>\n<p>Type the following to gain access to and execute commands within that Docker container:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sudo docker exec \u2013it nginx-test \/bin\/bash<\/pre>\n<p>You are now signed into the nginx-test container. Any instructions you type will therefore be executed in that container. The -t option enables a terminal typing interface, and the -i option selects interactive.<\/p>\n<h2><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-298\" src=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/get-access-to-docker-image.png\" alt=\"run commands on specific docker image\" width=\"800\" height=\"219\" srcset=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/get-access-to-docker-image.png 800w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/get-access-to-docker-image-300x82.png 300w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/get-access-to-docker-image-150x41.png 150w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/get-access-to-docker-image-768x210.png 768w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/get-access-to-docker-image-100x27.png 100w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/get-access-to-docker-image-700x192.png 700w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><br \/>\nOption 2: Connect to a Running Container by Using the Docker Attach Command<\/h2>\n<p>Using the docker attach command, you can connect a container to a local input, output, and error stream. It starts up by default in a bash shell. Enter the following information to connect to a running container:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sudo docker attach container_Name<\/pre>\n<p>The system will connect to the nginx-test container in the example below:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sudo docker attach nginx-test<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-299\" src=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/connect-docker-image-attach-command.png\" alt=\"connect to docker image with docker attach\" width=\"800\" height=\"270\" srcset=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/connect-docker-image-attach-command.png 800w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/connect-docker-image-attach-command-300x101.png 300w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/connect-docker-image-attach-command-150x51.png 150w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/connect-docker-image-attach-command-768x259.png 768w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/connect-docker-image-attach-command-100x34.png 100w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/connect-docker-image-attach-command-700x236.png 700w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/p>\n<p>You will be working within the container once the command has been carried out. The virtual Docker environment will be affected by any commands you issue.<\/p>\n<h2>Option 3: Connect to a Docker Container via SSH.<\/h2>\n<p>A Docker container can be accessed using SSH (Secure Shell). SSH is typically used to connect remotely to a server across a network. Connecting to a virtual Docker container on your PC uses the same technology.<\/p>\n<h3>Step 1: System SSH Enable<\/h3>\n<p>Install and activate the SSH service first:<\/p>\n<p>Switching on SSH in Ubuntu 18.04:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sudo apt-get install ssh\r\nsudo systemctl ssh start\r\nsudo systemctl ssh enable\r\nservice ssh status<\/pre>\n<p><strong>On CentOS 7, activate SSH:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">yum \u2013y install openssh-server openssh-clients\r\nservice sshd start\r\nservice sshd enable\r\nservice sshd status<\/pre>\n<h3>Step 2: Get the Container&#8217;s IP address.<\/h3>\n<p>Using the docker inspect command, filter the returned information to obtain the container&#8217;s IP address.<\/p>\n<p>Use the following command for contemporary Docker engines:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sudo docker inspect -f \"{{ .NetworkSettings.IPAddress }}\" container_name<\/pre>\n<p>Run: for older Docker engines.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-300\" src=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/get-ip-address-of-docker-container.png\" alt=\"docker command to get ip address of docker container\" width=\"800\" height=\"168\" srcset=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/get-ip-address-of-docker-container.png 800w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/get-ip-address-of-docker-container-300x63.png 300w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/get-ip-address-of-docker-container-150x32.png 150w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/get-ip-address-of-docker-container-768x161.png 768w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/get-ip-address-of-docker-container-100x21.png 100w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/get-ip-address-of-docker-container-700x147.png 700w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/p>\n<h3>Step 3: SSH Into a Container for Docker<\/h3>\n<p>To verify that the IP address is active, ping it.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">ping \u2013c 3 172.17.0.2<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-301\" src=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/ping-ip-address-docker-container.png\" alt=\"ping ip address to verify ssh into docker container\" width=\"800\" height=\"344\" srcset=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/ping-ip-address-docker-container.png 800w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/ping-ip-address-docker-container-300x129.png 300w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/ping-ip-address-docker-container-150x65.png 150w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/ping-ip-address-docker-container-768x330.png 768w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/ping-ip-address-docker-container-100x43.png 100w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/ping-ip-address-docker-container-700x301.png 700w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/p>\n<p>To connect to the image, use the SSH tool:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">ssh root@172.17.0.2<\/pre>\n<p>The system should request the root user password for that container. Connection rejected indicates that the container is most likely not SSH provided. You are now connected through SSH and can issue commands inside the container if the prompt changes.<\/p>\n<h4>Wrapping Up the SSH into a Running Docker Container and Run Commands<\/h4>\n<p>List the main ideas covered in the blog post in brief. To manage and debug Docker containers, emphasize the value of secure SSH access. Encourage readers to learn about and use SSH. Access to improve their abilities to manage Docker containers. To know How to Delete Docker Images, Containers, and Volumes read it. To securely transfer files in <a href=\"https:\/\/www.dedicatedcore.com\/blog\/transfer-files-rsync-over-ssh\/\" target=\"_blank\" rel=\"noopener\">different directories using a secure shell<\/a> rsync over SSH protocol is used.<\/p>\n<p>A conventional SSH connection is not advised because Docker containers are portable and light. Either docker exec or docker attaches are suggested ways to execute commands inside a Docker container. You might connect to a virtual machine through Docker. Use the docker-machine SSH command if you&#8217;re deploying many distant virtual machines. The first two command techniques are suggested for most users.<\/p>\n<h4>Frequently Asked Questions (FAQ)<\/h4>\n<p><strong>1. What is SSH?<\/strong><\/p>\n<p>A secure shell is a network protocol used to securely access remote computers. SSH provides a secure channel for data transmission.<\/p>\n<p><strong>2. How do I Create a Shell session within a Docker Container?<\/strong><\/p>\n<p>Use the following command:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">docker exec -it &lt;container_id&gt;<\/pre>\n<p>With the help of the above command, one can create an interactive shell session within a docker container.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A container can be made using the tool Docker to run apps. A fully contained virtual computer is a Docker container. Three different ways to&#8230;<\/p>\n","protected":false},"author":1,"featured_media":439,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/posts\/294"}],"collection":[{"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/comments?post=294"}],"version-history":[{"count":14,"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/posts\/294\/revisions"}],"predecessor-version":[{"id":2449,"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/posts\/294\/revisions\/2449"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/media\/439"}],"wp:attachment":[{"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/media?parent=294"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/categories?post=294"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/tags?post=294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}