Profile picture
Sidharth R
  • Home
  • Posts
  • Journal
  • Labs
  • Home
  • Posts
  • Journal
  • Search
  1. Home
  2. Labs
  3. Docker labs
  4. Docker Environment Variables and Networking

Docker Environment Variables and Networking

In this lab, we’ll explore:

  • Inspecting environment variables in running containers
  • Passing environment variables at runtime
  • Deploying MySQL containers
  • Creating custom Docker networks
  • Connecting applications and databases across containers

Task 1: Inspect Environment Variables

First, identify the value configured for the APP_COLOR environment variable in a running container.

List the running containers:

$ docker ps -a
CONTAINER ID   IMAGE                     COMMAND           CREATED          STATUS          PORTS      NAMES
cdf886f902d4   kodekloud/simple-webapp   "python app.py"   10 minutes ago   Up 10 minutes   8080/tcp   mystifying_knuth

Inspect the container and look for the Env section:

DOCKER
docker inspect cdf886f902d4 | grep -A 10 Env

Output:

TEXT
"Env": [
    "APP_COLOR=pink",
    "PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
    "LANG=C.UTF-8",
    "GPG_KEY=0D96DF4D4110E5C43FBFB17F2D347EA6AA65421D",
    "PYTHON_VERSION=3.6.6",
    "PYTHON_PIP_VERSION=18.1"
],

The value of APP_COLOR is pink.

Task 2: Run a Web Application with Environment Variables

Run a container named blue-app using the kodekloud/simple-webapp image.

Requirements:

  • Container name: blue-app
  • Set APP_COLOR=blue
  • Expose container port 8080
  • Map it to host port 38282
DOCKER
docker run -d --name blue-app -e APP_COLOR=blue -p 38282:8080 kodekloud/simple-webapp

Task 3: Deploy a MySQL Database

Deploy a MySQL container named mysql-db.

Requirements:

  • Image: mysql
  • Container name: mysql-db
  • Root password: db_pass123

The MySQL image expects the root password through the MYSQL_ROOT_PASSWORD environment variable.

DOCKER
docker run --name mysql-db -e MYSQL_ROOT_PASSWORD=db_pass123 -d mysql

Task 4: Run Ubuntu in Detached Mode

Run an Ubuntu container that executes the command sleep 1000.

DOCKER
docker run -d ubuntu sleep 1000

Task 5: Run a Tagged Web Application Image

Deploy the blue version of the web application.

Requirements:

  • Image: kodekloud/simple-webapp:blue
  • Container name: blue-app
  • Host port: 38282
  • Container port: 8080
DOCKER
docker run \
  -p 38282:8080 \
  --name blue-app \
  kodekloud/simple-webapp:blue

Docker Networking

Task 6: Run a Container on the None Network

Create a container named alpine-2 and attach it to the none network.

DOCKER
docker run --name alpine-2 --network none alpine

Task 7: Create a Custom Bridge Network

Create a custom bridge network named wp-mysql-network.

Requirements:

  • Subnet: 182.18.0.0/24
  • Gateway: 182.18.0.1
DOCKER
docker network create \
  --driver bridge \
  --subnet 182.18.0.0/24 \
  --gateway 182.18.0.1 \
  wp-mysql-network

Optionally, verify the network:

DOCKER
docker network ls

Task 8: Deploy MySQL on the Custom Network

Run a MySQL 5.7 container attached to the custom network.

Requirements:

  • Image: mysql:5.7
  • Name: mysql-db
  • Network: wp-mysql-network
  • Root password: db_pass123
DOCKER
docker run \
  --name mysql-db \
  --network wp-mysql-network \
  -e MYSQL_ROOT_PASSWORD=db_pass123 \
  -d mysql:5.7

Task 9: Deploy a Web Application Connected to MySQL

Deploy the web application and connect it to the MySQL database.

Requirements:

  • Container name: webapp

  • Image: kodekloud/simple-webapp-mysql

  • Host port: 38080

  • Container port: 8080

  • Network: wp-mysql-network

  • Environment variables:

    • DB_Host=mysql-db
    • DB_Password=db_pass123
DOCKER
docker run \
  --name webapp \
  -d \
  -p 38080:8080 \
  --network wp-mysql-network \
  -e DB_Host=mysql-db \
  -e DB_Password=db_pass123 \
  --link mysql-db:mysql-db \
  kodekloud/simple-webapp-mysql

Understanding the Configuration

The application uses:

  • DB_Host to locate the MySQL container
  • DB_Password to authenticate with the database

Because both containers are attached to the same Docker network, they can communicate using container names as hostnames.

Nerdsid.com

Links
  • Home
  • Contact
  • About
  • Posts
  • Journal
  • Quotes
  • Links worth your time
  • Notes
  • Style guide
© 2026 Sidharth R.
All content on this website is licensed under CC BY-NC-SA 4.0.