Nat*_*per 7 nginx docker docker-compose
我是新来的码头工人,并试图与展示,最终一个完整的世界您好网页的基础上最重要的是最简单的搬运工,compose.yml LEMP堆栈,将有相同的配置为我的服务器。但是,大多数教程已过时,并且使用docker的方式太多了,我仅使用Docker compose v3仍然无法找到一种方法。我检查了一下文档,对于初学者来说也很令人困惑,在过去的5个小时里一直在努力使其正常工作,所以我想我应该问一下。
docker-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"
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
我需要一个Dockerfile吗?教程似乎没有提到它是必需的。
注意:
尝试添加卷
- "./default.conf:/etc/nginx/conf.d/default.conf"
Run Code Online (Sandbox Code Playgroud)
但是没有任何变化,欢迎页面仍然加载,而使用nginx:latest时,我收到一个非常冗长的错误,其中包含以下短语:“未知:您是否尝试将目录挂载到文件上(反之亦然)?检查指定的主机路径存在,并且是预期的类型”。
有关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)
Run Code Online (Sandbox Code Playgroud)
运行docker inspect <container>将为卷返回此值,但不确定将类型“绑定”为绑定安装而不是卷是正常的。
"Mounts": [
{
"Type": "bind",
"Source": "/e/DEV/sandbox/docker",
"Destination": "/usr/share/nginx/html",
"Mode": "rw",
"RW": true,
"Propagation": "rprivate"
}
],
Run Code Online (Sandbox Code Playgroud)
挂载自己的hello world页面很容易。我将使用官方nginx:latest图像对其进行解释,但是如果您愿意,您可以使用bitnami图像自己完成。
首先很基本。只需运行nginx容器(无需docker-compose)。我将详细和基本地解释它,当然我可以尝试执行更高级或更快速的命令来读取容器内的文件,但这对于初学者可能会造成混淆。因此,只需运行容器并命名my-nginx:
$ docker run --rm -d -p 80:80 --name my-nginx nginx
Run Code Online (Sandbox Code Playgroud)
转到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
Run Code Online (Sandbox Code Playgroud)
现在nginx.conf使用读取cat。该文件中最重要的行是:
include /etc/nginx/conf.d/*.conf;
Run Code Online (Sandbox Code Playgroud)
这意味着confs该目录中的所有内部文件均已使用/读取。所以进入/etc/nginx/conf.d/。
root@2888fdb672a1:~# cd /etc/nginx/conf.d/
root@2888fdb672a1:/etc/nginx/conf.d# ls
default.conf
Run Code Online (Sandbox Code Playgroud)
该default.conf是唯一的文件。在此文件中,您会看到配置:
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
Run Code Online (Sandbox Code Playgroud)
服务器是本地主机,端口是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>
...
Run Code Online (Sandbox Code Playgroud)
这是预期的文件。我们可以看到“欢迎使用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"
Run Code Online (Sandbox Code Playgroud)
The code directory just contains an index.html with hello world.
Run docker-compose up -d --build and when you curl localhost:80 you will see your own index.html.
If you really want to put your code in /var/www/html instead of /usr/share/nginx you can do that.
Use your test.conf. Here you define to put your file in /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;
}
Run Code Online (Sandbox Code Playgroud)
In the compose you will overwrite the default.conf with your own conf where you tell nginx to look in /var/www/html.
Your compose can look like this:
version: '3'
services:
web:
image: nginx:latest
volumes:
- "./test.conf:/etc/nginx/conf.d/default.conf"
- "./code:/var/www/html"
ports:
- "80:80"
Run Code Online (Sandbox Code Playgroud)
Now you will also see your own index.html while it's on your own specified location. Long answer but I hope this helps.
| 归档时间: |
|
| 查看次数: |
4870 次 |
| 最近记录: |