使用具有PHP 7的Docker涉及创建一个docker映像,其中包含您的PHP应用程序运行所需的所有内容:PHP本身,Web服务器(例如Apache或nginx),必要的扩展程序,必要的扩展程序和您的应用程序代码。这是该过程的细分:
1。创建 Dockerfile
:此文件包含用于构建Docker映像的说明。使用Apache的基本示例可能看起来像这样:
<code class="“" dockerfile>从php:7.4-apache#安装必要的PHP扩展程序运行Docker-Php-ext-ext-install pdo_mysql#复制应用程序代码复制。 /var/www/html#公开端口apache在公开上听port 80 </code>
this dockerfile
以基本php 7.4图像开始,包括apache。然后,它将 pdo_mysql
扩展名(数据库交互必不可少的)安装,并将您的应用程序代码复制到正确的目录中。最后,它暴露了端口80,使您的应用程序可以从容器外部访问。
2。构建Docker映像:导航到包含您的 dockerfile
并运行:
<code class="“" bash> docker build -t my-php-app。运行Docker容器:构建后,运行容器: <pre class="brush:php;toolbar:false"> <code class="“" bash> docker run -p 8080:80 -d my-d my-php-app </code>
此命令以独立模式运行( -d
),映射port 8080 On Inserty Ons Machine On Inserter Macniter,以独立模式运行该容器。现在,您可以通过 http:// localhost:8080
访问您的应用程序。请记住,如有必要,请用首选端口替换 8080
。 You might need to adjust this based on your specific setup (eg, using Nginx instead of Apache).
Securing a PHP 7 application in Docker involves a multi-layered approach:
Dockerfile
should create and switch to a non-root user.是的,Docker组成是在单个应用程序中管理多个服务的理想选择。例如,您可能有用于PHP应用程序的单独容器,数据库(例如MySQL或PostgreSQL),消息队列(例如RabbitMQ)和Redis Cache。
a a <code> docker-compose.yml-compose.yml
文件文件将定义每个服务:
pre> pre> pre> pre> pre> <pre class="brush:php;toolbar:false"> <codale class="capetl" yaml depends_on: db db: image: mysql:8 environment: mysql_root_password: my-secret-password mysql_database: mydatabase mysql_user: myuser mysql_password: mypassword></codale>
This example shows a web
service (your PHP application) and a db
service (mysql)。 依赖性
指令确保数据库在Web应用程序之前启动。您将为每个服务提供单独的 dockerfile
。 Docker组成的简化了这些相互联系的服务的管理,确保它们开始,停止和缩放在一起。
使用Docker部署的应用程序?在容器内进行日志以识别错误或警告。使用 docker logs&lt; container_id&gt;
查看日志。
docker ps
检查容器是否正在运行,并且 code> code> docker Inspect&lt; contract&lt; continer_id&gt; contine&gt&gt; configuration.
Dockerfile
correctly installs necessary extensions, sets the correct working directory, and copies all required files.docker network inspect bridge
(or the name of your network) to check connectivity.Dockerfile
, rebuild the image to reflect those更改。以上是如何使用PHP 7使用Docker?的详细内容。更多信息请关注PHP中文网其他相关文章!