Oct 13, 2023 5 min read

How to Connect to a Docker Container

Learn to connect to a docker container with our step-by-step tutorial. It is a lightweight, standalone, and executable package.

Connect to a Docker Container
Table of Contents

Introduction

Before we begin talking on how to connect to a docker container. Let’s briefly understand - What is a Docker Container?

A Docker container is a lightweight, standalone, and executable package that includes everything needed to run an application, including the code, runtime, system libraries, and system tools. It is built based on Docker images, which are read-only templates that define a specific environment and set of instructions for creating a container.

It uses operating system-level virtualization to provide isolation and consistency across different computing environments. They allow applications to be run in a consistent and reproducible manner, regardless of the underlying infrastructure or the host operating system.

Connecting to a Docker container allows you to access and interact with the running application or services inside the container. In this tutorial, we will provide a brief overview of how to connect to a Docker container, enabling you to manage and troubleshoot your application's environment effectively.

In this tutorial, you will how to connect to a docker container. We will also address a few FAQs on how to connect to a docker container.

Attach to a Container

Despite the fact that numerous processes can be run simultaneously in a container, most docker containers only have a single process active at a time. The command that is executed when a container is started is provided using the ENTRYPOINT and/or RUN instructions.

With the help of the docker attach command, you may connect your terminal to the running container. This is effective if you wish to see what is printed in the standard output in real-time or to control the process interactively.

Let us run a new, detached Nginx container using the official Nginx image to better understand how the attach command functions.

docker container run --name my_nginx -d -p 8080:80 nginx

The -p 8080:80 option instructs Docker to map port 80 in the container to port 8080 on the host computer.

Check the running status of the “my_nginx” container by listing the containers:

docker container ls
Output

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
8e1c4974a8d8        nginx               "nginx -g 'daemon of…"   3 minutes ago       Up 2 seconds        0.0.0.0:8080->80/tcp   my_nginx

Attach to the container using the ID or name of the container:

docker container attach my_nginx

When you run the container, the nginx image's default command is set to CMD ["nginx", "-g", "daemon off;"]. You can attach your terminal to the nginx process by using the attach command.

You may view the output of the Nginx process in real time by opening 127.0.0.1:8080 in your browser.

Output

192.168.33.1 - - [04/Oct/2019:21:12:28 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.75 Safari/537.36" "-"
192.168.33.1 - - [04/Oct/2019:21:12:28 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "http://192.168.33.71:8080/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.75 Safari/537.36" "-"

It is recommended to use the docker logs command to access the container logs.

Use CTRL-pCTRL-q to detach from the container without stopping it. The container is stopped by pressing CTRL-c.

You can transmit instructions to the running processes you are attached to if they accept input.

Get a Shell to a Container

You can issue commands inside a container that is currently running by using the docker exec command.

Start a new container first to see how the exec command functions and how it may be used to access the container shell. We will use the official Nginx image:

docker container run --name my_nginx -d nginx

A “my_nginx” container will be created as a result.

Run the following command to execute a command within the container:

docker container exec -it my_nginx ls /var

The -i option stands for interactive, and -t instructs Docker to allocate a pseudo-TTY device. All files and directories in the /var directory of the container will be listed by the ls command:

Output

backups  cache	lib  local  lock  log  mail  opt  run  spool  tmp

Start a new shell session by executing the shell binary to get a shell to the container, i.e., to enter the container. You can use sh, bash, or any other shell that is present in the image.

To create a new Bash session inside the container, run the following command:

docker container exec -it my_nginx /bin/bash

You will notice a change in your command prompt, signifying that you are now working on the container shell.

From this point, you can issue commands just like you would on another Linux server. For instance, type env to see a list of the current environment variables:

env

Here is how the output should appear:

Output

HOSTNAME=e0214d97e0fe
MYSQL_ROOT_PASSWORD=my-secret-pw
PWD=/
HOME=/root
MYSQL_MAJOR=8.0
GOSU_VERSION=1.7
MYSQL_VERSION=8.0.17-1debian9
TERM=xterm
SHLVL=1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
_=/usr/bin/env

FAQs to Connect to a Docker Container

Can I connect to a Docker container without using the command line? 

Yes, besides the command line, there are graphical user interface (GUI) tools available, such as Portainer and Kitematic, that provide a visual interface for managing and connecting to Docker containers.

How can I list all running Docker containers? 

To list all running Docker containers, use the command docker ps, which will display the container ID, name, status, and other relevant information.

How do I exit a Docker container's shell without stopping the container? 

To exit a Docker container's shell without stopping the container itself, simply type exit or press Ctrl + D in the terminal session.

Can I connect to a Docker container running on a remote server? 

Yes, you can connect to a Docker container running on a remote server by using SSH to access the server first, and then using the docker exec command to connect to the container.

Can I connect to multiple Docker containers simultaneously? 

Yes, you can connect to multiple Docker containers simultaneously by opening separate terminal sessions or using tools that provide a multi-container management interface.

How do I connect to the command-line interface (CLI) of a Docker container?

 To connect to the command-line interface of a Docker container, use the docker exec -it command followed by the container's name or ID, and the command to access the shell, such as bash.

Can I connect to a Docker container if it is not currently running?

 No, you cannot connect to a Docker container if it is not running. Containers need to be running for you to be able to connect and interact with them.

Conclusion

To summarize, connecting to a Docker container allows you to access and interact with the running application or services inside the container. Additionally, there are GUI tools available for a visual interface to connect and manage Docker containers.

By understanding how to connect to a Docker container, you can effectively monitor, troubleshoot, and perform necessary tasks within the container environment.

If you have any suggestions or queries, kindly leave them in the comments section.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.