π§Ύ Docker Directory Change, EBS Setup & Custom Networking


β PART 1 β Attach and Mount a New EBS Volume to EC2
π Goal:
Avoid root volume exceeding 50% usage by moving Docker data to a new EBS volume.
π§ Steps:
Create a New EBS Volume
Must be in same region and AZ as the EC2 instance.
Attach it to the EC2 instance (e.g., as
/dev/xvdf
).
Check the Volume for Filesystem
sudo file -s /dev/xvdf
- If it shows
data
, it has no filesystem yet.
- If it shows
Format the Volume
sudo mkfs.ext4 /dev/xvdf
Get UUID
sudo lsblk -o +UUID
- Example UUID:
5a8bd636-5809-47fa-927c-027de4350f0c
- Example UUID:
Create Mount Directory
sudo mkdir /dockerData
Mount Permanently via
/etc/fstab
sudo nano /etc/fstab
Add this line:
UUID=5a8bd636-5809-47fa-927c-027de4350f0c /dockerData ext4 defaults,nofail 0 2
Mount Immediately (optional)
sudo mount -a
β PART 2 β Change Docker's Default Data Directory
π Goal:
Move Docker storage to the new volume mounted at /dockerData
.
π§ Steps:
Check Current Docker Root Directory
docker info | grep 'Docker Root Dir'
Edit Docker Service File
sudo nano /lib/systemd/system/docker.service
Replace the
ExecStart
line:ExecStart=/usr/bin/dockerd --data-root /dockerData -H fd:// --containerd=/run/containerd/containerd.sock
π This replaces the default root
/var/lib/docker
.Reload and Restart Docker
sudo systemctl daemon-reexec sudo systemctl daemon-reload sudo systemctl restart docker
Verify
docker info | grep 'Docker Root Dir'
Should show:
Docker Root Dir: /dockerData
β PART 3 β Create and Use a Custom Docker Network
π Why?
Containers are stateless and may change IPs after restart.
Default
bridge
network does not allow container name resolution.When we say "the default bridge network does not allow container name resolution", we mean that:
Containers connected to the default
bridge
network cannot communicate using each otherβs container names (likeping app1
). They must use IP addresses instead.
π§ Setup:
Create a Custom Network
docker network create custom --driver bridge
Verify
docker network ls
Run Containers in Bridge (non-custom) β Names don't resolve
docker run --rm -d --name app1 -p 8000:80 nginx:latest docker run --rm -d --name app2 -p 8001:80 nginx:latest
docker exec -it app1 bash apt update && apt install iputils-ping ping <app2 IP> β ping app2 β (fails in default bridge)
Run Containers in Custom Network β Name resolution works
docker run --rm -d --name app3 -p 8003:80 --network custom nginx:latest docker run --rm -d --name app4 -p 8004:80 --network custom nginx:latest
Inside
app3
container:ping app4 β (name-based ping works)
Add Existing Container to Custom Network
docker network connect custom app1 docker inspect custom
β Summary
Task | Command or File |
Format EBS volume | sudo mkfs.ext4 /dev/xvdf |
Mount EBS via UUID | Edit /etc/fstab |
Change Docker storage path | Edit docker.service β --data-root=/dockerData |
Restart Docker safely | systemctl daemon-reexec && systemctl restart docker |
Create custom Docker network | docker network create custom --driver bridge |
Enable container name ping | Use custom network + container names |
Subscribe to my newsletter
Read articles from Chaitanya Vamsi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
