【发布时间】:2019-08-23 03:43:13
【问题描述】:
我正在学习 Docker 和 Swoole。运行一个包含 2 个文件和一个空目录的 Docker 容器。在浏览器中访问 HTTP 服务器时,图像不显示(获取损坏的图像图标)。
我已经尝试了来自外部网页的 PNG,该图像已正确显示。我也试过“docker run -it hello-swoole sh”并确认容器显示
data index.php image.png
Dockerfile
FROM php:7.2-fpm
RUN apt-get update && apt-get install vim -y && \
apt-get install openssl -y && \
apt-get install libssl-dev -y && \
apt-get install wget -y
RUN cd /tmp && wget https://pecl.php.net/get/swoole-4.2.9.tgz && \
tar zxvf swoole-4.2.9.tgz && \
cd swoole-4.2.9 && \
phpize && \
./configure --enable-openssl && \
make && make install
RUN touch /usr/local/etc/php/conf.d/swoole.ini && \
echo 'extension=swoole.so' > /usr/local/etc/php/conf.d/swoole.ini
RUN mkdir -p /app/data
WORKDIR /app
COPY ./app /app
EXPOSE 8101
CMD ["/usr/local/bin/php", "/app/index.php"]
index.php
<?php
$http = new swoole_http_server("0.0.0.0", 8101);
$http->on("start", function ($server) {
echo "Swoole http server is started at http://127.0.0.1:8101\n";
});
$http->on("request", function ($request, $response) {
$response->header("Content-Type", "text/html; charset=utf-8");
$response->end('<!DOCTYPE html><html lang="en"><body><img src="image.png"></body></html>');
});
$http->start();
知道为什么 image.png 没有显示吗?
更新 以下用于显示图像,但没有显示任何 HTML。感觉爱德华的答案在这里是正确的,我还没有正确处理所有请求。可以肯定的是,现在这个问题更像是一个 Swoole 问题而不是 Docker 问题。
<?php
$http = new swoole_http_server("0.0.0.0", 8101);
$http->on("start", function ($server) {
echo "Swoole http server is started at http://127.0.0.1:8101\n";
});
$http->on("request", function ($request, $response) {
$response->header('Content-Type', 'image/png');
$response->sendfile('image.png'); // code seems to stop executing here
$response->header("Content-Type", "text/html; charset=utf-8");
$response->end('<!DOCTYPE html><html lang="en"><body><img src="/image.png"></body></html>');
});
$http->start();
【问题讨论】:
-
当您使用此命令
docker run -it hello-swoole sh确认图像存在时,您是否拉取了此容器hub.docker.com/r/cinctree/hello-swoole?你的 Docker 文件和 hello-swoole 是两个独立的容器。我会尝试将文件从您的主机复制到您的 PHP 容器中。COPY ./local.png /app/container.png看看能不能用复制命令注入图片 -
我使用命令
docker build -f ./Dockerfile -t hello-swoole .构建了容器我尝试使用除hello-swoole 之外的其他标签,例如运行docker build -f ./Dockerfile -t garrett .然后运行docker run -it garrett sh和dir仍然显示data index.php image.png -
很有趣,但很好澄清。听起来你的 Docker 文件是正确构建的。可能是 img 标签
<img src="image.png">的路径问题。<img src="/image.png">工作吗?什么是绝对路径,是否包括在图像工作之前? -
<img src="/image.png">和<img src="/app/image.png">没有骰子...认为 Edward 可能会有所作为。 -
看我下面的答案,想通了!