🧾 Docker Directory Change, EBS Setup & Custom Networking

Chaitanya VamsiChaitanya Vamsi
3 min read

βœ… 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:

  1. 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).

  2. Check the Volume for Filesystem

     sudo file -s /dev/xvdf
    
    • If it shows data, it has no filesystem yet.
  3. Format the Volume

     sudo mkfs.ext4 /dev/xvdf
    
  4. Get UUID

     sudo lsblk -o +UUID
    
    • Example UUID: 5a8bd636-5809-47fa-927c-027de4350f0c
  5. Create Mount Directory

     sudo mkdir /dockerData
    
  6. Mount Permanently via /etc/fstab

     sudo nano /etc/fstab
    

    Add this line:

     UUID=5a8bd636-5809-47fa-927c-027de4350f0c  /dockerData  ext4  defaults,nofail  0  2
    
  7. 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:

  1. Check Current Docker Root Directory

     docker info | grep 'Docker Root Dir'
    
  2. 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.

  3. Reload and Restart Docker

     sudo systemctl daemon-reexec
     sudo systemctl daemon-reload
     sudo systemctl restart docker
    
  4. 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 (like ping app1). They must use IP addresses instead.

πŸ”§ Setup:

  1. Create a Custom Network

     docker network create custom --driver bridge
    
  2. Verify

     docker network ls
    
  3. 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)
    
  4. 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)
    
  5. Add Existing Container to Custom Network

     docker network connect custom app1
     docker inspect custom
    

βœ… Summary

TaskCommand or File
Format EBS volumesudo mkfs.ext4 /dev/xvdf
Mount EBS via UUIDEdit /etc/fstab
Change Docker storage pathEdit docker.service β†’ --data-root=/dockerData
Restart Docker safelysystemctl daemon-reexec && systemctl restart docker
Create custom Docker networkdocker network create custom --driver bridge
Enable container name pingUse custom network + container names
0
Subscribe to my newsletter

Read articles from Chaitanya Vamsi directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Chaitanya Vamsi
Chaitanya Vamsi