I'm getting started working with Docker. I'm using the WordPress base image and docker-compose.
I'm trying to ssh into one of the containers to inspect the files/directories that were created during the initial build. I tried to run docker-compose run containername ls -la
, but that didn't do anything. Even if it did, I'd rather have a console where I can traverse the directory structure, rather than run a single command. What is the right way to do this with Docker?
If you have Docker installed with
Kitematic
, you can use the GUI. OpenKitematic
from the Docker icon and in theKitematic
window select your container, and then click on theexec
icon.You can see the container log and lots of container information (in settings tab) in this GUI too.
In some cases your image can be Alpine-based. In this case it will throw:
Because
/bin/bash
doesn't exist. Instead of this you should use:or
Notice: this answer promotes a tool I've written.
I've created a containerized SSH server that you can 'stick' to any running container. This way you can create compositions with every container. The only requirement is that the container has Bash.
The following example would start an SSH server attached to a container with name 'my-container'.
When you connect to this SSH service (with your SSH client of choice) a Bash session will be started in the container with name 'my-container'.
For more pointers and documentation see: https://github.com/jeroenpeeters/docker-ssh
If the container has already exited (maybe due to some error), you can do
or
or
to create a new container and get a shell into it. Since you specified --rm, the container would be deleted when you exit the shell.
If you're using Docker on Windows and want to get shell access to a container, use this:
Most likely, you already have Git Bash installed. If you don't, make sure to install it.
docker attach
will let you connect to your Docker container, but this isn't really the same thing asssh
. If your container is running a webserver, for example,docker attach
will probably connect you to the stdout of the web server process. It won't necessarily give you a shell.The
docker exec
command is probably what you are looking for; this will let you run arbitrary commands inside an existing container. For example:Of course, whatever command you are running must exist in the container filesystem.
In the above command
<mycontainer>
is the name or ID of the target container. It doesn't matter whether or not you're usingdocker compose
; just rundocker ps
and use either the ID (a hexadecimal string displayed in the first column) or the name (displayed in the final column). E.g., given:I can run:
I could accomplish the same thing by running:
Similarly, I could start a shell in the container;