HiveBrain v1.2.0
Get Started
← Back to all entries
snippetdockerMinor

How to serve a custom index.html using the stock nginx docker image?

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
theimagedockerstocknginxcustomserveusinghowindex

Problem

How do I serve static content from the default nginx docker image? Given:

root@mordor:~# 
root@mordor:~# docker image ls nginx
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              c39a868aad02        8 days ago          133MB
root@mordor:~#


where and how can a hello world be configured?

In the context of:

https://askubuntu.com/q/1292182/847449

Solution

The "official" nginx image has a specific path where it looks for files. You can either copy a file into the image by creating a new image from it or mount your files into this path as a volume.

To copy files, have a folder with your index.html and a Dockerfile that looks like this:

FROM nginx
COPY index.html /usr/share/nginx/html


Then build a new image with docker build -t my-nginx .

To start a container with your own files as a volume, you can run the container like this:

docker run -p 8080:80 -v ${PWD}:/usr/share/nginx/html nginx


All the files in your current folder (including the index.html) will now be available via http://localhost:8080/index.html.

Most of this information is covered in the documentation for the image at https://hub.docker.com/_/nginx

Code Snippets

FROM nginx
COPY index.html /usr/share/nginx/html
docker run -p 8080:80 -v ${PWD}:/usr/share/nginx/html nginx

Context

StackExchange DevOps Q#12757, answer score: 6

Revisions (0)

No revisions yet.