1、拉取nginx镜像
docker pull nginx:1.18.0
2、nginx配置
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
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;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
location /admin{
alias /usr/local/html/admin;
index index.html;
}
location /web {
alias /usr/local/html/web;
index index.html;
}
location /api/ {
proxy_pass http://192.168.32.136:29080/;
}
}
}
3、运行nginx
docker run -d -p 80:80 --name nginx -v /home/zhu/nginx/nginx.conf:/etc/nginx/nginx.conf -v /home/zhu/html/:/usr/local/html/ --restart always nginx:1.18.0
4、
宿主机 /home/zhu/html/ 映射到容器 /usr/local/html/下,nginx路径映射 admin 映射到 /usr/local/html/admin web 映射到/usr/local/html/web
5、宿主机ip 192.168.32.136
运行java程序 ,程序接口
http://192.168.32.136:29080/test
通过nginx容器访问宿主机接口
http://192.168.32.136/api/test
docker pull docker.io/portainer/portainer
docker run -d -p 29000:9000 --restart=always -v /var/run/docker.sock:/var/run/docker.sock --name prtainer docker.io/portainer/portainer
http://192.168.32.136:29000/#/containers
admin
zsj381599113qq
nginx 容器内访问宿主机服务,可以用宿主机的ip加端口访问相应的服务。