search
HomeCMS TutorialWordPressHow to Use the Official Docker WordPress Image

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>

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

docker pull wordpress
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>

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>

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>

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

This works like -e WORDPRESS_DB_PASSWORD=password.

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

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>

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>

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>

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
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
How to easily move your blog from WordPress.com to WordPress.orgHow to easily move your blog from WordPress.com to WordPress.orgApr 18, 2025 am 11:33 AM

Do you want to move your blog from WordPress.com to WordPress.org? Many beginners start with WordPress.com but quickly realize their limitations and want to switch to the self-hosted WordPress.org platform. In this step-by-step guide, we will show you how to properly move your blog from WordPress.com to WordPress.org. Why migrate from WordPress.com to WordPress.org? WordPress.com allows anyone to create an account

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

How to Fix Custom Menu Item Limits in WordPressHow to Fix Custom Menu Item Limits in WordPressApr 18, 2025 am 11:18 AM

Just a few days ago, one of our users reported an unusual problem. The problem is that he reaches the limit of custom menu items. Any content he saves after reaching the menu item limit will not be saved at all. We've never heard of this issue, so we decided to give it a try on our local installation. More than 200 menu items were created and saved. The effect is very good. Move 100 items to the drop-down list and save them very well. Then we knew it had to do with the server. After further research, it seems that many others have encountered the same problem. After digging deeper, we found a trac ticket ( #14134 ) that highlighted this issue. Read very

How to add custom metafields to custom classification in WordPressHow to add custom metafields to custom classification in WordPressApr 18, 2025 am 11:11 AM

Do you need to add custom metafields to custom taxonomy in WordPress? Custom taxonomy allows you to organize content besides categories and tags. Sometimes it is useful to add other fields to describe them. In this article, we will show you how to add other metafields to the taxonomy they create. When should custom metafields be added to custom taxonomy? When you create new content on your WordPress site, you can organize it using two default taxonomy (category and tag). Some websites benefit from the use of custom taxonomy. These allow you to sort content in other ways. For example,

How to Remotely Publish to WordPress using Windows Live WriterHow to Remotely Publish to WordPress using Windows Live WriterApr 18, 2025 am 11:02 AM

Windows live writer is a versatile tool that allows you to post posts directly from your desktop to your WordPress blog. This means you don't need to log in to the WordPress admin panel to update your blog at all. In this tutorial, I will show you how to enable desktop publishing for your WordPress blog using Windows Live Writer. How to set up Windows Live Writer on WordPress Step 1: To use Windows Live Writer in WordPr

How to Fix White Text and Missing Buttons in WordPress Visual EditorHow to Fix White Text and Missing Buttons in WordPress Visual EditorApr 18, 2025 am 10:52 AM

Recently, one of our users reported a very strange installation problem. When writing a post, they can’t see anything they write. Because the text in the post editor is white. What's more, all the visual editor buttons are missing, and the ability to switch from visual to HTML doesn't work either. In this article, we will show you how to fix the white text and missing button issues in the WordPress visual editor. Be a Beginner Note: If you are looking for hidden buttons that may be seen in screenshots of other websites, you may be looking for a kitchen sink. You have to click on the kitchen sink icon to see other options such as underline, copy from word, etc.

How to display avatar in user email in WordPressHow to display avatar in user email in WordPressApr 18, 2025 am 10:51 AM

Do you want to display avatars in user emails in WordPress? Gravatar is a network service that connects a user's email address to an online avatar. WordPress automatically displays visitors’ profile pictures in the comments section, but you may also want to add them to other areas of the site. In this article, we will show you how to display avatars in user emails in WordPress. What is Gravatar and why should I display it? Gravatar stands for globally recognized avatars, which allows people to link images to their email addresses. If the website supports

How to change the default media upload location in WordPressHow to change the default media upload location in WordPressApr 18, 2025 am 10:47 AM

Do you want to change the default media upload location in WordPress? Moving media files to other folders can improve website speed and performance and help you create backups faster. It also gives you the freedom to organize your files in the way that suits you best. In this article, we will show you how to change the default media upload location in WordPress. Why change the default media upload location? By default, WordPress stores all images and other media files in the /wp-content/uploads/ folder. In this folder you will find children of different years and months

See all articles

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

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.