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

Bihar Tourism: Complete Travel Guide to Tourist Places, Culture, Food & Things to Do

The plains along the Ganges hold the foundations of ancient empires, global centers of learning, and the origins of two world religions. Bihar Tourism offers travelers an…

Read More

Complete Guide to Upcoming Events, Tickets & Things to Do

Lucknow is widely known for its classical heritage, historic architecture, and legendary culinary traditions. Beyond its timeless monuments, the city is also home to an active, modern…

Read More

What MedTech Teams Should Understand Before Building SaMD Products

Software is no longer a supporting feature in much of medical technology. It is increasingly the product itself, responsible for interpreting data, guiding clinical decisions, monitoring patients,…

Read More

Best Knee Replacement Hospitals in India: How to Choose the Right Hospital & Surgeon

Facing chronic joint stiffness or learning that a family member may need joint surgery brings up critical healthcare decisions. When daily tasks like climbing stairs, walking around…

Read More

Events in Kolkata: Complete Guide to Upcoming Events, Tickets & Things to Do

Kolkata has a distinct pulse, driven by a rich heritage that effortlessly intersects with a modern, dynamic cultural calendar. From intimate acoustic sessions in heritage cafes to…

Read More

How to Create a Professional Training Agenda in Minutes with the DevOpsSchool Training Agenda Builder

Designing a good training program sounds simple: Choose a topic → divide it into sessions → start teaching. In reality, creating a professional training agenda requires much…

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