Connecting to Docker Containers via SSH
Now that you have enabled SSH access in your Docker containers, you can connect to them using the SSH protocol.
Obtain the Container's IP Address
To connect to a Docker container via SSH, you first need to obtain the container's IP address. You can do this by running the following command:
## Get the IP address of the Docker container
docker inspect <container_name> | grep IPAddress
Replace <container_name>
with the name or ID of your Docker container.
Connect to the Container via SSH
Once you have the container's IP address, you can use the ssh
command to connect to the container.
## Connect to the Docker container via SSH
ssh root@<container_ip_address>
Replace <container_ip_address>
with the IP address you obtained in the previous step.
If you have configured the SSH server to use password-based authentication, you will be prompted to enter the root password. If you have set up key-based authentication, you will need to provide the appropriate private key.
graph TD
A[Docker Host] -- SSH Connection --> B[Docker Container]
B[Docker Container] -- SSH Access --> C[Container Shell]
Table 3: SSH Connection Commands
| Command | Description |
| --------------------------------- | ------------------------------------------------------------- | ----------------------------------------------- |
| docker inspect <container_name> | grep IPAddress
| Obtains the IP address of the Docker container. |
| ssh root@<container_ip_address>
| Connects to the Docker container via SSH using the root user. |
By following these steps, you can now securely access the shell of your Docker containers using SSH, enabling you to perform various administrative tasks, debug issues, and manage your containerized applications more effectively.