Skip to content

Commit e11938d

Browse files
author
Prajwal Koirala
authored
Create connect-container.md
1 parent 7308dd6 commit e11938d

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

Docker/connect-container.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
If you've exited or disconnected from a Docker container and want to reconnect to it, you can do so using Docker's built-in commands. Here’s how you can reconnect to a running or stopped container:
2+
3+
### 1. **Reconnect to a Running Container**
4+
5+
If your container is still running, you can use `docker attach` or `docker exec` to reconnect to it.
6+
7+
#### Using `docker attach`
8+
9+
The `docker attach` command will reattach your terminal to the container's main process. Note that this might not always be ideal if the container is running a service like a web server or application that doesn’t handle terminal input/output well.
10+
11+
```bash
12+
docker attach my_ubuntu_container
13+
```
14+
15+
#### Using `docker exec`
16+
17+
A more flexible approach is using `docker exec` to start a new interactive session. This is especially useful if you want to run a new shell session:
18+
19+
```bash
20+
docker exec -it my_ubuntu_container bash
21+
```
22+
23+
This command starts a new Bash shell in the running container, allowing you to interact with it as if you had just started it.
24+
25+
### 2. **Reconnect to a Stopped Container**
26+
27+
If the container is stopped, you'll need to start it again before you can reconnect.
28+
29+
#### Start the Container
30+
31+
First, start the container:
32+
33+
```bash
34+
docker start my_ubuntu_container
35+
```
36+
37+
#### Reconnect to the Container
38+
39+
After starting it, you can use `docker exec` to open a new session:
40+
41+
```bash
42+
docker exec -it my_ubuntu_container bash
43+
```
44+
45+
### 3. **Check Container Status**
46+
47+
To see if your container is running or stopped, use:
48+
49+
```bash
50+
docker ps -a
51+
```
52+
53+
This command lists all containers, their statuses, and their IDs.
54+
55+
### Summary
56+
57+
- **Running Container**: Use `docker exec -it container_name bash` for a new interactive session.
58+
- **Stopped Container**: Use `docker start container_name` to start it, then `docker exec -it container_name bash` to reconnect.
59+
- **Container Status**: Check with `docker ps -a`.
60+
61+
By following these steps, you should be able to reconnect to your Docker container, whether it's currently running or has been stopped.

0 commit comments

Comments
 (0)