SSH into a Docker Container? How to Execute Your Commands

Summary In this article, you learned: The main difference between a Docker container and a virtual machine Why SSH isn’t the preferred choice when it comes to running commands within your containers and what to do instead What it takes to get inside the container using SSH if you need to I hope you have […]

Summary In this article, you learned: The main difference between a Docker container and a virtual machine Why SSH isn’t the preferred choice when it comes to running commands within your containers and what to do instead What it takes to get inside the container using SSH if you need to I hope you have a clear understanding of how Docker works and how you can get the best out of it. Docker containers can basically be used in two ways: To execute a given command and finish As daemons The second usage is what can be confusing at first: Docker containers can be alive in the background, which makes them similar to a running VM, but in reality they’re more like a service (Apache, MySQL, etc.). So now you have a clearer picture, but you’re still facing the same problem: How do you run commands inside your Docker container? Assuming it’s an Ubuntu derivative, this command will do.) You’re ready to move to the next section if you get a response similar to: You need to set up the SSH daemon as you would in any other scenario if you get something like this: Given you’re working with Docker, you can do two things: Update your Dockerfile (and rebuild the image) Install the SSH daemon in the running container, in which case the changes you make will not be preserved once the container is stopped How to Include an SSH Daemon in Your Dockerfile Let’s say your Dockerfile looks like this: All you’d have to do is add the following lines at the end: Then rebuild your image and restart your container.
Source: CloudBees