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 Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

Complete Guide to AI Guest Posting with GuestPostAI

In the fast-evolving digital marketing landscape, search engine optimization (SEO) remains the ultimate catalyst for sustainable organic growth. Among the myriad of off-page SEO strategies, guest posting…

Read More

WizBrand Review: The Ultimate All-in-One Digital Marketing Platform for Growth

In the rapidly evolving digital ecosystem, running a successful online marketing strategy often feels like juggling spinning plates—marketing teams routinely find themselves bouncing between disconnected platforms for…

Read More

Best Invoice and Payment Management Software for Growing Businesses

In the fast-paced modern economy, business growth relies heavily on healthy cash flow and efficient financial operations. Yet, countless organizations find themselves bogged down by administrative bottlenecks….

Read More

Mastering Generative AI Workflows: Why You Need the Ultimate AI Prompt Management Tool

Artificial intelligence has rapidly evolved from a futuristic novelty into the core operational engine of modern business, creative production, and software development. Whether you are using large…

Read More

Best KYC Software in 2026

You already know that KYC is a compliance requirement. What most comparisons skip is the part that actually matters once you are past procurement: how these platforms…

Read More

Free Content Publishing Platforms: The Ultimate FreePostFinder Directory

In today’s competitive digital landscape, distributing your content effectively is just as critical as creating it, yet rising ad costs and unpredictable social media algorithms make organic…

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