04. Accessing Docker Containers & Using Volumes
04. Accessing Docker Containers & Using Volumes
Accessing Docker Containers & Using Volumes
Prerequisites
1. Accessing a Running Container
✔️ Using docker exec
1
docker exec -it <container_id> bash
-it→ interactive terminalbash→ shell inside container
Example:
1
docker exec -it 2a8dda3 bash
👉 Allows you to:
- Inspect files
- Run commands
- Debug application
| Command | Purpose |
|---|---|
| docker run | Create + start new container |
| docker exec | Enter already running container |
2. Volume (Persistent Storage)
| Feature | Bind Mount (-v path:path) | Volume (-v name:path) |
|---|---|---|
| Location | User-defined | Docker-managed |
| Use case | Development | Production |
| Portability | Low | High |
2-1. Why Data Disappears (Important)
Containers are ephemeral
- When container is deleted → data is gone
- Because data lives in writable layer
2-2. Solution: Volume (Persistent Storage)
✔️ What is a Volume?
A volume is a way to store data outside the container
- Data persists even if container is deleted
- Can be shared across containers
✔️ Basic Syntax (Bind Mount)
1
docker run -v <host_path>:<container_path> <image>
✔️ Example
1
docker run -v C:\data:/app/data ubuntu
👉 Meaning:
1
Host (C:\data) ←→ Container (/app/data)
- Changes in container → reflected on host
- Changes in host → reflected in container
✔️ Create volume (Volume)
1
docker volume create my-volume
✔️ Use volume
1
docker run -v my-volume:/app/data ubuntu
Docker manages storage internally
1
2
3
4
5
6
7
8
[ Host Disk ]
└── /var/lib/docker/volumes/my-volume/_data ← Real Data
or \\wsl$\docker-desktop-data\data\docker\volumes\my-volume
↑ mount
[ Container ]
└── /app/data ← Container
This post is licensed under CC BY 4.0 by the author.