WordPress开发人员Docker简介

tech2022-12-23  119

Up until now, we’ve seen a number of different options for development environments. We’ve seen what Vagrant VVV and Chassis can do; both are great solutions, and both build on top of Vagrant. If you’ve started developing using Vagrant solutions, then good for you. But wait, there is one more option. In this article we will see what Docker is, and how to start using it in your WordPress projects.

到目前为止,我们已经看到了开发环境的许多不同选择。 我们已经了解了Vagrant VVV和机箱可以做什么; 两者都是很好的解决方案,并且都建立在Vagrant之上。 如果您已开始使用Vagrant解决方案进行开发,那么对您有好处。 但是,等等,还有另一种选择。 在本文中,我们将了解什么是Docker ,以及如何在WordPress项目中开始使用它。

为什么选择Docker? (Why Docker?)

Docker is an open source platform that helps you build, run and deploy applications. In this article, we will start using Docker with WordPress, but it’s really easy to use with any other CMS or framework, and every language out there. Docker has been a buzzword in the DevOps community for sometime. Consider this: in its first 1.0 stable release, many cloud providers started adopting Docker. If you have used Heroku or other PaaS for a while, you may know that it’s easy to develop on them. With most PaaS providers, you have a web container, a database container, a container that saves files, and so on. Each container is different and communicates with each other via IPs and Ports. PaaS platforms also uses container technologies.

Docker是一个开源平台,可帮助您构建,运行和部署应用程序。 在本文中,我们将开始在WordPress上使用Docker,但是将它与其他任何CMS或框架以及其中的每种语言一起使用都非常容易。 Docker在DevOps社区中一直是流行语 。 考虑一下:在其第一个1.0稳定版本中,许多云提供商开始采用Docker。 如果您使用Heroku或其他PaaS已有一段时间,那么您可能会知道在它们上进行开发很容易。 对于大多数PaaS提供程序,您都有一个Web容器,一个数据库容器,一个用于保存文件的容器,等等。 每个容器都是不同的,并通过IP和端口相互通信。 PaaS平台还使用容器技术。

Docker does the same. You can create a container that runs your code (this may be Apache and PHP that runs PHP scripts), and one container that holds your database (MySQL, MongoDB, and so on). But you can also do this using Vagrant, and I must admit that Vagrant is quite good at what it does. It solves the problems usually associated with working on my machine in the development stage, and it also gets close to the production environment. On the other hand though, Docker makes the local environment and production the same.

Docker也是如此。 您可以创建一个运行代码的容器(可以是运行PHP脚本的Apache和PHP ),另一个可以存储数据库的容器( MySQL , MongoDB等)。 但是您也可以使用Vagrant做到这一点,我必须承认Vagrant擅长于此。 它解决了在开发阶段通常与在我的机器上工作相关的问题,并且它也接近生产环境。 但另一方面,Docker使本地环境和生产环境相同。

My preferred Vagrant configuration is Homestead. Yes, the famous Laravel box. It’s very good for the majority of projects, but it also comes with two databases and many other software programs that you may not need. Docker performs better than classical hypervisor technologies. Docker is super fast for local development if you are using Linux. It can also be faster than traditional hypervisors, even if you’re not running Linux as your primary OS.

我首选的Vagrant配置是Homestead 。 是的,著名的Laravel盒子。 对于大多数项目来说,这非常好,但是它附带了两个数据库和许多其他您可能不需要的软件程序。 Docker的性能优于传统的虚拟机管理程序技术。 如果您使用的是Linux,则Docker对于本地开发而言非常快。 即使您没有将Linux作为主要操作系统,它也可以比传统的虚拟机管理程序更快。

If you’ve been using Vagrant for a while and want to learn something new and cool, then this article is for you.

如果您已经使用Vagrant一​​段时间了,并且想学习一些新奇而有趣的东西,那么本文适合您。

Docker简介 (Introduction to Docker)

Docker is built with the concept of containers. This is not a new idea. We’ve been using containers for many years. Android applications are also built on top of this concept. Docker consists of a Docker CLI and a daemon. You will only be working with the CLI. The CLI communicates with the daemon, and it’s the daemon that does the processing.

Docker是用容器的概念构建的。 这不是一个新的想法。 我们使用容器已有多年了。 Android应用程序也基于此概念构建。 Docker由Docker CLI和守护程序组成。 您将仅使用CLI。 CLI与守护程序进行通信,并且由守护程序进行处理。

Containers are built from images, with some additional configuration. You can also create images from other images. Let’s say you want to use the Ubuntu 14.04 image. You also want to use Apache and PHP. You could use the Ubuntu image and then install Apache and PHP. This way you can create another image and share this image with others. Images are like blueprints for containers. Containers are built from those images. Compared with Vagrant, where you have a base OS which is Ubuntu, then using a tool like Puppet, you then instruct it to install Apache and PHP. You then ‘vagrant up’, and you’re ready to go with your local development.

容器是根据映像构建的,具有一些其他配置。 您也可以从其他图像创建图像。 假设您要使用Ubuntu 14.04映像。 您还想使用Apache和PHP。 您可以使用Ubuntu映像,然后安装Apache和PHP。 这样,您可以创建另一个图像并与他人共享该图像。 图像就像容器的蓝图。 容器是根据这些图像构建的。 与Vagrant相比,后者拥有一个基本的操作系统Ubuntu,然后使用Puppet之类的工具,然后指示它安装Apache和PHP。 然后,您就“无所事事了”,并准备进行本地开发。

Before starting this article, I highly recommend you to give the interactive tutorial a try.

在开始本文之前,我强烈建议您尝试一下交互式教程 。

Docker入门 (Getting Practical with Docker)

First of all, make sure you have Docker installed on your system. The ideal solution is to have Linux installed as your primary OS, particularly given that Docker uses the Linux kernel. But you can run Docker on Windows and Mac. There is a tool called Boot2Docker that is available for both Mac and Windows. For installation instructions, read their installation guides.

首先,确保在系统上安装了Docker。 理想的解决方案是将Linux安装为主操作系统,特别是考虑到Docker使用Linux内核。 但是您可以在Windows和Mac上运行Docker。 有一个名为Boot2Docker的工具,可用于Mac和Windows。 有关安装说明,请阅读其安装指南 。

To make sure that Docker is installed properly on your system, run docker -v and docker will tell you what version is installed on your system.

为了确保在您的系统上正确安装了Docker,请运行docker -v然后docker会告诉您系统上安装了哪个版本。

There are two main things you need to know about Docker: Images and Containers. Images are like a blueprint for the containers. Images tell Docker what the container should be. You can’t run images; instead you build containers from them. On the Docker Hub you can see a list of images. There are images that come from the Docker Team, and images that come from the community.

关于Docker您需要了解两件事:图像和容器。 图像就像容器的蓝图。 图像告诉Docker容器应该是什么。 您无法运行图像; 相反,您可以从中构建容器。 在Docker Hub上,您可以看到图像列表。 有些镜像来自Docker团队,另一半来自社区。

To see what images are currently available on your system, execute:

要查看系统上当前可用的图像,请执行:

docker images

This will give you a list of images downloaded on your system. If you haven’t used Docker in the past, you will receive the result of no images on your system.

这将为您提供下载到系统上的图像的列表。 如果您过去从未使用过Docker,则会在系统上收到没有映像的结果。

Let’s download an image from Docker Hub. The most downloaded is the Ubuntu image. To download an image you should execute docker pull [imagename]:[tag] or docker pull [imagename].

让我们从Docker Hub下载映像。 下载最多的是Ubuntu映像。 要下载映像,您应该执行docker pull [imagename]:[tag]或docker pull [imagename] 。

docker pull ubuntu:12.04

or

要么

docker pull ubuntu

Now check again for your images, using docker images. There are now two images. Tag names can be used as versioning, but not on their own. For example, in this PHP image, there are the same PHP versions with Apache, nginx or without any of them. You can find tag names on every image page (on Docker Hub). If no tag name is specified, then it pulls the image with tag name latest.

现在,使用docker images再次检查您的docker images 。 现在有两个图像。 标记名称可以用作版本控制,但不能单独使用。 例如,在此PHP映像中,具有与Apache,nginx相同或不包含其中任何一个的相同PHP版本。 您可以在每个图像页面(在Docker Hub上)找到标签名称。 如果没有指定标记名称,然后将其拉与标签名称的图像latest 。

You can build containers using docker run [imagename]:[tag] or docker run [imagename]. If you do not specify a tag name, it will run the image with the tag latest. Then you can start setting up your containers using docker start [name_of_the_container]. If you don’t specify a name for the container, Docker will give the container random name. Let’s build a container:

您可以使用docker run [imagename]:[tag]或docker run [imagename]来构建容器。 如果您未指定标签名称,它将使用标签latest运行图像。 然后,您可以使用docker start [name_of_the_container]开始设置容器。 如果您没有为容器指定名称,则Docker将为容器提供随机名称。 让我们建立一个容器:

docker run ubuntu

To run containers, you have to execute:

要运行容器,您必须执行:

docker ps

But why is there no running container? Because the container has finished its process. To see the list of created containers so far, you’ll need to run docker ps -a. This will give you the list of all existing containers and some additional information like the container ID, the command that will be executed when the container starts, the image name, its status, port and name. When first creating the container, we didn’t gave it a name. Therefore, the container ID is its unique identifier. It can replace the container’s name in some cases. The port is a port that is public outside the container (the ports can be mapped between the Docker container and your system). You can delete a created container with:

但是为什么没有正在运行的容器? 因为容器已完成其过程。 要查看到目前为止已创建的容器列表,您需要运行docker ps -a 。 这将为您提供所有现有容器的列表以及一些其他信息,例如容器ID,容器启动时将执行的命令,映像名称,其状态,端口和名称。 首次创建容器时,我们没有给它起一个名字。 因此,容器ID是其唯一标识符。 在某些情况下,它可以替换容器的名称。 该端口是容器外部的公共端口(可以在Docker容器和您的系统之间映射端口)。 您可以使用以下方法删除创建的容器:

docker rm [container name] docker ps -a

To better understand how a container works, let’s analyze its Dockerfile. In the Ubuntu page on Docker Hub, you can see different tag names that link to a Dockerfile. Let’s see what Ubuntu 12.04 looks like.

为了更好地了解容器的工作原理,让我们分析其Dockerfile。 在Docker Hub的Ubuntu页面上,您可以看到链接到Dockerfile的不同标签名称。 让我们看看Ubuntu 12.04的外观。

FROM is a way to tell Docker to use a base image, which is scratched in this case. You also can have other images that use the Ubuntu image. In their Dockerfile, you would see FROM ubuntu:14.04 or other versions of the Ubuntu image. In this case, scratch barely does anything.

FROM是一种告诉Docker使用基本映像的方法,在这种情况下该映像是临时的。 您还可以拥有其他使用Ubuntu映像的映像。 在他们的Dockerfile中,您将看到FROM ubuntu:14.04或其他版本的Ubuntu映像。 在这种情况下,刮擦几乎什么也不做。

Then it downloads the content of Ubuntu, runs some commands like we would do on the terminal (on nix systems). If you looked carefully, there was a command filed when we executed docker ps -a. In the end of the Dockerfile, the command that will be executed when the container is created, and every time it starts is, /bin/bash. This doesn’t do anything.

然后下载Ubuntu的内容,像在终端上(在nix系统上)运行一些命令。 如果仔细看,当我们执行docker ps -a时会提交一个命令。 在Dockerfile的末尾,创建容器时及其每次启动时将执行的命令是/bin/bash 。 这什么也没做。

Now, let’s create another container, but this time with a name and different command, to run every time the container starts.

现在,让我们创建另一个容器,但是这次使用一个名称和不同的命令在每次容器启动时运行。

docker run --name ubuntucontainer ubuntu echo something

Do you see that this echoed something on the terminal? If you enter it again, docker ps -a you will see a new container with the given name, and with a different command this time. By adding something after the image name, Docker accepts it as a command.

您是否看到这在终端上回音? 如果再次输入, docker ps -a您将看到一个具有给定名称的新容器,这次使用不同的命令。 通过在映像名称之后添加一些内容,Docker将其作为命令接受。

But this is really boring. When we run a container, we want it to stay up for some time. But this really depends on the command it executes when it starts (or is first created). Create another container from the base Ubuntu image.

但这真的很无聊。 当我们运行一个容器时,我们希望它保持一段时间。 但这实际上取决于它在启动(或首次创建)时执行的命令。 从基本的Ubuntu映像创建另一个容器。

docker run ubuntu ping google.com

This will say that it cannot find ping. This is because ping is not installed by default. But we can do something else: build a container that runs the apt-get install -y ping command, and then create an image from that.

这将表明找不到ping。 这是因为默认情况下未安装ping。 但是我们可以做其他事情:建立一个运行apt-get install -y ping命令的容器,然后从中创建一个映像。

docker run ubuntu apt-get install -y ping

Then you have to find the container that was built. After that execute:

然后,您必须找到已构建的容器。 之后执行:

docker commit [containername]

You will find the new image like this:

您将找到像这样的新图像:

docker images

Your new image doesn’t have a name but it has an image id. We can use this id to create a container from it. Using the four first characters is enough.

您的新图片没有名称,但有图片ID。 我们可以使用此ID从中创建一个容器。 使用前四个字符就足够了。

docker run [id] ping google.com

The problem here is that it doesn’t stop. We can make it to run in background mode.

这里的问题是它不会停止。 我们可以使其在后台模式下运行。

docker run -d [id] ping google.com

Now execute docker ps to see the running containers. To see the output of the ping utility, use:

现在执行docker ps以查看正在运行的容器。 要查看ping实用程序的输出,请使用:

docker logs [container_name]

This will run forever (or until you shutdown your computer). To stop a running container, you can use:

这将永远运行(或直到您关闭计算机)。 要停止正在运行的容器,可以使用:

docker stop [name_of_the_container]

If you enter docker ps, you will see that there are no running containers anymore.

如果输入docker ps ,将会看到不再有正在运行的容器。

结论 (Conclusion)

In this article we discovered what Docker is. But we barely scratched the surface of all its functionality. Don’t despair though: there are three more articles coming. In the next article, we will see how to manually setup Docker for WordPress. After that, we will see how we can setup Docker more easily via the official WordPress image, and also using Docker Compose. So, make sure you stay tuned for more.

在本文中,我们发现了Docker是什么。 但是我们几乎不了解其所有功能的表面。 不过不要失望:还有三篇文章来了。 在下一篇文章中,我们将看到如何为WordPress手动设置Docker。 之后,我们将看到如何通过官方WordPress映像以及使用Docker Compose更轻松地设置Docker。 因此,请确保您继续关注更多。

We want to hear from you. What do you think about Docker. Would you consider it on your next project? If you have a question please leave a comment below.

我们希望收到你的来信。 您如何看待Docker。 您会在下一个项目中考虑吗? 如果您有任何疑问,请在下面发表评论。

翻译自: https://www.sitepoint.com/docker-for-wordpress-developers/

相关资源:jdk-8u281-windows-x64.exe
最新回复(0)