Post

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 terminal
  • bash → shell inside container

Example:

1
docker exec -it 2a8dda3 bash

👉 Allows you to:

  • Inspect files
  • Run commands
  • Debug application
CommandPurpose
docker runCreate + start new container
docker execEnter already running container

2. Volume (Persistent Storage)

FeatureBind Mount (-v path:path)Volume (-v name:path)
LocationUser-definedDocker-managed
Use caseDevelopmentProduction
PortabilityLowHigh

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.