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

Top 50 Most Popular Technology Certifications Worldwide

Technology certifications have become an important way for IT professionals to validate their technical knowledge, demonstrate practical skills, and advance their careers in an increasingly competitive global…

Read More

The Role of Automation in Modern iGaming Infrastructure

The iGaming industry operates in a fast-moving digital environment where scalability, reliability, security and speed are essential. Online sportsbooks and casino platforms need to handle high traffic,…

Read More

Running LLM Workloads Like Any Other Production Service: A DevOps Playbook for Model APIs

A model API is a dependency like any other, and it deserves the same operational discipline. Photo via Unsplash. When a team ships its first feature built…

Read More

Combining Tech Skills with Higher Education: Strategies for Top University Admissions

The modern higher education landscape has shifted dramatically, moving away from purely standardized metrics toward holistic evaluations where applied, real-world skills matter deeply. Secondary school students who…

Read More

What a Financial Strategy Partner Brings Beyond Basic Advisory

Basic advisory explains what happened in a business. A financial strategy partner helps leaders decide what should happen next. That difference matters when growth creates cash pressure,…

Read More

Designing a production-ready web scraping pipeline that survives blocks, retries, and CI

Most scraping failures in production look like random network noise. The real cause is usually a weak contract between your code and the target site. You deploy…

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