Master Docker: ENV, ARG, and Docker Networking (docker0, bridge, custom)

Table of contents

Docker makes application development smoother by providing isolated environments through containers. In this post, weโll deep-dive into:
๐น What is Docker?
๐น Dockerfile:
ENV
vsARG
with examples๐น Docker default network
docker0
๐น Docker
bridge
andcustom bridge
networks๐น When and why to use each
๐ฆ What is Docker?
Docker is an open-source platform that enables developers to automate the deployment of applications inside portable containers. Each container runs independently, along with its own binaries and libraries, yet shares the host OS kernel โ making it more efficient than virtual machines.
Benefits of Docker:
โ Consistency across development and production
โ Faster deployments and CI/CD pipelines
โ Lightweight, reusable infrastructure
โ๏ธ Dockerfile: ENV vs ARG
Environment variables in Docker can be passed at two stages:
๐ง ARG
โ Build-Time Variable
Used during image build only
Cannot be accessed in the running container
FROM alpine
ARG APP_VERSION
RUN echo "Building app version $APP_VERSION"
Build it with:
docker build --build-arg APP_VERSION=1.0.0 -t myapp:1.0 .
๐ ENV
โ Run-Time Variable
Available in both build and runtime
Can be overridden when starting the container
FROM alpine
ENV NAME=Imran
CMD echo "Hello, $NAME"
Run it:
docker run myimage # Hello, Imran
docker run -e NAME=Sheak myimage # Hello, Sheak
โ Use Both Together
FROM alpine
ARG USERNAME
ENV USER=$USERNAME
RUN echo "User: $USER"
CMD ["sh"]
Build and run:
docker build --build-arg USERNAME=sheak -t arg-env-demo .
docker run -e USER=sheak arg-env-demo
๐ Docker Networking Explained
Docker containers communicate over virtual networks created and managed by Docker. There are several types:
Network Type | Description |
bridge | Default for standalone containers |
host | Shares hostโs network |
none | No networking |
overlay | For multi-host (Docker Swarm) |
macvlan | Assigns MAC addresses to containers |
Letโs focus on three relevant types:
๐ docker0: Default Bridge Network
When Docker is installed, it creates a default network interface called docker0
.
All containers run without specifying a network are attached here.
๐ Example
docker run -dit --name alpha alpine sh
docker run -dit --name beta alpine sh
# They both attach to the docker0 network
Now check IPs:
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' alpha
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' beta
Try pinging between containers (install iputils
inside):
docker exec -it alpha sh
apk add iputils
ping <beta_ip>
โ ๏ธ Limitation:
Containers canโt resolve each other by name unless on a user-defined bridge.
๐งฑ Custom Bridge Network: Better Isolation
Creating your own bridge network allows containers to:
Communicate via name resolution
Be isolated from other containers on the default
bridge
networkAvoid conflicts
docker network create mybridge
Run containers in that network:
docker run -dit --name app1 --network mybridge alpine sh
docker run -dit --name app2 --network mybridge alpine sh
Now from app1
:
ping app2 # works via DNS!
๐ง Why Use a Custom Network?
Feature | Default (docker0 ) | Custom Bridge |
Name resolution (DNS) | โ No | โ Yes |
Isolation | โ Shared | โ Separate |
User control (subnet, gateway) | โ None | โ Yes |
Port conflict handling | โ Needs manual port mapping | โ Optional |
Custom networks are recommended in real-world use cases like:
Microservices where services call each other by name (
user-service
,auth-service
)Docker Compose setups (Docker Compose auto-creates a custom network)
CI/CD pipelines for isolated test environments
๐ผ๏ธ Visual Overview
๐ (Use the diagram Understanding_Docker_Networks.png
here)
The image visually shows:
docker0
with multiple containersCustom bridge network with name-based DNS resolution
ENV and ARG usage in Dockerfile
๐งช Bonus: Inspecting Networks
Check all Docker networks:
docker network ls
Inspect a network:
docker network inspect mybridge
Remove a custom network (when no containers are using it):
docker network rm mybridge
๐ Final Thoughts
โ
ARG
is for build-time only
โ
ENV
is available during build and runtime
โ
docker0
is default, but limited
โ
Custom bridge networks offer isolation and DNS-based service discovery
Mastering these tools will help you scale your Docker workflows efficiently โ especially when working with complex deployments and microservices.
๐ Let's Connect!
If you found this helpful, react, share, or leave a comment. Got questions? Iโm always happy to help fellow DevOps learners!
Subscribe to my newsletter
Read articles from sheak imran directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

sheak imran
sheak imran
System Administrator FM Associates BD | Network Specialist | RHCE, RHCSA, RHCVA certified.