In the application deployment approaches, there are instances where the developer wants to interact with containerized applications or integrate into a running container via the shell prompt. Moreover, in the case of halting or detaching the running containers. In such situations, attaching and detaching from Docker containers is of great aid in debugging the application issues.
How to Attach and Detach from Docker Containers?
There are various modes in which the Docker containers can run, listed below:
-
- Default Mode
- Interactive Mode
- Detached Mode
Let’s delve into each of the modes individually.
Default Mode
A container can be created using the “container run” cmdlet. It is such that the container starts in the foreground:
The above-executed cmdlet initiates the “NGINX” web server in a container. Also, in default mode, returning back to the shell prompt is impossible as long as the container process is executing. Therefore, stop/halt the container by hitting the “CTRL + C” keys.
Now, check the status of the container via the below-stated cmdlet:
Here, it can be implied that the container is “Exited”. It occurs as the “CTRL + C” sends/transmits a “SIGKILL” signal to the container, which terminates/ends the container.
Interactive Mode
Likewise, the “container run” command can also be executed to initiate the container in the interactive mode using “-i” and “-t” parameters/options:
-
- The “-i” option attaches/integrates the stdin of the terminal with the container.
- The “-t” option, however, assigns a pseudo-terminal. This enables the developer to interact with the container via the terminal:
Now, exit/close this container via the below-given cmdlet using its id:
After that, analyze the container’s status via the discussed cmdlet:
From here, it can be verified that the container has transitioned to the “Exited” status.
Detached Mode
Docker enables the developer to execute the container in the background as well via the “-d” option using the following cmdlet:
This cmdlet executes a container in the background, displays its id, and gives a shell. The “-p” option publishes a port that maps port 80 in the container to port 80 on the Docker host.
Now, verify it by analyzing the container’s status:
Here, it can be implied that the container is executing in the background.
How to Attach to a Running/Executing Docker Container?
To attach to a running/executing container, utilize the docker “attach” child cmdlet, as follows:
As seen, the logs are displayed on the terminal accordingly.
This cmdlet attaches/integrates input, output, and error streams to the “web-server-3” container.
Note: After running this cmdlet, navigate to the “http://localhost/” URL from the web browser that displays the following message:
Upon doing so, the container logs will be displayed on the terminal and the above cmdlet will be executed.
Note: In the above demonstration, the most recent container i.e., detached mode is attached. Also, note that a stopped container cannot be attached.
Lastly, exit/leave from the container by triggering the “CTRL + C” keys and analyze its status using the below-stated command:
Here, it can be analyzed that the container moves to the “Exited” status successfully.
How to Detach from a Running/Executing Container?
Detaching from the container varies with respect to the mode in which the container is executing. Let’s discuss the detaching from the containers in each mode:
Default Mode
In this mode, the “CTRL + C” keys can be utilized to detach from the container, discussed before that sends/transmits a “SIGKILL” signal to the container. This results in the container to move to the “Exited” status instead of disconnecting the session.
This behavior can be overridden using the “–sig-proxy” option.
To do so, firstly, initiate the container in default mode via the “–sig-proxy” option:
Now, detach from the container via the “CTRL + C” keys and analyze the container’s status:
In the above cmdlet, it can be implied that the container is detached without affecting its running state.
Interactive Mode
Detaching in this specific mode can be carried out likewise, via the discussed “CTRL + C” keys by firstly, starting the container in an interactive mode via the following cmdlet:
In the next step, detach from it by hitting the “Ctrl + C” keys and analyze its status utilizing the below-provided cmdlet:
In this example, as analyzed, the detach key combination terminates/ends the current interactive session only, thereby keeping the container in an executing/running state.
Detached Mode
In this mode, there is a minor change. It is such that the “–sig-proxy” option must be disabled while attaching/integrating to the container. To do so, first of all, start the container in the detached mode via this cmdlet:
After that, attach to it via “–sig-proxy=false” value and exit from it/close it by hitting the “Ctrl + C” keys:
Finally, analyze the container’s status:
In this outcome, it can be observed that the container is detached appropriately without affecting its running state.
Conclusion
To attach from the Docker Containers, the “attach” cmdlet is used and to detach from the Docker container, mostly the “Ctrl+C” keys are used. Also, the stopped container cannot be attached. This article discussed attaching and detaching from Docker containers along with multiple modes in which the Docker containers can be executed.