我是docker的新手,并尝试使用最简单的docker-compose.yml来显示一个hello world页面,最终构建一个完整的LEMP堆栈,它与我的服务器具有相同的配置.然而,大多数教程已经过时,并且有很多使用docker的方法,我找不到只使用仍然是实际的Docker compose v3的方法.我检查了文档,这对初学者来说非常混乱,一直试图让它在过去的5个小时内工作,所以我想我会问.
泊坞窗,compose.yml
version: '3'
services:
web:
image: bitnami/Nginx:1.10.3-r0 #using this version as it's the same on my server
volumes:
- "./test.conf:/etc/Nginx/sites-available/test.local"
- "./test.conf:/etc/Nginx/sites-enabled/test.local"
- "./code:/var/www/html" #code contains only a basic index.html file
ports:
- "80:80"
test.conf
server {
listen 80;
listen [::]:80;
server_name test.local;
index index.html; #Only a basic helloworld index.html file
error_log /var/log/Nginx/error.log;
access_log /var/log/Nginx/access.log;
root /var/www/html;
}
我需要一个Dockerfile吗?教程似乎没有提到它是必要的.
注意:
尝试添加音量
- "./default.conf:/etc/Nginx/conf.d/default.conf"
但没有任何变化,欢迎页面仍然加载,而使用Nginx:latest我得到一个包含这句话的非常详细的错误:“unkNown:你是否试图将目录挂载到文件上(反之亦然)?检查指定的主机路径存在并且是期望的类型“.
关于docker-compose.yml的更新:
>如果没有“./code:/usr/share/Nginx/html”这一行,/usr/share / Nginx / html文件夹包含默认的index.html文件(预期)
>使用“./code:/usr/share/Nginx/html”行,/usr/share / Nginx / html文件夹是EMPTY!
>使用“./:/usr/share / Nginx / html”行,/usr/share / Nginx / html文件夹中有空的“code”文件夹和一堆我之前删除的随机测试文件.
在尝试之间,我运行我的重置脚本以确保我重新开始:
docker rm $(docker ps -a -q)
docker rmi $(docker images -q) --force
docker volume rm $(docker volume ls -q)
运行docker inspect< container>为卷返回此值,不确定该类型是“绑定”为bind mount而不是volume是正常的.
"Mounts": [
{
"Type": "bind",
"Source": "/e/DEV/sandBox/docker",
"Destination": "/usr/share/Nginx/html",
"Mode": "rw",
"RW": true,
"Propagation": "rprivate"
}
],
解决方法:
您可以轻松安装自己的hello world页面.我将使用官方Nginx:最新图片解释它,但如果你愿意,你可以用bitnami图像自己做.
首先是非常基本的.只需运行Nginx容器(不使用docker-compose).我将详细解释它和基本的,当然我可以尝试执行更高级或更快的命令来读取容器内的文件,但这对于初学者来说可能会令人困惑.所以只需运行容器并将其命名为my-Nginx:
$docker run --rm -d -p 80:80 --name my-Nginx Nginx
转到localhost:80,您将看到默认的Nginx页面.
现在,您可以使用它的名称在容器内执行. exec将带你’在容器内’,这样你就可以检查它的文件.
$docker exec -it my-Nginx bash
root@2888fdb672a1:/# cd /etc/Nginx/
root@2888fdb672a1:/etc/Nginx# ls
conf.d koi-utf mime.types Nginx.conf uwsgi_params
fastcgi_params koi-win modules scgi_params win-utf
现在使用cat读取Nginx.conf.
此文件中最重要的一行是:
include /etc/Nginx/conf.d/*.conf;
这意味着使用/读取该目录中的所有confs.
所以进入/etc/Nginx/conf.d/.
root@2888fdb672a1:~# cd /etc/Nginx/conf.d/
root@2888fdb672a1:/etc/Nginx/conf.d# ls
default.conf
default.conf是唯一的文件.在此文件中,您可以看到配置:
listen 80;
server_name localhost;
location / {
root /usr/share/Nginx/html;
index index.html index.htm;
}
服务器是本地主机,端口是80,将显示的文件位于/usr/share / Nginx / html /目录中
现在检查容器中的文件:
root@2888fdb672a1:/etc/Nginx/conf.d# cat /usr/share/Nginx/html/index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to Nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
...
这是预期的文件.这是我们可以看到的“欢迎来到Nginx”页面.
那么我们如何展示自己的index.html呢?只需将其挂载在/usr/share / Nginx / html中即可.
你将docker-compose.yaml看起来像这样.
version: '3'
services:
web:
image: Nginx:latest
volumes:
- ./code:/usr/share/Nginx/html
ports:
- "80:80"
代码目录只包含一个带hello world的index.html.
运行docker-compose up -d –build,当你卷曲localhost:80时,你会看到自己的index.html.
如果你真的想把你的代码放在/ var / www / html而不是/usr/share / Nginx中你可以这样做.
使用你的test.conf.在这里定义将文件放在/ var / www / html /中:
server {
listen 80;
listen [::]:80;
server_name test.local;
index index.html; #Only a basic helloworld index.html file
error_log /var/log/Nginx/error.log;
access_log /var/log/Nginx/access.log;
root /var/www/html;
}
在compose中,你将使用自己的conf覆盖default.conf,告诉Nginx查看/ var / www / html.
你的作品看起来像这样:
version: '3'
services:
web:
image: Nginx:latest
volumes:
- "./test.conf:/etc/Nginx/conf.d/default.conf"
- "./code:/var/www/html"
ports:
- "80:80"
现在,您还可以在自己指定的位置看到自己的index.html.答案很长,但我希望这会有所帮助.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。