环境
- linux centos 7.6
- Docker version 24.0.5, build ced0996
- docker-compose version v2.20.2
需求
使用 Dockerfile
构建 nginx
镜像,访问静态资源
目标结构
[root@node2 offlinemap]# tree
.
├── Dockerfile
├── docker-run.sh
├── docker-stop.sh
├── Makefile
├── roadmap
│ └── index.html
└── scripts
└── nginx
└── api_proxy.conf
index.html
roadmap/index.html
hello nginx roadmap !!!
api_proxy.conf
nginx 配置文件
scripts/nginx/api_proxy.conf
server {
listen 80; #监听容器内的端口
server_name cert-view-web;
client_max_body_size 10m;
# 配置前端静态文件目录
location / {
index index.html index.htm;
root /usr/share/nginx/html;
try_files $uri $uri/ /index.html;
}
# 配置后台go服务api接口服务 代理到8081端口
}
Dockerfile
FROM nginx:1.16.1
MAINTAINER wanghaima
# 复制配置文件
COPY scripts/nginx/api_proxy.conf /etc/nginx/conf.d/api_proxy.conf
# 复制宿主机当前路径上的roadmap目录,到容器/usr/share/nginx/目录中,并改名为html
COPY roadmap /usr/share/nginx/html
EXPOSE 88
# 防止主程序退出后,nginx容器也就退出了
CMD ["nginx", "-g", "daemon off;"]
Makefile
.PHONY: build run deploy check
check:
@revive ./...
build:
# docker build offlinemap_api image
@docker build -t 192.168.2.103/public/offlinemap:main-V1.0.0 .
@echo "offlinemap_api image build successful"
run:
# docker run offlinemap_api container
@if [ $(shell docker ps -aq --filter name=offlinemap_api --filter publish=88) ]; then docker rm -f offlinemap_api; fi
# 这里注意-v挂载的宿主机的地址写部署时的实际决对路径
@docker run --name=offlinemap_api -p 88:80 -v /data/docker/offlinemap/scripts/nginx/api_proxy.conf:/etc/nginx/conf.d/api_proxy.conf -d --restart=always 192.168.2.103/public/offlinemap:main-V1.0.0
@docker image prune -f
@echo "offlinemap_api api service is running successful"
stop:
# docker delete offlinemap_api container
@if [ $(shell docker ps -aq --filter name=offlinemap_api --filter publish=88) ]; then docker rm -f offlinemap_api; fi
@echo "go-admin stop success"
docker-up:
# docker-compose up -d run offlinemap_api container
@if [ $(shell docker ps -aq --filter name=offlinemap_api --filter publish=88) ]; then docker-compose down; fi
@docker-compose up -d
@docker image prune -f
@echo "offlinemap_api api service is running successful"
docker-down:
# docker-compose down stop offlinemap_api container
@docker-compose down
@echo "offlinemap_api api service is down successful"
deploy:
# @git checkout main
# @git pull origin main
make build
make run
docker-compose.yml
docker-compose.yml
version: '3.8'
services:
offline-map-api:
container_name: offlinemap
image: 192.168.2.103/public/offlinemap:main-V1.0.0
privileged: true
restart: always
ports:
- 88:80
volumes:
# 宿主机的地址:容器内的地址
- ./scripts/nginx/api_proxy.conf:/etc/nginx/conf.d/api_proxy.conf
#network_mode: host # 使用network_mode:host模式,上面的ports端口映射会失效 访问端口将以nginx中配置的为准
networks:
- myweb
networks:
myweb:
driver: bridge
docker-run.sh
#!/bin/bash
docker-compose up -d
docker-stop.sh
#!/bin/bash
docker-compose down
启动
以下任选一条命令执行即可
- docker-compose up -d
- make docker-up
- sh docker-stop.sh
- make docker run
测试服务
[root@node2 offlinemap]# curl 192.168.2.102:88
hello nginx roadmap !!!
停止
以下任选一条命令执行即可
- docker-compose down
- make docker-donw
- sh docker-stop.sh
- make docker stop
作者:海马 创建时间:2023-08-27 09:09
最后编辑:海马 更新时间:2024-08-03 21:41
最后编辑:海马 更新时间:2024-08-03 21:41