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

sheak imransheak imran
4 min read

Docker makes application development smoother by providing isolated environments through containers. In this post, weโ€™ll deep-dive into:

  • ๐Ÿ”น What is Docker?

  • ๐Ÿ”น Dockerfile: ENV vs ARG with examples

  • ๐Ÿ”น Docker default network docker0

  • ๐Ÿ”น Docker bridge and custom 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 TypeDescription
bridgeDefault for standalone containers
hostShares hostโ€™s network
noneNo networking
overlayFor multi-host (Docker Swarm)
macvlanAssigns 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 network

  • Avoid 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?

FeatureDefault (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 containers

  • Custom 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!

0
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.