How to Connect to a Docker Container?

A Docker container is the virtual environment to create, run and deploy applications. Connecting to the docker container enables users to modify running processes inside it. Additionally, users can install and manage packages in the docker container.

The article will demonstrate the possible methods to connect to the Docker container. The outline of these methods is as follows:

Prerequisite: Check the Docker Services

To check the services of the docker container, the “systemctl” utility is used by specifying the name of the application “docker” with the “sudo” privilege:

$ sudo systemctl status docker

It displays the current “Active” status of docker services.

Let’s start with the first method to connect to the docker container.

Method 1: Using “docker attach” to Connect to a Docker Container

The “docker attach” command enables users to connect to the running container. It links the local input and output to the container. The name or ID of the container is needed to establish a connection. The syntax to connect to the docker container is provided below:

Syntax

$ sudo docker attach container_Name

The above syntax requires the “Container_Name” to make the connection.

To make the connection of running docker container, “nginx” (built-in proxy server), the “docker attach” command executes as below:

$ sudo docker attach nginx

The output confirms that the connection has been successfully established to the “nginx” container.

List the Containers

To display the list of running containers, the “ls” command is used by specifying the “sudo” privilege:

$ sudo docker container ls

The output shows that “Container ID” is “d69015b71db8”, “Image” is “nginx”, “Command” is “/docker-entrypoint…”, “Created” date “minute”, “Status” is minute, “Port” is “80” and “Names” is “nginx”.

Let’s explore another method.

Method 2: Using “docker exec” to Connect to a Docker Container

The “docker exec” executes the new command in the already running container. It allows users to start the new session within the container’s directory. It is utilized to launch the bash terminal within the container. To start the input terminal, execute the “docker exec” command with the “it” option:

$ sudo docker exec -it nginx bash

After executing the command, it navigates to the bash environment.

Verify the Connection

To verify the new connection of the docker container,  the “ls” option is utilized to list out the containers with the “sudo” privilege:

$ sudo docker ls

The output confirms the new connection to the docker container about 45 minutes ago.

That’s how you can connect to a Docker container.

Conclusion

Docker offers the “docker attach” and “docker exec” commands to establish the connection with the running container. Before running commands, users must ensure the active services of docker via the “systemctl” utility. The list of currently running containers can also be displayed via the “ls” utility. This article briefly explained two methods to connect to the docker container.