【发布时间】:2019-12-11 03:40:32
【问题描述】:
我使用 Nginx 作为 Jenkins 服务器的代理。都在 Docker 容器中。
这个想法是 Jenkins 在端口 8080 上运行,端口 8080 暴露。 Nginx 监听 80 端口并将流量重定向到 8080 端口上的 Jenkins。如果您尝试直接访问 8080 端口,它将拒绝连接。
请查看 docker-compose.yml 文件:
version: '3.7'
services:
master:
build: ./jenkins-master
networks:
- jenkins-net
volumes:
- jenkins-log:/var/log/jenkins
- jenkins-data:/var/jenkins_home
nginx:
build: ./jenkins-nginx
ports:
- "80:80"
networks:
- jenkins-net
networks:
jenkins-net:
volumes:
jenkins-log:
jenkins-data:
Jenkins-master Dockerfile:
FROM jenkins/jenkins:alpine
LABEL maintainer=''
USER root
RUN mkdir /var/log/jenkins
RUN mkdir /var/cache/jenkins
RUN chown -R jenkins:jenkins /var/log/jenkins
RUN chown -R jenkins:jenkins /var/cache/jenkins
USER jenkins
ENV JAVA_OPTS='-Xmx8192m'
ENV JENKINS_OPTS=' --handlerCountMax=300 -- logfile=/var/log/jenkins/jenkins.log --webroot=/var/cache/jenkins/war'
这是 nginx.conf 文件:
server {
listen 80;
server_name localhost;
access_log off;
location / {
proxy_pass http://master:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto http;
proxy_max_temp_file_size 0;
proxy_connect_timeout 150;
proxy_send_timeout 100;
proxy_read_timeout 100;
proxy_buffer_size 8k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
}
这是我的 jenkins-nginx Dockerfile:
FROM nginx:mainline-alpine
RUN rm /etc/nginx/conf.d/default.conf
COPY jenkins.conf /etc/nginx/conf.d/jenkins.conf
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 80
CMD ["nginx"]
添加 Nginx Dockerfile 以确保完整性:
FROM nginx:mainline-alpine
RUN rm /etc/nginx/conf.d/default.conf
COPY jenkins.conf /etc/nginx/conf.d/jenkins.conf
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 80
CMD ["nginx"]
Jenkins.conf 文件:
daemon off;
user nginx;
worker_processes 2;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
use epoll;
accept_mutex off;
}
http {
include /etc/nginx/mime.types;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request"'
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" ';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
client_max_body_size 300m;
client_body_buffer_size 128k;
gzip on;
gzip_http_version 1.0;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_proxied any;
gzip_types text/plain text/css text/xml text/javascript application/xml application/xml+rss application/javascript application/json;
gzip_disable 'MSIE [1-6]\.';
gzip_vary on;
include /etc/nginx/conf.d/*.conf;
}
问题在于两者都是独立工作的,但是一旦我尝试将它们连接到一个网络上,它们就会崩溃。
两个服务都抛出localhost refused to connect 错误
【问题讨论】:
-
看起来 nginx 正在监听 80 端口并将“/”代理到 127.0.0.1:8080,我想你想要master:50000。如果 nginx 要代理,那么您也可能不想将您的 jenkins 容器映射到本地端口。
-
这个想法是 Jenkins 在 8080 端口上运行,并暴露了 8080 端口。 Nginx 在端口 80 上侦听并将流量重定向到端口 8080 上的 Jenkins。在端口 50000 上公开的 Docker 网络连接它们。
-
我在使用
docker-compose up --build重建图像时遇到了这个错误 -
Jenkins 位于 1 个容器中,实际运行在端口 8080 上,nginx 位于另一个容器中,运行在端口 80 上。您将 nginx 容器中的端口 80 映射到主机上的端口 80。 Nginx 然后应该将流量转发到 not 为 127.0.0.1 的 jenkins 容器。相反,您可以只使用服务名称和端口,例如 master:8080
-
你为什么使用 build: ./jenkins-master ?您是否使用自定义 jenkins 而不是官方 Jenkins Dockerfile?
标签: docker nginx jenkins docker-compose