PHP Docker容器无故退出 [英] PHP Docker container exits for no reason
问题描述
我对docker和docker-compose比较陌生,所以我制作了这个文件
版本:"3"服务:网络:图片:nginx:latest端口:-"8050:80"数量:-./code:/code-./site.conf:/etc/nginx/conf.d/default.conf链接:-PHP的的PHP:图片:php:7.3-fpm-alpine3.9命令:apk --update添加php7-mysqli数量:-./code:/codeD b:图片:mysql命令:--default-authentication-plugin = mysql_native_password重启:总是端口:-3306:3306环境:MYSQL_ROOT_PASSWORD:示例管理员:图片:管理员重启:总是端口:-8080:8080
出于某种原因,该行
命令:apk --update添加php7-mysqli
无缘无故地停止php容器,只显示 dock_php_1退出,代码为0
因此,我的Web容器也停止了,服务也无法正常工作
问题的核心是什么以及如何解决?
这是因为您告诉容器在启动时运行apk更新命令,此操作完成并以有效退出代码0退出.>
要使其运行该apk更新命令并仍使用php容器,您需要使用自己的构建扩展php映像,以创建基本php映像的自己的自定义映像"(有点像扩展PHP类)),然后将该apk更新作为dockerfile的一部分运行.
这相当容易做到,您的dockerfile看起来像这样:
从FROM php:7.3-fpm-alpine3.9运行apk --update添加php7-mysqli
您可以将其另存为 ./php/Dockerfile
然后将您的 docker-compose.yml
文件更新为:
...的PHP:内部版本:./php数量:...
删除命令:部分
然后,这将在 docker-compose up
之后,在其中包含apk更新的扩展映像作为容器的额外层构建,并继续运行原始映像提供的标准php命令.
这里是 build:
指令上的文档,因为您可以使用它进行很多其他有趣的操作,例如,如果您不想将Dockerfile放入子目录,则指定它,并提供 context:
(如果您希望将文件烘焙到新映像中) https://docs.docker.com/compose/compose-file/#build
I'm relatively new to docker and docker-compose so I made this file
version: '3'
services:
web:
image: nginx:latest
ports:
- "8050:80"
volumes:
- ./code:/code
- ./site.conf:/etc/nginx/conf.d/default.conf
links:
- php
php:
image: php:7.3-fpm-alpine3.9
command: apk --update add php7-mysqli
volumes:
- ./code:/code
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: example
adminer:
image: adminer
restart: always
ports:
- 8080:8080
For some reason the line
command: apk --update add php7-mysqli
Stops php container for no reason, just prints dock_php_1 exited with code 0
Thus my web container also stops and service doesn't work
What could be core of the problem and how to fix it?
This is because you are telling the container to run the apk update command when it starts, this completes and exits with a valid exit code 0...
To get it to run that apk update command and still use the php container, you need to extend the php image with your own build to create your own 'custom image' of the base php image (kinda like extending a PHP class), and then run that apk update as part of the dockerfile.
This is reasonably easy to do and your dockerfile would look something like:
FROM php:7.3-fpm-alpine3.9
RUN apk --update add php7-mysqli
You can save this as ./php/Dockerfile
Then update your docker-compose.yml
file to say:
...
php:
build: ./php
volumes:
...
Removing the command: section
This would then, upon docker-compose up
, build your extended image with the apk update inside it as an extra layer on the container, and continue running the standard php command that the original image provides.
Here is the documentation on the build:
directive, as there are quite a few other cool things you can do with it,
like specifying the Dockerfile if you don't want to put it into a subdirectory, and providing the context:
should you wish to bake in files to your new image
https://docs.docker.com/compose/compose-file/#build
这篇关于PHP Docker容器无故退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!