Table of Contents
Key Takeaways
The Official WordPress Docker Image
Docker Compose
Conclusion
Frequently Asked Questions (FAQs) about Using the Official Docker WordPress Image
What is the Official Docker WordPress Image?
How do I install the Official Docker WordPress Image?
How do I run a WordPress site using the Docker WordPress Image?
How do I configure the Docker WordPress Image?
How do I update the Docker WordPress Image?
How do I backup my WordPress site running in a Docker container?
How do I troubleshoot issues with the Docker WordPress Image?
Can I use the Docker WordPress Image in a production environment?
How do I secure my WordPress site running in a Docker container?
How do I scale my WordPress site running in a Docker container?
Home CMS Tutorial WordPress How to Use the Official Docker WordPress Image

How to Use the Official Docker WordPress Image

Feb 17, 2025 am 11:52 AM

How to Use the Official Docker WordPress Image

Key Takeaways

  • The Official Docker WordPress Image simplifies the process of setting up Docker. To use it, create a new folder, build a MySQL container, and pull the WordPress image from Docker Hub. The image uses default values for unspecified environment variables when first created.
  • Docker Compose is a tool that simplifies the management of multiple containers. It uses a docker-compose.yml file to build the WordPress and MySQL containers. It allows for the creation of multiple containers of one type, and each container’s name is derived from the parent folder of docker-compose.yml, the container name, and the container number.
  • To access WordPress files on the local filesystem, use the command -v “$PWD/”:/var/www/html. Docker Compose allows for the mapping of two folders, one on the container and one on the local filesystem. The WordPress files are primarily located in the wp-content folder.

In our previous articles in this series, we covered what Docker is, and then how to manually build Docker containers for WordPress.

How to Use the Official Docker WordPress Image

In this article I’ll show you how to set up Docker in a much easier way. Docker has an official image for WordPress that makes it easier to get started. We’ll also check out Docker Compose for a Heroku-like configuration. After all, building and managing containers manually can take a lot of work.

The Official WordPress Docker Image

Before we get started, make sure you create a new folder and navigate inside that folder.

In my previous article we built a MySQL container. The official WordPress image (by the Docker team) requires you to have a running MySQL container.

docker run --name wordpressdb <span>-e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=wordpress -d mysql:5.7</span>
Copy after login
Copy after login
Copy after login

Next, search for ‘WordPress’ on Docker Hub and pull that image.

docker pull wordpress
Copy after login
Copy after login
How to Use the Official Docker WordPress Image

Then, build a container from it.

docker run <span>-e WORDPRESS_DB_PASSWORD=password -d --name wordpress --link wordpressdb:mysql  wordpress</span>
Copy after login
Copy after login

We see two environment variables. WORDPRESS_DB_PASSWORD is the database password for our user. But we haven’t yet specified this. Don’t worry though, this image will use default values for environment variables that we haven’t specified (when the container is first created). For example, WORDPRESS_DB_USER will be root if we don’t define it. The same applies to the database name with wordpress as the default. Check out the official documentation for additional environment variables. -d makes the container to run in the background. We can’t see the IP address of the container, to get this execute docker inspect wordpress to get its IP and port. Mine is 172.17.0.10 and the port is 80. Using docker inspect, you can find lots of useful information about a container.

How to Use the Official Docker WordPress Image

Visit the IP of your wordpress container in your browser and you’ll see something like this:

How to Use the Official Docker WordPress Image

The problem with this example is that we can’t access the WordPress files, also each time we start the container, it will change it’s IP. But we can easily fix both of these issues.

docker run --name wordpressdb <span>-e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=wordpress -d mysql:5.7</span>
Copy after login
Copy after login
Copy after login

This is a very long command, we see two new arguments here. The -p and -v arguments. The -p argument is used for port mapping. Inside the container, Apache runs on port 80. We tell Docker that we’ll use the port 8080 to communicate with the port 80 inside the container. Also, we specified 127.0.0.2 as the IP for this container. -v "$PWD/":/var/www/html will map the two folders. By default, the container puts the WordPress files in the /var/www/html directory which is the filesystem inside the container (this is nothing to do with our local filesystem). Check out your current directory and you’ll see that some additional files are there.

Using your new container, you’ll now be able to modify files in our filesystem. In the previous example, it was much more work to be able to write to our filesystem. This method is a lot simpler. But can it be more simpler? Sure it can!

Docker Compose

Until now, we’ve seen how to create our containers using commands on the terminal. As much as I love Linux and the terminal, I really don’t want to create the containers using the command line and remembering the various arguments all the time. That’s why Docker Compose exists.

This tool started under the name Fig (not by the Docker team) as an Open Source, easier solution to manage multiple containers. Now the tool is officially maintained by Docker (under the name Docker Compose). Before moving to Compose, make sure to first install it. All you need is a docker-compose.yml file and a new project folder. In this file we’ll write all the configuration that is needed to build the wordpress and mysql container. Docker Compose, just like Docker CLI, uses the Docker daemon to create the containers, so basically you can do almost everything that you did with the CLI.

Let’s see an example:

docker run --name wordpressdb <span>-e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=wordpress -d mysql:5.7</span>
Copy after login
Copy after login
Copy after login

This should be familiar to most readers. web and mysql are the names of the containers. When these containers are built from Compose, the actual names are different. A container created from Compose has a name like [foldername]compose_[containername]_[number]. The [foldername] would be the parent folder of docker-compose.yml, [containername] would be web or mysql in this case and [number] would be the number of the container. The good thing about Compose is that you can have more than one container of one type. We could have two web containers. Their names in this case would be [foldername]compose_web_1 and [foldername]compose_web_2. This also applies to other containers that are defined in docker-compose.yml (the mysql container for example).

When using containers of one type, you’ll need to specify separate IP addresses.

docker pull wordpress
Copy after login
Copy after login

This works like -e WORDPRESS_DB_PASSWORD=password.

docker run <span>-e WORDPRESS_DB_PASSWORD=password -d --name wordpress --link wordpressdb:mysql  wordpress</span>
Copy after login
Copy after login

This works like --link mysql:mysql.

docker run -e <span>WORDPRESS_DB_PASSWORD=password -d --name wordpress --link wordpressdb:mysql -p 127.0.0.2:8080:80 -v "$PWD/":/var/www/html  wordpress</span>
Copy after login

This works just like -p 127.0.0.3:8080:80.

To learn more about Docker Compose commands and docker-compose.yml, you’ll find the CLI reference here and docker-compose.yml reference here.

Now that you’ve got your docker-compose.yml file, execute docker-compose up and Compose will start building the containers for you. Navigate to 127.0.0.3:8080 and you’ll again see a welcome page. Now, all we need is access to the WordPress files.

As we’ve discussed previously, to get access to the WordPress files on our local filesystem, we need to do something like -v "$PWD/":/var/www/html. You should add a working directory and map two folders (one on your container and one on your local filesystem). To do this, we just add the following two lines:

web:
    image: wordpress
    links:
     -<span> mysql
</span><span>    environment:
</span>     -<span> <span>WORDPRESS_DB_PASSWORD=password
</span></span><span>    ports:
</span>     -<span> <span>"127.0.0.3:8080:80"
</span></span><span>mysql:
</span>    image: mysql:5.7
    environment:
     -<span> <span>MYSQL_ROOT_PASSWORD=password
</span></span><span>     - <span>MYSQL_DATABASE=wordpress</span></span>
Copy after login

The working_dir tells the WordPress image to install WordPress in that directory. volumes: behaves like -v "$PWD/wordpress":/var/www/html/wp-content. The WordPress files live under /var/www/html folder. We’re really only interested in the wp-content folder, since that’s where our plugins and themes reside. After all, most developers only deal with this folder.

Again, execute docker-compose up. If you’re on Linux, you should change the permissions of the wordpress folder (the local folder) to writable. That’s because the containers are created by the Docker daemon, a process that starts when the system boots (by the sudo user). To fix this execute:

<span>environment:
</span><span>     - WORDPRESS_DB_PASSWORD=password</span>
Copy after login

You’ll now have write access to these folders and you’ll be good to go!

Conclusion

In this article we saw how to use the official WordPress Docker image, with a quick introduction to Docker Compose. If you haven’t checked the previous articles in this series, you can find them below:

  • Introduction to Docker for WordPress Developers
  • How to Manually Build Docker Containers for WordPress

This series wouldn’t be complete without showing you how to deploy a WordPress project. In the next article, I’ll show you how to deploy a Docker-based WordPress project on DigitalOcean. Stay tuned!

If you’ve been reading the series this far, you should now have a much better understanding of the underlying concepts of how Docker works. You should now be able to work with abstractions and tools that make things even easier. If you have any questions please leave a comment below. I know that it’s the third time I’m asking, but I’m very interested if would you consider using Docker in production for your next project?

Frequently Asked Questions (FAQs) about Using the Official Docker WordPress Image

What is the Official Docker WordPress Image?

The Official Docker WordPress Image is a pre-configured Docker image that allows you to quickly set up and run a WordPress site in a Docker container. It includes the latest version of WordPress and is designed to be lightweight, secure, and easy to use. This image is maintained by the Docker community and is regularly updated to include the latest security patches and WordPress updates.

How do I install the Official Docker WordPress Image?

To install the Official Docker WordPress Image, you need to have Docker installed on your system. Once Docker is installed, you can pull the WordPress image from the Docker Hub using the command docker pull wordpress. This will download the image to your system and make it available for use.

How do I run a WordPress site using the Docker WordPress Image?

To run a WordPress site using the Docker WordPress Image, you need to create a Docker container from the image. This can be done using the command docker run --name some-wordpress -p 8080:80 -d wordpress. This command will start a new container named some-wordpress, map port 8080 on your host to port 80 on the container, and run the container in the background.

How do I configure the Docker WordPress Image?

The Docker WordPress Image can be configured using environment variables. These variables can be set when creating the container using the -e option. For example, to set the WordPress database name, you can use the command docker run -e WORDPRESS_DB_NAME=mydbname -d wordpress.

How do I update the Docker WordPress Image?

To update the Docker WordPress Image, you need to pull the latest version of the image from the Docker Hub using the command docker pull wordpress. Once the latest image is downloaded, you can create a new container from the updated image.

How do I backup my WordPress site running in a Docker container?

To backup your WordPress site, you can use the docker cp command to copy the WordPress files from the container to your host system. You can also use a Docker volume to persist the WordPress files and database outside of the container.

How do I troubleshoot issues with the Docker WordPress Image?

To troubleshoot issues with the Docker WordPress Image, you can use the docker logs command to view the logs of the WordPress container. This can provide valuable information about any errors or issues that may be occurring.

Can I use the Docker WordPress Image in a production environment?

Yes, the Docker WordPress Image is designed to be used in both development and production environments. However, for production use, it is recommended to use a Docker Compose file to manage the WordPress and database containers, and to use Docker volumes to persist data.

How do I secure my WordPress site running in a Docker container?

To secure your WordPress site, you should always use the latest version of the Docker WordPress Image, which includes the latest security patches. You should also configure your WordPress site to use HTTPS, and regularly backup your WordPress files and database.

How do I scale my WordPress site running in a Docker container?

To scale your WordPress site, you can use Docker’s built-in scaling features. This includes the ability to create multiple containers from the same image, and to distribute traffic between these containers using a load balancer.

The above is the detailed content of How to Use the Official Docker WordPress Image. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How To Begin A WordPress Blog: A Step-By-Step Guide For Beginners How To Begin A WordPress Blog: A Step-By-Step Guide For Beginners Apr 17, 2025 am 08:25 AM

Blogs are the ideal platform for people to express their opinions, opinions and opinions online. Many newbies are eager to build their own website but are hesitant to worry about technical barriers or cost issues. However, as the platform continues to evolve to meet the capabilities and needs of beginners, it is now starting to become easier than ever. This article will guide you step by step how to build a WordPress blog, from theme selection to using plugins to improve security and performance, helping you create your own website easily. Choose a blog topic and direction Before purchasing a domain name or registering a host, it is best to identify the topics you plan to cover. Personal websites can revolve around travel, cooking, product reviews, music or any hobby that sparks your interests. Focusing on areas you are truly interested in can encourage continuous writing

How to adjust the wordpress article list How to adjust the wordpress article list Apr 20, 2025 am 10:48 AM

There are four ways to adjust the WordPress article list: use theme options, use plugins (such as Post Types Order, WP Post List, Boxy Stuff), use code (add settings in the functions.php file), or modify the WordPress database directly.

How to display child categories on archive page of parent categories How to display child categories on archive page of parent categories Apr 19, 2025 pm 11:54 PM

Do you want to know how to display child categories on the parent category archive page? When you customize a classification archive page, you may need to do this to make it more useful to your visitors. In this article, we will show you how to easily display child categories on the parent category archive page. Why do subcategories appear on parent category archive page? By displaying all child categories on the parent category archive page, you can make them less generic and more useful to visitors. For example, if you run a WordPress blog about books and have a taxonomy called "Theme", you can add sub-taxonomy such as "novel", "non-fiction" so that your readers can

How to get logged in user information in WordPress for personalized results How to get logged in user information in WordPress for personalized results Apr 19, 2025 pm 11:57 PM

Recently, we showed you how to create a personalized experience for users by allowing users to save their favorite posts in a personalized library. You can take personalized results to another level by using their names in some places (i.e., welcome screens). Fortunately, WordPress makes it very easy to get information about logged in users. In this article, we will show you how to retrieve information related to the currently logged in user. We will use the get_currentuserinfo();  function. This can be used anywhere in the theme (header, footer, sidebar, page template, etc.). In order for it to work, the user must be logged in. So we need to use

How to sort posts by post expiration date in WordPress How to sort posts by post expiration date in WordPress Apr 19, 2025 pm 11:48 PM

In the past, we have shared how to use the PostExpirator plugin to expire posts in WordPress. Well, when creating the activity list website, we found this plugin to be very useful. We can easily delete expired activity lists. Secondly, thanks to this plugin, it is also very easy to sort posts by post expiration date. In this article, we will show you how to sort posts by post expiration date in WordPress. Updated code to reflect changes in the plugin to change the custom field name. Thanks Tajim for letting us know in the comments. In our specific project, we use events as custom post types. Now

Is WordPress easy for beginners? Is WordPress easy for beginners? Apr 03, 2025 am 12:02 AM

WordPress is easy for beginners to get started. 1. After logging into the background, the user interface is intuitive and the simple dashboard provides all the necessary function links. 2. Basic operations include creating and editing content. The WYSIWYG editor simplifies content creation. 3. Beginners can expand website functions through plug-ins and themes, and the learning curve exists but can be mastered through practice.

How to display query count and page loading time in WordPress How to display query count and page loading time in WordPress Apr 19, 2025 pm 11:51 PM

One of our users asked other websites how to display the number of queries and page loading time in the footer. You often see this in the footer of your website, and it may display something like: "64 queries in 1.248 seconds". In this article, we will show you how to display the number of queries and page loading time in WordPress. Just paste the following code anywhere you like in the theme file (e.g. footer.php). queriesin

How to Automate WordPress and Social Media with IFTTT (and more) How to Automate WordPress and Social Media with IFTTT (and more) Apr 18, 2025 am 11:27 AM

Are you looking for ways to automate your WordPress website and social media accounts? With automation, you will be able to automatically share your WordPress blog posts or updates on Facebook, Twitter, LinkedIn, Instagram and more. In this article, we will show you how to easily automate WordPress and social media using IFTTT, Zapier, and Uncanny Automator. Why Automate WordPress and Social Media? Automate your WordPre

See all articles