Docker 可以看到 image.png 个文件,但是浏览器中没有图像显示
Docker can see image.png file, but no image is displayed in browser
我正在学习 Docker 和 Swoole。 运行 一个 Docker 容器,其中包含 2 个文件和一个空目录。在浏览器中访问 HTTP 服务器时,图像不显示(获取损坏的图像图标)。
我已经从外部网页尝试过 PNG,该图像显示正确。我也试过 "docker run -it hello-swoole sh" 并确认容器显示
data index.php image.png
Docker文件
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 的 none。感觉爱德华在这里的回答是正确的,我还没有正确处理所有请求。现在可以肯定的是,这个问题更像是一个 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();
我假设您还必须对服务器进行编程以提供静态文件。
我在这里找到了答案:https://www.swoole.co.uk/docs/modules/swoole-http-server/configuration ...
只需添加:
$http->set([
'document_root' => '/app',
'enable_static_handler' => true,
]);
完整更新代码
<?php
$http = new swoole_http_server("0.0.0.0", 8101);
$http->set([
'document_root' => '/app',
'enable_static_handler' => true,
]);
$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" height="200"></body></html>');
});
$http->start();
我正在学习 Docker 和 Swoole。 运行 一个 Docker 容器,其中包含 2 个文件和一个空目录。在浏览器中访问 HTTP 服务器时,图像不显示(获取损坏的图像图标)。
我已经从外部网页尝试过 PNG,该图像显示正确。我也试过 "docker run -it hello-swoole sh" 并确认容器显示
data index.php image.png
Docker文件
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 的 none。感觉爱德华在这里的回答是正确的,我还没有正确处理所有请求。现在可以肯定的是,这个问题更像是一个 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();
我假设您还必须对服务器进行编程以提供静态文件。
我在这里找到了答案:https://www.swoole.co.uk/docs/modules/swoole-http-server/configuration ...
只需添加:
$http->set([
'document_root' => '/app',
'enable_static_handler' => true,
]);
完整更新代码
<?php
$http = new swoole_http_server("0.0.0.0", 8101);
$http->set([
'document_root' => '/app',
'enable_static_handler' => true,
]);
$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" height="200"></body></html>');
});
$http->start();