通过Dockerfile修改Nginx默认首页后,运行容器仍显示默认页的问题
Let's break down the most likely issues and fixes here — this is a super common snag with Nginx Docker setups, so we'll get it sorted quickly!
1. Nginx Doesn't Know to Load welcome.html by Default
This is the #1 reason for this problem. The official Nginx image is configured to look for index.html (or index.htm) as the default landing page — it has no idea about your welcome.html file unless you explicitly tell it to prioritize it.
Fixes for this:
Simplest fix: Rename your file to
index.html
Just rename your local HTML file toindex.html, then update your Dockerfile to copy it:FROM nginx:latest WORKDIR /usr/share/nginx/html COPY index.html index.htmlRebuild your image (
docker image build -t html_nginx .) and rerun the container — this should immediately load your custom page.Customize Nginx's configuration to use
welcome.html
If you want to keep the filename aswelcome.html, create a custom Nginx config file (let's call itdefault.conf) with the following content:server { listen 80; listen [::]:80; server_name localhost; location / { root /usr/share/nginx/html; index welcome.html index.html index.htm; # Add welcome.html as the first priority } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }Then update your Dockerfile to copy this config into the Nginx config directory:
FROM nginx:latest WORKDIR /usr/share/nginx/html COPY welcome.html welcome.html COPY default.conf /etc/nginx/conf.d/default.confRebuild and rerun the container — Nginx will now prioritize your
welcome.htmlover the defaultindex.html.Temporary fix: Override Nginx's index at runtime
If you don't want to rebuild the image, you can pass a custom Nginx command when starting the container:docker container run -p 80:80 --rm html_nginx nginx -g "daemon off; index welcome.html;"
2. Verify welcome.html Was Actually Copied to the Image
Sometimes the issue is that your welcome.html file wasn't copied into the image at all. Let's check:
- Run a temporary container to inspect the image's contents:
Once inside, navigate todocker run --rm -it html_nginx /bin/bash/usr/share/nginx/htmland list the files withls. Ifwelcome.htmlisn't there:- Double-check that you're running
docker buildfrom the same directory wherewelcome.htmlis stored. - Make sure there's no typo in the filename (case sensitivity matters! e.g.,
Welcome.htmlvswelcome.html). - Check your build logs for errors like
COPY failed: no source files were specified— this means Docker couldn't find the file.
- Double-check that you're running
3. (Less Likely) Port Conflict or Cached Image
- Port conflict: If another service is using port 80 on your host, you might be hitting a different Nginx instance. Check with
netstat -tulpn | grep 80(Linux/macOS) ornetstat -ano | findstr ":80"(Windows) to see what's listening on port 80. - Cached image: Docker might be using an old cached version of your image. Rebuild with the
--no-cacheflag to force a fresh build:docker image build --no-cache -t html_nginx .
内容的提问来源于stack exchange,提问作者Venkat

