Find the Best Cosmetic Hospitals

Explore trusted cosmetic hospitals and make a confident choice for your transformation.

“Invest in yourself — your confidence is always worth it.”

Explore Cosmetic Hospitals

Start your journey today — compare options in one place.

Docker Compose: WordPress + MySQL Using Shared Environment Variables

Create a project folder:

mkdir wordpress-env-demo
cd wordpress-env-demo

Create a .env file:

MYSQL_DATABASE=wordpressdb
MYSQL_USER=wpuser
MYSQL_PASSWORD=wppassword123
MYSQL_ROOT_PASSWORD=rootpassword123

WORDPRESS_PORT=8080

Create docker-compose.yml:

services:
  db:
    image: mysql:latest
    container_name: wordpress_mysql
    restart: always
    environment:
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
    volumes:
      - mysql_data:/var/lib/mysql

  wordpress:
    image: wordpress:latest
    container_name: wordpress_app
    restart: always
    depends_on:
      - db
    ports:
      - "${WORDPRESS_PORT}:80"
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_NAME: ${MYSQL_DATABASE}
      WORDPRESS_DB_USER: ${MYSQL_USER}
      WORDPRESS_DB_PASSWORD: ${MYSQL_PASSWORD}
    volumes:
      - wordpress_data:/var/www/html

volumes:
  mysql_data:
  wordpress_data:
Code language: JavaScript (javascript)

Start the containers:

docker compose up -d

Open WordPress:

http://localhost:8080
Code language: JavaScript (javascript)

Now students can see how environment variables are passed into both containers.

Check MySQL container env:

docker compose exec db printenv MYSQL_DATABASE
docker compose exec db printenv MYSQL_USER
docker compose exec db printenv MYSQL_PASSWORD

Check WordPress container env:

docker compose exec wordpress printenv WORDPRESS_DB_HOST
docker compose exec wordpress printenv WORDPRESS_DB_NAME
docker compose exec wordpress printenv WORDPRESS_DB_USER
docker compose exec wordpress printenv WORDPRESS_DB_PASSWORD

Verify both containers are using the same password:

docker compose exec db printenv MYSQL_PASSWORD
docker compose exec wordpress printenv WORDPRESS_DB_PASSWORD

Check how WordPress reads env and creates database config:

docker compose exec wordpress bash -lc 'grep DB_ /var/www/html/wp-config.php'
Code language: JavaScript (javascript)

Test MySQL login using the env variable inside DB container:

docker compose exec db bash -lc 'mysql -u$MYSQL_USER -p$MYSQL_PASSWORD $MYSQL_DATABASE -e "SHOW TABLES;"'
Code language: JavaScript (javascript)

Check service-name networking between containers:

docker compose exec wordpress bash -lc 'getent hosts db'
Code language: JavaScript (javascript)

This works because Docker Compose creates an internal network where db becomes the hostname of the MySQL container.

Stop everything:

docker compose down

Stop and delete volumes also:

docker compose down -v

For teaching, this is perfect. For production, avoid latest, use fixed versions like wordpress:6.8-apache and mysql:8.4, and never commit real passwords into Git.

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at <a href="https://www.cotocus.com/">Cotocus</a>. I share tech blog at <a href="https://www.devopsschool.com/">DevOps School</a>, travel stories at <a href="https://www.holidaylandmark.com/">Holiday Landmark</a>, stock market tips at <a href="https://www.stocksmantra.in/">Stocks Mantra</a>, health and fitness guidance at <a href="https://www.mymedicplus.com/">My Medic Plus</a>, product reviews at <a href="https://www.truereviewnow.com/">TrueReviewNow</a> , and SEO strategies at <a href="https://www.wizbrand.com/">Wizbrand.</a> Do you want to learn <a href="https://www.quantumuting.com/">Quantum Computing</a>? <strong>Please find my social handles as below;</strong> <a href="https://www.rajeshkumar.xyz/">Rajesh Kumar Personal Website</a> <a href="https://www.youtube.com/TheDevOpsSchool">Rajesh Kumar at YOUTUBE</a> <a href="https://www.instagram.com/rajeshkumarin">Rajesh Kumar at INSTAGRAM</a> <a href="https://x.com/RajeshKumarIn">Rajesh Kumar at X</a> <a href="https://www.facebook.com/RajeshKumarLog">Rajesh Kumar at FACEBOOK</a> <a href="https://www.linkedin.com/in/rajeshkumarin/">Rajesh Kumar at LINKEDIN</a> <a href="https://www.wizbrand.com/rajeshkumar">Rajesh Kumar at WIZBRAND</a> <a href="https://www.rajeshkumar.xyz/dailylogs">Rajesh Kumar DailyLogs</a>

Related Posts

OpenShift ImageStreams vs Normal Container Images: A Complete Hands-on Tutorial Using HTTPD

1. Introduction OpenShift can deploy applications in two major ways: Both approaches ultimately run containers in Pods, but they behave very differently in terms of tracking, automation,…

Read More

Kubernetes ConfigMap vs Secret — Updated Reference for Kubernetes

As of July 4, 2026, the current stable upstream Kubernetes line is Kubernetes v1.36, with v1.36.2 released on June 9, 2026. Kubernetes v1.37 is in release-cycle/pre-release status,…

Read More

Top 10 AI Case Management Prioritization (Social Services) Tools: Features, Pros, Cons & Comparison

Introduction Social services organizations—including child welfare agencies, disability support programs, housing assistance departments, unemployment systems, and public health outreach units—handle an extremely high volume of complex, sensitive,…

Read More

Top 10 AI Permit Application Auto-Review Tools: Features, Pros, Cons & Comparison

Introduction Government agencies, municipal authorities, urban planning departments, and regulatory bodies handle millions of permit applications every year—ranging from building permits, environmental clearances, zoning approvals, construction licenses,…

Read More

Top 10 AI Disaster Response Forecasting Tools: Features, Pros, Cons & Comparison

Introduction Natural disasters such as floods, hurricanes, earthquakes, wildfires, heatwaves, landslides, and cyclones are becoming more frequent, more intense, and harder to predict with traditional models alone….

Read More

Top 10 AI Emergency Call Triage Assistants: Features, Pros, Cons & Comparison

Introduction Emergency response systems are the backbone of public safety infrastructure, handling millions of calls related to medical emergencies, fire incidents, accidents, crime reporting, and disaster situations….

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x