Docker

Docker SSH into Container

Docker is a tool or an echo system where you can create or run containers. It is a free and open-source that makes your job easier to create, deploy, and run your applications using the containers.

After creating a Docker container, you may often need to connect to a container and perform some tasks.

There are several ways you can connect to your running Docker container. In this tutorial, we will show how to connect to the running Docker container.

Requirements

  • A system running Linux with Docker installed.
  • A root password is configured or a user account with sudo privileges.

Create WordPress Container

Before starting, you will need at least one container running in your system.

First, pull the WordPress image from the Docker Hub registry with the following command:

docker pull WordPress

You should see the following output:

Using default tag: latest
latest: Pulling from library/wordpress
bf5952930446: Pull complete
a409b57eb464: Pull complete
3192e6c84ad0: Pull complete
43553740162b: Pull complete
d8b8bba42dea: Pull complete
eb10907c0110: Pull complete
10568906f34e: Pull complete
03fe17709781: Pull complete
98171b7166c8: Pull complete
3978c2fb05b8: Pull complete
71bf21524fa8: Pull complete
24fe81782f1c: Pull complete
7a2dfd067aa5: Pull complete
a04586f4f8fe: Pull complete
b8059b10e448: Pull complete
e5b4db4a14b4: Pull complete
48018c17c4e9: Pull complete
d09f106f9e16: Pull complete
289a459a6137: Pull complete
c4e8f9c90fda: Pull complete
Digest: sha256:6da8f886b20632dd05eeb22462f850a38e30600cedd894d2c6b1eb1a58e9763c
Status: Downloaded newer image for wordpress:latest
docker.io/library/wordpress:latest

Now, create a container using the downloaded image by running the following command:

docker run –d WordPress

You should see the following output:

c338aac5081c4cdc2164fed72a17e596acdd28cf5f9c26e1f7fc992389a2749b

Now, you can verify all running container with the following command:

docker ps

You should see your WordPress container in the following output:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c338aac5081c wordpress "docker-entrypoint.s…" 7 seconds ago Up 5
seconds 80/tcp xenodochial_hawking

At this point, you have one container running in your system. You can now proceed to the next step.

Connect Running container with docker exec

The docker exec is a command that allows you to connect to the running container and run your desired command inside the container.

To connect the running container, run the following command:

docker exec –it c338aac5081c /bin/bash

Once you are connected to the container, you should see the following output:

root@c338aac5081c:/var/www/html#

Where: c338aac5081c is the id of the container.

Now, run any command like “free -m” inside the container as shown below:

[email protected]:/var/www/html# free -m

You should see the following output:

total used free shared buff/cache available

Mem: 1986 981 80 117 924 711
Swap: 0 0 0

Now, exit from the running container with the following command:

exit

Connect Running container with docker attach

The docker attaches command will also allow you to connect to the running container and manage the container.

To connect the running container using the attach command, run the following command:

docker container attach c338aac5081c

Once you are connected, you should get the following output:

root@c338aac5081c:/var/www/html#

Conclusion

In the above guide, you learned how to connect to the running Docker container using the exec and attach command. Both commands allow you to start a new shell session and give an interactive shell to a container.

About the author

Hitesh Jethva

Hitesh Jethva

I am Hitesh Jethva lives in Ahmedabad, INDIA. I am Linux system administrator and Technical writer.

I felt in love with Linux while i was started to learn Linux. I am a fan of open source technology and have more than 8+ years of experience in Linux and Open Source technologies. My main motto is to make uncomplicated things easier.

I have extensive experience within the following areas: CentOS/RHEL/FreeBSD/Ubuntu/Debian, cPanel/WHM, High Availability Architecture, Proactive security/Web Application Security, Amazon Web Services (AWS), Apache/Nginx, OpenLDAP, DevOps automation: Chef/Docker.

My expertise includes Linux system administration, installation, configuration, tuning, security and troubleshooting. I have large skills in configuring/management/support of Apache, Nginx, MariaDB, MongoDB, PHP-FPM, Nagios, Zabbix, Graylg, Redis, Docker and much more.