Run WordPress in Docker: A Simple Guide

3 min


0
Run WordPress in Docker

If you’re looking to set up a WordPress website and want to use Docker, you’re in the right place. Docker is an awesome tool that makes managing your WordPress site super easy. In this blog post, I’ll walk you through how to run WordPress in Docker without needing to use Docker Compose. Whether you’re a beginner or just looking for a refresher, this guide will help you get started quickly!

What is Docker and Why Use It for WordPress?

Before we dive into the steps, letโ€™s quickly cover why Docker is such a great choice for running WordPress.

Docker is a platform that lets you run applications in isolated environments, known as “containers.” This makes it really easy to run things like WordPress because you donโ€™t have to worry about installing everything on your local machine. Docker handles the setup, dependencies, and even the underlying operating system. Itโ€™s a clean and easy way to manage and deploy applications.

Now that we know the basics, letโ€™s get started on how to run WordPress in Docker!

Step 1: Set Up MySQL in Docker

Before running WordPress, we need a MySQL container. WordPress relies on MySQL to store all the posts, pages, comments, and settings for your site. Here’s how to get that running:

Run the following command to start MySQL in Docker:

docker run --name wordpress_mysql_container \
    -e MYSQL_ROOT_PASSWORD=root_password \
    -e MYSQL_DATABASE=wordpress_db \
    -e MYSQL_USER=wordpress_user \
    -e MYSQL_PASSWORD=wordpress_password \
    -v mysql_data:/var/lib/mysql \
    -d mysql:5.7

Hereโ€™s a breakdown:

  • –name wordpress_mysql_container: This gives the container a name (wordpress_mysql_container).
  • -e MYSQL_ environment variables*: These set the password, database name, and user for the MySQL container.
  • -v mysql_data:/var/lib/mysql: This sets up a volume so that your database data is persistent.
  • -d mysql:5.7: This runs the MySQL container in the background.

Once this is done, your MySQL database is ready for WordPress to connect to.

Step 2: Run WordPress in Docker

Next, we need to run WordPress itself. Here’s how to start WordPress in Docker:

docker run --name wordpress_container \
    --link wordpress_mysql_container:mysql \
    -e WORDPRESS_DB_HOST=wordpress_mysql_container:3306 \
    -e WORDPRESS_DB_USER=wordpress_user \
    -e WORDPRESS_DB_PASSWORD=wordpress_password \
    -e WORDPRESS_DB_NAME=wordpress_db \
    -p 8080:80 \
    -v wordpress_data:/var/www/html \
    -d wordpress:latest

Let me explain:

  • –name wordpress_container: This gives the WordPress container a name (wordpress_container).
  • –link wordpress_mysql_container:mysql: This links the WordPress container to the MySQL container.
  • -e WORDPRESS_ environment variables*: These set the connection details for WordPress to connect to MySQL.
  • -p 8080:80: This exposes port 8080 on your local machine, so you can access WordPress at http://localhost:8080.
  • -v wordpress_data:/var/www/html: This keeps your WordPress files persistent, even if the container is removed.

Step 3: Access Your WordPress Site

Once the WordPress container is up and running, open your web browser and go to:

http://localhost:8080

This should bring up the WordPress installation page. Follow the instructions to complete the setup, and youโ€™ll have a fully functional WordPress site running in Docker!

Step 4: Update the WordPress URL in the Database

Now, letโ€™s say you want to update the WordPress URL (maybe youโ€™re changing domains or running it locally). To do this, youโ€™ll need to access the WordPress database directly. Hereโ€™s how:

1. Access MySQL in Docker:Run this command to get into the MySQL container:

docker exec -it wordpress_mysql_container mysql -uwordpress_user -p

2. Select the WordPress Database:

After logging in, select your WordPress database:

USE wordpress_db;

3. Update the URL:

Run these SQL queries to change the WordPress URL:

UPDATE wp_options SET option_value = 'http://new-domain.com' WHERE option_name = 'siteurl';
UPDATE wp_options SET option_value = 'http://new-domain.com' WHERE option_name = 'home';

Replace http://new-domain.com with your new URL.

4. Exit MySQL:

Once done, exit the MySQL shell:

exit

Step 5: Stopping and Managing Docker Containers

When youโ€™re done, you might want to stop or remove the containers. To stop the containers, run:

docker stop wordpress_container wordpress_mysql_container

And if you want to remove them completely:

docker rm wordpress_container wordpress_mysql_container

Wrapping Up: Run WordPress in Docker with Ease

And thatโ€™s it! Youโ€™ve just learned how to run WordPress in Docker without using Docker Compose. Docker makes it super simple to set up a WordPress site, and this method gives you full control over the environment.

Whether you’re a beginner or an experienced developer, Docker is a great tool for running WordPress locally or even for deploying your website to production. Give it a try, and let me know how it goes!

If you want to learn more about Docker or WordPress, feel free to check out my blog at Blog Bounty Tutorials. Happy blogging!

By using Docker to run WordPress, you can create a clean, isolated environment that’s easy to manage and scale. Itโ€™s a fantastic solution whether you’re setting up a local development environment or preparing to deploy your WordPress site to production!


Like it? Share with your friends!

0

What's Your Reaction?

hate hate
0
hate
confused confused
0
confused
fail fail
0
fail
fun fun
0
fun
geeky geeky
0
geeky
love love
0
love
lol lol
0
lol
omg omg
0
omg
win win
0
win
Amitesh Kumar

Senior

Hide Ads for Premium Members by Subscribing
Hide Ads for Premium Members. Hide Ads for Premium Members by clicking on subscribe button.
Subscribe Now
Choose A Format
Personality quiz
Series of questions that intends to reveal something about the personality
Trivia quiz
Series of questions with right and wrong answers that intends to check knowledge
Poll
Voting to make decisions or determine opinions
Story
Formatted Text with Embeds and Visuals
List
The Classic Internet Listicles
Countdown
The Classic Internet Countdowns
Open List
Submit your own item and vote up for the best submission
Ranked List
Upvote or downvote to decide the best list item
Meme
Upload your own images to make custom memes
Video
Youtube and Vimeo Embeds
Audio
Soundcloud or Mixcloud Embeds
Image
Photo or GIF
Gif
GIF format