简体   繁体   English

如何对PHP应用程序进行Docker化

[英]How to Dockerize a PHP app

I am trying to understand the general process that goes into deploying a PHP web app through Docker. 我试图了解通过Docker部署PHP Web应用程序的一般过程。 I have a web app developed in LAMP. 我有一个在LAMP中开发的Web应用程序。

So far I understood that firs of all I have to download and install Docker itself. 到目前为止,我了解到我必须首先下载并安装Docker本身。 Afterwards I have to install the Docker Composer. 之后,我必须安装Docker Composer。 Then using the Composer I have to create a container that will contain the image of my server (Apache). 然后,必须使用Composer创建一个包含我的服务器(Apache)映像的容器。

And this is where I get confused. 这就是我感到困惑的地方。 Do I have to then create a container for my database and another one for the application itself (the directory containing the code) or do I have one container for the server the database and the app? 然后我必须为数据库创建一个容器,为应用程序本身创建一个容器(包含代码的目录),还是为服务器和数据库和应用程序创建一个容器?

I dont need a detailed explanation, just the general idea behind the process then I can figure out the rest on my own 我不需要详细的解释,仅需要流程背后的一般思想,那么我就可以自行解决其余的问题

Thank you to anyone who can provide any help. 谢谢任何能提供帮助的人。

There can be many ways but the simple way is to install docker on some linux machine , write a docker file that installs and configures all the necessay components such as apache , php , mysql etc , and then grab your application code either inside the container or attach it as external volume from the host. 有很多方法,但是简单的方法是在某些linux机器上安装docker,编写一个docker文件来安装和配置所有必需的组件,例如apache,php,mysql等,然后在容器或容器中获取应用程序代码。从主机将其作为外部卷连接。

After writing dockerfile , you can build the docker image by using the docker build command , and after the image is build you can use it locally or push to dockerhub , or push it to your private docker registery if you want. 写入dockerfile之后 ,您可以使用docker build命令构建docker映像,并且在构建映像之后,可以在本地使用它,也可以将其推送到dockerhub,或者根据需要将其推送到私有docker register。

The other option is if you just want to test , you can pull an already existing image from dockerhub that contains the LAMP stack and you then just need to do docker run on the image attaching your php application as external volume . 另一个选择是,如果您只想测试,则可以从包含LAMP堆栈的 dockerhub中提取一个已经存在的映像 ,然后只需要在Docker上运行该映像,并将您的php应用程序附加到外部卷上即可

Ofcourse , to access the application on port 80 or 443 outside docker you have to expose those ports either in docker file , or at docker run command time. 当然,要在Docker外部的端口80或443上访问应用程序,您必须在docker file中或在docker run命令时公开这些端口。

For a test environment , you can run all the services in just one container. 对于测试环境,您可以在一个容器中运行所有服务。

For larger Deployements you can consider a container orchestration service such as DockerSwarm or Kubernetes . 对于较大的部署,可以考虑使用容器编排服务,例如DockerSwarmKubernetes You can also try DC/OS from MesoSphere , by grabbing a vagrant file from thier github repo that will setup the DC/OS on a single machine for you. 您也可以从MesoSphere尝试DC / OS ,方法是从github存储库中获取一个无用的文件,该文件将为您在一台计算机上设置DC / OS。 Then you can just spin up as many services as you want on Mesos . 然后,您可以根据需要在Mesos上启动任意数量的服务。 They provide out of the box support for service installation , container management and scaling. 它们为服务安装,容器管理和扩展提供了开箱即用的支持。

The best practices recommends to have one Docker container per process/service (one container for Apache + PHP, another container for MySQL and so on) but it's just a guideline and it doesn't mean that you cannot have only one container with everything you need inside it. 最佳做法建议每个进程/服务使用一个Docker容器(一个用于Apache + PHP的容器,另一个用于MySQL的容器,依此类推),但这只是一个准则,并不意味着您不能只拥有一个包含所有内容的容器需要里面。

If you decide to go with only one container to run all the services, you'll be fine just using Docker (Engine). 如果您决定只使用一个容器来运行所有服务,那么仅使用Docker (引擎)就可以了。 You can still use Docker Compose in this case but there's no real need for it. 在这种情况下,您仍然可以使用Docker Compose ,但实际上并不需要。

Docker Compose is more helpful when you have to run multiple containers. 当您必须运行多个容器时,Docker Compose更有用。 With just one command you can get all your containers up and running. 仅需一个命令,您就可以启动并运行所有容器。 Also here you can use only Docker Engine but you'll need to run each container manually. 同样在这里,您只能使用Docker Engine,但是您需要手动运行每个容器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM