我是一个码头初学者,我做的第一件事就是下载nginx并尝试将它安装在80:80端口,但Apache已经坐在那里了.
docker container run --publish 80:80 nginx
Run Code Online (Sandbox Code Playgroud)
和 docker container run --publish 3000:3000 nginx
我尝试这样做3000:3000在端口3000上使用它但它不起作用.并且它没有记录任何我可以用于referance的东西.
小智 10
当您从Docker开始时,您可以在DockerHub上找到有关图像的有用信息.例如,对于nginx,您有一个关于如何公开公共端口的部分.
你可以使用:
docker run --publish 3000:80 nginx
Run Code Online (Sandbox Code Playgroud)
您的localhost中的端口3000将被转发到端口80,这是nginx映像用于等待http连接的端口.
我还建议你阅读这篇关于Docker网络的官方文档.
接受的答案不会更改nginx正在启动的实际端口。
如果要更改nginx 在容器内部启动的端口,则必须修改容器内部的/etc/nginx/nginx.conf文件。
例如,要在端口9080上启动:
Docker文件
FROM nginx:1.17-alpine
COPY <your static content> /usr/share/nginx/html
COPY nginx.conf /etc/nginx/
EXPOSE 9080
CMD ["nginx", "-g", "daemon off;"]
Run Code Online (Sandbox Code Playgroud)
nginx.conf
# on alpine, copy to /etc/nginx/nginx.conf
user root;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
sendfile off;
access_log off;
keepalive_timeout 3000;
server {
listen 9080;
root /usr/share/nginx/html;
index index.html;
server_name localhost;
client_max_body_size 16m;
}
}
Run Code Online (Sandbox Code Playgroud)
现在要从您的计算机访问服务器:
docker build . -t my-app
docker run -p 3333:9080 my-app
Run Code Online (Sandbox Code Playgroud)
在浏览器中导航到localhost:3333,您将看到您的内容。
可能有一种方法可以包括默认的nginx.conf,并且仅覆盖server.listen = PORT属性,但是我对nginx的配置不太熟悉,因此我只重写了整个默认配置。
你写道你是一个初学者,所以首先我会提到 nginx 镜像的默认配置(我假设你使用的是标准镜像)是在 port 中侦听80。
这就是您不能映射到3000容器内端口的原因,因为没有进程侦听此端口。
现在,如果我理解正确,并且您将 nginx 与 docker 一起使用,我猜您希望能够配置容器的端口(而不是主机端口,因为这很简单)。
@mancini0 开始了一个很好的方向,但我将展示如何以更有活力的方式做到这一点。
我们将使用envsubst命令替换 shell 格式字符串中的环境变量。
此命令可用于官方 nginx 映像和 alpine 版本。
现在为解决方案。
第 1 步
在模板文件中编写您的 nginx 配置 - 我们称之为site.template:
server {
listen ${PORT};
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
Run Code Online (Sandbox Code Playgroud)
请注意 PORT 占位符。
步骤#2 - 使用 docker-compose
在/etc/nginx/conf.d目录中安装它,然后执行envsubst命令以使用模板作为参考,default.conf该文件将用于在容器内设置端口配置:
web:
image: nginx:alpine
volumes:
- ./site.template:/etc/nginx/conf.d/site.template
ports:
- "3000:8080"
environment:
- PORT=8080
command: /bin/sh -c "envsubst < /etc/nginx/conf.d/site.template > /etc/nginx/conf.d/default.conf && exec nginx -g 'daemon off;'"
Run Code Online (Sandbox Code Playgroud)
请注意:
1. 之后您需要执行 nginx 守护进程。
2. 我使用/bin/shand 不是/bin/bash因为我的基本图像是 alpine。
第 2 步(另一个选项) - 内联 docker run
如果由于某种原因您不想使用 docker-compose,您可以使用以下 bash 脚本:
#!/usr/bin/env bash
##### Variables #####
PORT=8080 #Or $1 if you pass it from command line
TEMPLATE_DIR=$(pwd)/site.template
TEMPLATE_REMOTE_DIR=/etc/nginx/conf.d/site.template
IMAGE_NAME=nginx:alpine
echo "Starting nginx on port: $PORT ..."
##### The docker command #####
docker run -p 3000:$PORT -v $TEMPLATE_DIR:$TEMPLATE_REMOTE_DIR $IMAGE_NAME \
/bin/sh -c "envsubst < $TEMPLATE_REMOTE_DIR > /etc/nginx/conf.d/default.conf && exec nginx -g 'daemon off;'"
Run Code Online (Sandbox Code Playgroud)
(*) 您也可以使用CMD命令将其写入 Dockerfile 中,但我不建议您这样做。
您可以使用 nginx.conf 将其更改为任何端口。
nginx.conf将包含与 nginx 相关的所有配置。这里端口的默认值为 80,我们可以如下更改。
nginx.conf
server {
listen 3000;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
}
include /etc/nginx/extra-conf.d/*.conf;
}
Run Code Online (Sandbox Code Playgroud)
并在 DockerFile 中添加一行:
COPY ./nginx.conf /etc/nginx/conf.d/default.conf
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8788 次 |
| 最近记录: |