亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Table of Contents
Key Takeaways
Setting up MySQL
Building a WordPress Container
Final Tweaks
Conclusion
Frequently Asked Questions (FAQs) about Building Docker Containers for WordPress
How can I ensure my Docker container for WordPress is secure?
How can I optimize the performance of my Docker container for WordPress?
How can I troubleshoot issues with my Docker container for WordPress?
How can I backup my WordPress site running in a Docker container?
How can I scale my WordPress site running in Docker containers?
How can I automate the deployment of my WordPress site in Docker containers?
How can I manage multiple WordPress sites in Docker containers?
How can I update my WordPress site running in a Docker container?
How can I monitor my WordPress site running in Docker containers?
How can I migrate my existing WordPress site to a Docker container?
Home CMS Tutorial WordPress How to Manually Build Docker Containers for WordPress

How to Manually Build Docker Containers for WordPress

Feb 17, 2025 pm 01:03 PM

How to Manually Build Docker Containers for WordPress

In my previous article, we covered what Docker is and how to get up and running with a few commands. However, we haven’t done anything useful just yet. There are numerous ways to get a WordPress environment using Docker, in this article, I’ll show you how to manually setup Docker containers to work with WordPress. If you’d like a quick intro into Docker, you can jump back to the first article here.

Key Takeaways

  • Docker can be manually set up to work with WordPress by creating containers for MySQL and WordPress. The MySQL container is created using a MySQL image from Docker Hub, while the WordPress container is built from a PHP image.
  • The MySQL container requires an environmental variable to be passed when first created, setting the password for the root user. Additional environmental variables can also be passed to the container, such as MYSQL_DATABASE, which ensures a database with that name is created.
  • The PHP image for the WordPress container does not have the MySQL extension installed by default. This can be fixed by building a container via a Dockerfile, which uses the php:5.6-apache image, installs the mysqli extension, and executes apache2-foreground.
  • The WordPress container needs to be linked with the MySQL container for the database to function. This is done by using the –link argument when running the WordPress container, with the name of the MySQL container as the first part and an alias as the second part.
  • There can be issues with file permissions and the WordPress container’s IP address changing each time it is restarted. These can be addressed by modifying the Dockerfile to include an entrypoint.sh file that ensures write access to the container’s filesystem, and by adding lines to the wp-config.php file that define the ‘Home’ and ‘Site’ URL as the server’s IP address.

Setting up MySQL

Every WordPress installation needs a MySQL database. To do this, we head over to Docker Hub and find a MySQL image.

The Docker team already has a MySQL image ready for us to use. Before running any commands on the terminal, make sure to read the documentation for this image. The latest version at the time of writing is 5.7. However, the latest tag name is 5.6. The latest version of an image can be for any previous version, but one in its stable state.

How to Manually Build Docker Containers for WordPress

The basic command to setup a container using this image is:

docker run --name wordpressdb -d mysql:5.7
How to Manually Build Docker Containers for WordPress

If you don’t already have a copy of the image locally, Docker will pull that for you from the Docker Hub. We know so far that --name gives our container a name, -d makes sure that our container runs in the background.

If you run docker ps you will see that wordpressdb container is not running. It should be running though. Run docker logs wordpressdb and you will see a message like this:

docker run --name wordpressdb -d mysql:5.7

Why is that? It’s because we didn’t pass a root password as an argument when we first built the container. So let’s do just that. First, we need to delete the container that we created with the name wordpressdb using docker rm wordpressdb. This is because the new container will use the same name and there can’t be two containers with the same name.

So let’s create our container again. We need to pass an environmental variable when we first create the container. It should look something like this:

error: database is uninitialized and MYSQL_ROOT_PASSWORD not set
  Did you forget to add -e MYSQL_ROOT_PASSWORD=... ?

-e MYSQL_ROOT_PASSWORD=password is an environmental variable. When the container is being built from the image, it reads this variable and sets the password for the root user to the specified value, which in this case is password.

If you now check docker logs wordpressdb, you’ll see a very long message, but don’t worry about this, it’s working. Again, run docker ps and you’ll see a container with the name wordpressdb that is active and running.

You can also pass other environmental variables to your container, you can find a complete list on the MySQL image documentation. Here’s another example:

docker run --name wordpressdb -e MYSQL_ROOT_PASSWORD=password -d mysql:5.7

If you tried to remove the previous container with the name wordpressdb, it probably failed. That’s because the container was still running in the background. You could first stop the running container and then remove it or just force remove it:

docker run --name wordpressdb -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=wordpress -d mysql:5.7

If we use MYSQL_DATABASE, it makes sure that a database with that name is created. This way, we know for sure what the name of the database and roots password is. You can also create another user with a password and database. Here’s a quick test for you, look at their docs and try to do this yourself.

If you’d like to know more how this container is built, look at the Dockerfile. It uses debian wheezy and builds the container using bash commands. It pulls it from the repository and then starts mysqld. When building your container from this image, the first time it will execute the commands of the build file. When using the container, it will only exec mysqld.

How to Manually Build Docker Containers for WordPress

Now that we have a running MySQL container, we can run a container that runs WordPress.

Building a WordPress Container

For this container we’ll use the PHP image. There are three types of PHP images, we only need the PHP image that comes with Apache.

docker rm -f wordpressdb

Without -d option, it wont run in background, instead it will show you everything the container is outputting (the same way that docker logs [container_name] does).

From the output you can see that it has automatically assigned an IP to that container. In my case it’s 172.17.0.35. If you visit this address using your browser, you’ll get a forbidden error. Why is that? It’s because there is nothing in the /var/www/html folder (on the containers filesystem), it’s empty.

So how we can put files in that folder? By default, that folder stays inside the container, and it’s invisible. However, not for long (don’t forget to docker rm wordpress). First, create a folder and navigate inside it (don’t forget to remove the old wordpress container).

docker run --name wordpressdb -d mysql:5.7

-v is used for mapping two folders. The first part is the folder on your OS and the second is the folder in the containers filesystem. On Unix-like systems, the "$PWD" returns the location where the terminal is when the command runs. When you first start a terminal, you’ll be in your home directory. The equivalent on Windows is cd.More about PWD can be found here.

So in our example, the first part is “$PWD/”, which is the local directory and the second part is /var/www/html/. -v requires both to be full paths. However, if we look in our working directory, we can see that no files exist there. Create a file called index.php that contains the following:

error: database is uninitialized and MYSQL_ROOT_PASSWORD not set
  Did you forget to add -e MYSQL_ROOT_PASSWORD=... ?

Check this again in your browser. This time you’ll notice that the IP address has changed because we created a new container. Every time we create a new container, it changes its IP. If you see that message in your browser then you have done everything right.

Let’s see what it happens if we put the WordPress files there. Stop the container by using docker stop wordpress. Grab the latest copy of WordPress from wordpress.org and drop the files inside the project folder. Start the container again using docker start wordpress. Also, make note that you’ll initially need to make the files readable. You can run chmod -R 777 projectfolder on *nix systems. If you reload the page, your browser will tell you that:

docker run --name wordpressdb -e MYSQL_ROOT_PASSWORD=password -d mysql:5.7

By default, the PHP image doesn’t have the MySQL extension installed, but we can fix that. This time we’ll build a container via a Dockerfile. We’ve already seen how Dockerfiles work. They are built from a base image, do some processing, then execute one command in the end.

Create a new file named Dockerfile:

We want to use the php:5.6-apache image.

docker run --name wordpressdb -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=wordpress -d mysql:5.7

Then we’ll install the mysqli extension.

docker rm -f wordpressdb

Next, we need to execute apache2-foreground as the PHP image does (we only needed to install the MySQL extension after all).

docker run --name wordpress php:5.6-apache

Using build files we can build images. Using this image, we build the container.

docker run --name wordpress -v "$PWD/":/var/www/html php:5.6-apache

The -t is used to give a repository name. The . tells to Docker where the Dockerfile is located. As the Dockerfile is located in the working directory, . tells docker that it is in the working directory.

If you check the images with docker images, you’ll now see a new image with tag latest (because we didn’t specify a tag for this image). Now build container this container with this image like we did with php5.6-apache image.

docker run --name wordpressdb -d mysql:5.7

Check your browser for the containers IP and you will see something like this:

How to Manually Build Docker Containers for WordPress

If you got this far, then you have done everything right. Now we have to link WordPress with a database. This is far from the famous 5 minutes install of WordPress and more complex, but you will get to see the benefits of Docker in the long run.

So how do we link WordPress with the database? First we need to link the wordpress container with a database container (wordpressdb). This can be done via linking two containers. More on linking can be found here.

error: database is uninitialized and MYSQL_ROOT_PASSWORD not set
  Did you forget to add -e MYSQL_ROOT_PASSWORD=... ?

The new arguments is --link. The first part wordpressdb is the name of the container that we want to link, and the second part mysql is the alias. Docker modifies the host of the wordpress container and sets the IP of the wordpressdb to mysql. So when we fill the information for the database on WordPress configuration, we will set the host to ‘mysql’.

Now go to your browser using IP of the container (the new IP). Fill the information for the database and login to the administrator panel. If you try to install a new theme (which will try to make changes on filesystem), you will see something like this:

How to Manually Build Docker Containers for WordPress

Why is that? It is because the user that runs Apache doesn’t have write access on the filesystem. This is where things get a little difficult. We need to build a new version of the phpwithmysql image. Go to your Dockerfile and modify it to look like this:

docker run --name wordpressdb -e MYSQL_ROOT_PASSWORD=password -d mysql:5.7

We haven’t created entrypoint.sh file yet, but we will do this shortly. COPY copies entrypoint.sh to / inside the container. chmod 777 /entrypoint.sh makes that file executable. And finally ENTRYPOINT executes that file. Now create the entrypoint.sh file in the same directory as the Dockerfile.

docker run --name wordpressdb -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=wordpress -d mysql:5.7

This is the simplified workaround of the official WordPress image, but will make sure we have write access to the containers filesystem. We can now build the new image:

docker rm -f wordpressdb

Make sure to remove the old containers and create the new containers:

docker run --name wordpress php:5.6-apache
docker run --name wordpress -v "$PWD/":/var/www/html php:5.6-apache
<span><span><?php 
</span></span><span>
</span><span><span>phpinfo();
</span></span><span>
</span><span><span>?></span></span>

Also, remove the old wp-config.php file.

Now check the IP for your wordpress container in your browser. This time you can install themes and plugins, and make changes on the containers filesystem.

Some of the steps above might seem quite cryptic and complex. That’s why there are official images for many different frameworks and languages. Every framework or language has different specifications on how they work. By default, Docker doesn’t allow the application to write on the filesystem. Is this a bad or good thing? I think it’s a good thing. We could create a third container that only holds files. There the application could write files. This way we would have a more modular architecture. But for those frameworks that can’t be changed (like WordPress), there are workarounds.

Final Tweaks

The last thing we have to do is to work around a problem that occurs when you stop the wordpress container, and start it again. The problem is that WordPress saves the last IP as its ‘Home’ and ‘Site’ URL. Stop wordpress container and start it again. This time it will have a new IP. If you try that in your browser, you will see that images, css and javascript files are not included properly. The solution is simple, just modify the wp-config.php by adding this lines:

docker run --name wordpressdb -d mysql:5.7

Note that if you define these values in your wp-config.php file, you can’t change them later on in General Settings.

Conclusion

In this article, we covered how we can build containers for WordPress. We did it in a rather cryptic way, with long commands that can be hard to remember. There should be an easier way, and there is! The Docker team has built a WordPress image that you can easily setup in minutes. After all, who wants to remember every command to setup WordPress?

In the next article in this series, I” show you how use the official WordPress image, and we’ll also learn how to use Docker Compose to make things even easier.

So why did I write this article if there’s an easier way? Essentially, it was to get a better understanding of how Docker works, to do this you have to get your hands dirty with the underlying complexities. It’s more of a personal rule, so when I get to use Docker tomorrow, I’ll know more about how it works and how to tweak it for my needs. I hope you also now have a deeper understanding of how Docker works behind the scenes. Stay tuned for the third article on this series where we’ll have even more fun with Docker and WordPress.

What do you think about Docker so far? Would you consider it on your next project? Let me know in the comments below.

Frequently Asked Questions (FAQs) about Building Docker Containers for WordPress

How can I ensure my Docker container for WordPress is secure?

Security is a crucial aspect when setting up Docker containers for WordPress. To ensure your container is secure, always use the latest version of Docker and WordPress. Regularly update your Docker images and containers to include the latest security patches. Also, use Docker secrets to manage sensitive data like passwords. Avoid running Docker containers as root to minimize potential damage if a container is compromised. Lastly, use Docker security scanning tools to identify and fix vulnerabilities in your images.

How can I optimize the performance of my Docker container for WordPress?

To optimize the performance of your Docker container for WordPress, consider using a lightweight base image. This reduces the size of the image and speeds up the build process. Also, use Docker’s multi-stage builds to separate the build-time and runtime dependencies, which can significantly reduce the size of your final image. Additionally, limit the resources (CPU, memory) that your container can use to prevent it from consuming all available resources on the host machine.

How can I troubleshoot issues with my Docker container for WordPress?

Docker provides several tools for troubleshooting. Use the ‘docker logs’ command to view the logs of a running container. If your container is crashing, use the ‘docker inspect’ command to get more information about the container. You can also use the ‘docker stats’ command to monitor the resource usage of your containers. If you’re facing network issues, use the ‘docker network inspect’ command to inspect your Docker network.

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

To backup your WordPress site running in a Docker container, you can use the ‘docker cp’ command to copy files from the container to the host machine. You can also use Docker volumes to persist data. If you’re using a MySQL database, you can use the ‘mysqldump’ command to create a backup of your database.

How can I scale my WordPress site running in Docker containers?

Docker provides several tools for scaling applications. You can use Docker Compose to define and run multi-container applications, and scale them by increasing the number of container instances. You can also use Docker Swarm or Kubernetes, which are orchestration tools that can manage and scale your containers across multiple hosts.

How can I automate the deployment of my WordPress site in Docker containers?

You can automate the deployment of your WordPress site in Docker containers using CI/CD tools like Jenkins, Travis CI, or GitHub Actions. These tools can build your Docker images, run tests, and deploy your containers to a Docker host or a Kubernetes cluster.

How can I manage multiple WordPress sites in Docker containers?

To manage multiple WordPress sites in Docker containers, you can use Docker Compose to define each site as a separate service. You can also use Docker networks to isolate the network traffic of each site. If you’re using a reverse proxy like Nginx, you can configure it to route traffic to the appropriate container based on the domain name.

How can I update my WordPress site running in a Docker container?

To update your WordPress site running in a Docker container, you can pull the latest WordPress image from the Docker Hub, stop your running container, and start a new one using the updated image. Remember to backup your data before updating.

How can I monitor my WordPress site running in Docker containers?

Docker provides several tools for monitoring containers. You can use the ‘docker stats’ command to monitor the resource usage of your containers. You can also use tools like Prometheus and Grafana to collect and visualize metrics from your containers.

How can I migrate my existing WordPress site to a Docker container?

To migrate your existing WordPress site to a Docker container, you need to backup your WordPress files and database, create a Dockerfile and a Docker Compose file to define your WordPress and database services, build your Docker images, and start your containers. Remember to update your WordPress configuration to point to the new database service.

The above is the detailed content of How to Manually Build Docker Containers for WordPress. 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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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)

Hot Topics

PHP Tutorial
1488
72
How to diagnose high CPU usage caused by WordPress How to diagnose high CPU usage caused by WordPress Jul 06, 2025 am 12:08 AM

The main reasons why WordPress causes the surge in server CPU usage include plug-in problems, inefficient database query, poor quality of theme code, or surge in traffic. 1. First, confirm whether it is a high load caused by WordPress through top, htop or control panel tools; 2. Enter troubleshooting mode to gradually enable plug-ins to troubleshoot performance bottlenecks, use QueryMonitor to analyze the plug-in execution and delete or replace inefficient plug-ins; 3. Install cache plug-ins, clean up redundant data, analyze slow query logs to optimize the database; 4. Check whether the topic has problems such as overloading content, complex queries, or lack of caching mechanisms. It is recommended to use standard topic tests to compare and optimize the code logic. Follow the above steps to check and solve the location and solve the problem one by one.

How to minify JavaScript files in WordPress How to minify JavaScript files in WordPress Jul 07, 2025 am 01:11 AM

Miniving JavaScript files can improve WordPress website loading speed by removing blanks, comments, and useless code. 1. Use cache plug-ins that support merge compression, such as W3TotalCache, enable and select compression mode in the "Minify" option; 2. Use a dedicated compression plug-in such as FastVelocityMinify to provide more granular control; 3. Manually compress JS files and upload them through FTP, suitable for users familiar with development tools. Note that some themes or plug-in scripts may conflict with the compression function, and you need to thoroughly test the website functions after activation.

How to optimize WordPress without plugins How to optimize WordPress without plugins Jul 05, 2025 am 12:01 AM

Methods to optimize WordPress sites that do not rely on plug-ins include: 1. Use lightweight themes, such as Astra or GeneratePress, to avoid pile-up themes; 2. Manually compress and merge CSS and JS files to reduce HTTP requests; 3. Optimize images before uploading, use WebP format and control file size; 4. Configure.htaccess to enable browser cache, and connect to CDN to improve static resource loading speed; 5. Limit article revisions and regularly clean database redundant data.

How to prevent comment spam programmatically How to prevent comment spam programmatically Jul 08, 2025 am 12:04 AM

The most effective way to prevent comment spam is to automatically identify and intercept it through programmatic means. 1. Use verification code mechanisms (such as Googler CAPTCHA or hCaptcha) to effectively distinguish between humans and robots, especially suitable for public websites; 2. Set hidden fields (Honeypot technology), and use robots to automatically fill in features to identify spam comments without affecting user experience; 3. Check the blacklist of comment content keywords, filter spam information through sensitive word matching, and pay attention to avoid misjudgment; 4. Judge the frequency and source IP of comments, limit the number of submissions per unit time and establish a blacklist; 5. Use third-party anti-spam services (such as Akismet, Cloudflare) to improve identification accuracy. Can be based on the website

How to use the Transients API for caching How to use the Transients API for caching Jul 05, 2025 am 12:05 AM

TransientsAPI is a built-in tool in WordPress for temporarily storing automatic expiration data. Its core functions are set_transient, get_transient and delete_transient. Compared with OptionsAPI, transients supports setting time of survival (TTL), which is suitable for scenarios such as cache API request results and complex computing data. When using it, you need to pay attention to the uniqueness of key naming and namespace, cache "lazy deletion" mechanism, and the issue that may not last in the object cache environment. Typical application scenarios include reducing external request frequency, controlling code execution rhythm, and improving page loading performance.

How to enqueue assets for a Gutenberg block How to enqueue assets for a Gutenberg block Jul 09, 2025 am 12:14 AM

When developing Gutenberg blocks, the correct method of enqueue assets includes: 1. Use register_block_type to specify the paths of editor_script, editor_style and style; 2. Register resources through wp_register_script and wp_register_style in functions.php or plug-in, and set the correct dependencies and versions; 3. Configure the build tool to output the appropriate module format and ensure that the path is consistent; 4. Control the loading logic of the front-end style through add_theme_support or enqueue_block_assets to ensure that the loading logic of the front-end style is ensured.

How to add custom fields to users How to add custom fields to users Jul 06, 2025 am 12:18 AM

To add custom user fields, you need to select the extension method according to the platform and pay attention to data verification and permission control. Common practices include: 1. Use additional tables or key-value pairs of the database to store information; 2. Add input boxes to the front end and integrate with the back end; 3. Constrain format checks and access permissions for sensitive data; 4. Update interfaces and templates to support new field display and editing, while taking into account mobile adaptation and user experience.

How to add custom rewrite rules How to add custom rewrite rules Jul 08, 2025 am 12:11 AM

The key to adding custom rewrite rules in WordPress is to use the add_rewrite_rule function and make sure the rules take effect correctly. 1. Use add_rewrite_rule to register the rule, the format is add_rewrite_rule($regex,$redirect,$after), where $regex is a regular expression matching URL, $redirect specifies the actual query, and $after controls the rule location; 2. Custom query variables need to be added through add_filter; 3. After modification, the fixed link settings must be refreshed; 4. It is recommended to place the rule in 'top' to avoid conflicts; 5. You can use the plug-in to view the current rule for convenience

See all articles