如何手动为WordPress构建Docker容器

tech2022-12-20  109

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.

在我的上一篇文章中, 我们介绍了Docker是什么以及如何使用一些命令启动和运行。 但是,我们还没有做任何有用的事情。 使用Docker获得WordPress环境的方法有很多,在本文中,我将向您展示如何手动设置Docker容器以使用WordPress。 如果您想快速介绍Docker,可以跳到此处的第一篇文章 。

设置MySQL (Setting up MySQL)

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

每个WordPress安装都需要一个MySQL数据库。 为此,我们转到Docker Hub并找到一个MySQL映像。

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.

Docker团队已经有一个MySQL映像可供我们使用。 在终端上运行任何命令之前,请确保已阅读该映像的文档 。 在撰写本文时,最新版本是5.7。 但是,最新的标记名称是5.6。 映像的最新版本可以适用于任何以前的版本,但是处于稳定状态。

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

使用此映像设置容器的基本命令是:

docker run --name wordpressdb -d mysql:5.7

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.

如果您在本地还没有该映像的副本,则Docker将从Docker Hub为您拉取该映像的副本。 到目前为止,我们知道--name为我们的容器命名, -d确保我们的容器在后台运行。

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 ps您将看到wordpressdb容器未运行。 它应该正在运行。 运行docker logs wordpressdb ,您将看到如下消息:

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

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.

这是为什么? 这是因为在我们第一次构建容器时,我们没有传递root密码作为参数。 因此,让我们做到这一点。 首先,我们需要使用docker rm wordpressdb删除名称为wordpressdb的容器。 这是因为新容器将使用相同的名称,并且不能有两个容器使用相同的名称。

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:

因此,让我们再次创建容器。 首次创建容器时,需要传递环境变量。 它看起来应该像这样:

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

-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.

-e MYSQL_ROOT_PASSWORD=password是环境变量。 从映像构建容器时,它将读取此变量并将root用户的密码设置为指定的值,在本例中为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.

如果现在检查docker logs wordpressdb ,您会看到一条很长的消息,但是不必担心,它正在运行。 再次,运行docker ps ,您将看到一个名称为wordpressdb的容器处于活动状态并且正在运行。

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:

您还可以将其他环境变量传递给容器,您可以在MySQL映像文档中找到完整列表。 这是另一个例子:

docker run --name wordpressdb -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=wordpress -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:

如果您尝试删除名称为wordpressdb的先前容器,则可能会失败。 那是因为容器仍在后台运行。 您可以先停止正在运行的容器,然后将其删除,或者只是强行删除它:

docker rm -f wordpressdb

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.

如果我们使用MYSQL_DATABASE ,则确保创建具有该名称的数据库。 这样,我们可以肯定知道数据库的名称和root密码。 您还可以使用密码和数据库创建另一个用户。 这是对您的快速测试,请查看他们的文档,然后尝试自己做。

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.

如果您想进一步了解此容器的构建方式,请查看Dockerfile 。 它使用debian wheezy,并使用bash命令构建容器。 它将它从存储库中拉出,然后启动mysqld 。 从该映像构建容器时,它将首次执行构建文件的命令。 使用容器时,它将仅执行mysqld 。

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

现在我们有了一个正在运行MySQL容器,我们可以运行一个运行WordPress的容器。

构建一个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.

对于此容器,我们将使用PHP映像。 PHP映像有三种类型,我们只需要Apache随附PHP映像。

docker run --name wordpress php:5.6-apache

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).

如果没有-d选项,它将不会在后台运行,而是将显示容器输出的所有内容(与docker logs [container_name]方式相同)。

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.

从输出中,您可以看到它已自动为该容器分配了IP。 在我的情况下是172.17.0.35 。 如果您使用浏览器访问此地址,则会收到一个禁止的错误。 这是为什么? 这是因为/var/www/html文件夹中(容器文件系统上)没有任何内容,因此为空。

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 rm wordpress )。 首先,创建一个文件夹并在其中导航(不要忘记删除旧的wordpress容器)。

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

-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.

-v用于映射两个文件夹。 第一部分是操作系统上的文件夹,第二部分是容器文件系统中的文件夹。 在类Unix系统上, "$PWD"返回命令运行时终端所在的位置。 首次启动终端时,您将位于主目录中。 Windows上的等效项是cd有关PWD的更多信息,请参见此处 。

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:

因此,在我们的示例中,第一部分是“ $ PWD /”,这是本地目录,第二部分是/var/www/html/ 。 -v要求两者都是完整路径。 但是,如果我们查看工作目录,则可以看到那里没有文件。 创建一个名为index.php的文件,其中包含以下内容:

<?php phpinfo(); ?>

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.

在浏览器中再次检查。 这次您会注意到IP地址已更改,因为我们创建了一个新容器。 每次我们创建一个新容器时,它都会更改其IP。 如果您在浏览器中看到该消息,则说明您所做的一切正确。

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:

让我们看看如果将WordPress文件放在那里会发生什么。 使用docker stop wordpress停止容器。 从wordpress.org上获取WordPress的最新副本,并将文件拖放到项目文件夹中。 使用docker start wordpress重新启动容器。 另外,请注意,您最初需要使文件可读。 您可以在* nix系统上运行chmod -R 777 projectfolder 。 如果您重新加载页面,浏览器将告诉您:

Your PHP installation appears to be missing the MySQL extension which is required by WordPress.

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.

默认情况下,PHP映像未安装MySQL扩展,但是我们可以修复该问题。 这次,我们将通过Dockerfile构建一个容器。 我们已经了解了Dockerfiles的工作方式。 它们是从基本映像构建的,进行一些处理,然后最后执行一个命令。

Create a new file named Dockerfile:

创建一个名为Dockerfile的新文件:

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

我们要使用php:5.6-apache图像。

FROM php:5.6-apache

Then we’ll install the mysqli extension.

然后,我们将安装mysqli扩展。

RUN docker-php-ext-install mysqli

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

接下来,我们需要像PHP映像一样执行apache2-foreground (毕竟,我们只需要安装MySQL扩展)。

CMD ["apache2-foreground"]

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

使用构建文件,我们可以构建图像。 使用此图像,我们构建了容器。

docker build -t phpwithmysql .

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.

-t用于指定存储库名称。 的. 向Docker告知Dockerfile的位置。 由于Dockerfile位于工作目录中,因此. 告诉docker它在工作目录中。

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 images检查docker images ,您现在将看到一个带有最新标签的新图像(因为我们没有为此图像指定标签)。 现在,像我们对php5.6-apache图像所做的那样,用此图像构建容器。

docker run --name wordpress -v "$PWD/":/var/www/html phpwithmysql

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

检查您的浏览器中的容器IP,您将看到类似以下内容:

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.

如果到此为止,那么您就做对了所有事情。 现在,我们必须将WordPress与数据库链接。 这与著名的WordPress 5分钟安装相距甚远,而且更加复杂,但是从长远来看,您将看到Docker的好处。

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.

那么如何将WordPress与数据库链接? 首先,我们需要将wordpress容器与数据库容器( wordpressdb )链接。 这可以通过链接两个容器来完成。 有关链接的更多信息,请参见此处 。

docker run --name wordpress --link wordpressdb:mysql -v "$PWD/":/var/www/html phpwithmysql

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’.

新的参数是--link 。 第一部分wordpressdb是我们要链接的容器的名称,第二部分mysql是别名。 泊坞窗修改主机wordpress容器和设置的IP wordpressdb到mysql 。 因此,当我们在WordPress配置上填写数据库信息时,会将主机设置为“ 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:

现在,使用容器的IP(新IP)进入浏览器。 填写数据库信息,然后登录到管理员面板。 如果您尝试安装新主题(它将尝试在文件系统上进行更改),则会看到类似以下内容:

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:

这是为什么? 这是因为运行Apache的用户对文件系统没有写访问权。 这就是事情变得有些困难的地方。 我们需要构建新版本的phpwithmysql映像。 转到您的Dockerfile并将其修改为如下所示:

FROM php:5.6-apache RUN docker-php-ext-install mysqli COPY entrypoint.sh /entrypoint.sh RUN chmod 777 /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] CMD ["apache2-foreground"]

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.

我们还没有创建entrypoint.sh文件,但是我们很快就会做到这一点。 COPY entrypoint.sh复制到容器中的/中。 chmod 777 /entrypoint.sh使该文件可执行。 最后ENTRYPOINT执行该文件。 现在,在与Dockerfile相同的目录中创建entrypoint.sh文件。

#!/bin/bash chown -R www-data:www-data . exec "$@"

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:

这是官方WordPress映像的简化解决方法,但将确保我们具有对容器文件系统的写访问权。 现在,我们可以构建新图像:

docker build -t phpwithmysql:v2 .

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

确保删除旧容器并创建新容器:

docker rm -f wordpress docker rm -f wordpressdb docker run --name wordpressdb -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=wordpress -d mysql:5.7 docker run --name wordpress --link wordpressdb:mysql -v "$PWD/":/var/www/html phpwithmysql:v2

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

另外,删除旧的wp-config.php文件。

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.

现在,在浏览器中检查您的wordpress容器的IP。 这次您可以安装主题和插件,并在容器文件系统上进行更改。

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.

上面的某些步骤似乎很神秘且复杂。 这就是为什么有许多不同框架和语言的官方图像的原因。 每种框架或语言都有关于其工作方式的不同规范。 默认情况下,Docker不允许应用程序在文件系统上写入。 这是好事还是坏事? 我认为这是一件好事。 我们可以创建第三个仅包含文件的容器。 在那里,应用程序可以写入文件。 这样,我们将拥有一个更加模块化的体系结构。 但是对于那些无法更改的框架(如WordPress),有一些解决方法。

最终调整 (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:

我们要做的最后一件事是解决当您停止wordpress容器并重新启动它时发生的问题。 问题是WordPress将最后一个IP保存为“首页”和“站点” URL。 停止wordpress容器,然后重新启动。 这次它将有一个新的IP。 如果您在浏览器中尝试这样做,则会看到图像,css和javascript文件未正确包含在内。 解决方案很简单,只需添加以下wp-config.php行即可修改wp-config.php :

define('WP_HOME',$_SERVER['SERVER_ADDR']); define('WP_SITEURL',$_SERVER['SERVER_ADDR']);

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

请注意,如果您在wp-config.php文件中定义了这些值, wp-config.php后将无法在“常规设置”中更改它们。

结论 (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?

在本文中,我们介绍了如何为WordPress构建容器。 我们以一种相当隐秘的方式完成了它,并使用了难以记住的长命令。 应该有一种更简单的方法,而且有! Docker团队构建了一个WordPress映像,您可以在几分钟内轻松设置。 毕竟,谁想记住设置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.

在本系列的下一篇文章中,我将向您展示如何使用官方WordPress图像,并且我们还将学习如何使用Docker Compose使事情变得更加轻松。

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.

那么,如果有更简单的方法,为什么还要写这篇文章呢? 从本质上讲,这是为了更好地了解Docker的工作方式,为此,您必须动手处理底层的复杂性。 这更多是个人规则,因此,当我明天开始使用Docker时,我将进一步了解它的工作方式以及如何根据需要对其进行调整。 我希望您现在也对Docker的幕后工作有更深入的了解。 请继续关注本系列的第三篇文章,我们将在Docker和WordPress上获得更多的乐趣。

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

到目前为止,您如何看待Docker? 您会在下一个项目中考虑吗? 在下面的评论中让我知道。

翻译自: https://www.sitepoint.com/how-to-manually-build-docker-containers-for-wordpress/

最新回复(0)