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_knuthInspect the container and look for the Env section:
docker inspect cdf886f902d4 | grep -A 10 EnvOutput:
"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 run -d --name blue-app -e APP_COLOR=blue -p 38282:8080 kodekloud/simple-webappTask 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 run --name mysql-db -e MYSQL_ROOT_PASSWORD=db_pass123 -d mysqlTask 4: Run Ubuntu in Detached Mode
Run an Ubuntu container that executes the command sleep 1000.
docker run -d ubuntu sleep 1000Task 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 run \
-p 38282:8080 \
--name blue-app \
kodekloud/simple-webapp:blueDocker Networking
Task 6: Run a Container on the None Network
Create a container named alpine-2 and attach it to the none network.
docker run --name alpine-2 --network none alpineTask 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 network create \
--driver bridge \
--subnet 182.18.0.0/24 \
--gateway 182.18.0.1 \
wp-mysql-networkOptionally, verify the network:
docker network lsTask 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 run \
--name mysql-db \
--network wp-mysql-network \
-e MYSQL_ROOT_PASSWORD=db_pass123 \
-d mysql:5.7Task 9: Deploy a Web Application Connected to MySQL
Deploy the web application and connect it to the MySQL database.
Requirements:
Container name:
webappImage:
kodekloud/simple-webapp-mysqlHost port:
38080Container port:
8080Network:
wp-mysql-networkEnvironment variables:
DB_Host=mysql-dbDB_Password=db_pass123
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-mysqlUnderstanding the Configuration
The application uses:
DB_Hostto locate the MySQL containerDB_Passwordto authenticate with the database
Because both containers are attached to the same Docker network, they can communicate using container names as hostnames.
