docker 部署前端nginx服务详细步骤
时间: 2023-07-26 17:05:54 浏览: 222
以下是 Docker 部署前端 Nginx 服务的详细步骤:
1. 编写 Dockerfile 文件:
```dockerfile
# 基础镜像
FROM nginx:alpine
# 删除 Nginx 默认配置文件
RUN rm /etc/nginx/conf.d/default.conf
# 将本地 Nginx 配置文件拷贝到 Docker 容器中
COPY nginx.conf /etc/nginx/conf.d/
COPY dist/ /usr/share/nginx/html/
```
2. 在本地创建 Nginx 配置文件 nginx.conf:
```conf
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
```
3. 在本地构建镜像:
```bash
docker build -t my-nginx .
```
4. 运行容器:
```bash
docker run --name my-nginx-container -p 80:80 -d my-nginx
```
其中,`my-nginx-container` 为容器名称,`80:80` 表示将容器内的 `80` 端口映射到宿主机的 `80` 端口,`my-nginx` 为上一步构建的镜像名称。
5. 访问前端服务:
在浏览器中访问 `http://localhost` 即可查看前端服务。如果是在远程服务器上运行容器,则需要将 `localhost` 替换为服务器的 IP 地址或域名。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20250102104920.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)