<p>running and copying files into the container:</p>
<pre><code>root@mordor:~# 
root@mordor:~# sudo docker run --name docker-nginx -p 80:80 -d nginx
29b4280504c2d46c10b8459de0844ba30bce0c1bc97b70521c3f812f29d17c27
root@mordor:~# 
root@mordor:~# docker cp index.html docker-nginx:/indx.html
root@mordor:~# 
root@mordor:~# docker exec -it docker-nginx bash
root@29b4280504c2:/# 
root@29b4280504c2:/# ls
bin   dev          docker-entrypoint.sh  home       lib    media  opt   root  sbin  sys  usr
boot  docker-entrypoint.d  etc           indx.html  lib64  mnt    proc  run   srv   tmp  var
root@29b4280504c2:/# 
root@29b4280504c2:/# cd /usr/share/nginx/html/
root@29b4280504c2:/usr/share/nginx/html# 
root@29b4280504c2:/usr/share/nginx/html# ls
50x.html  index.html
root@29b4280504c2:/usr/share/nginx/html# 
root@29b4280504c2:/usr/share/nginx/html# mv index.html index.html.bkup
root@29b4280504c2:/usr/share/nginx/html# 
root@29b4280504c2:/usr/share/nginx/html# cp /indx.html ./index.html
root@29b4280504c2:/usr/share/nginx/html# 
root@29b4280504c2:/usr/share/nginx/html# exit
exit
root@mordor:~# 
root@mordor:~# docker container list
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
29b4280504c2        nginx               &quot;/docker-entrypoint.…&quot;   2 minutes ago       Up 2 minutes        0.0.0.0:80-&gt;80/tcp   docker-nginx
root@mordor:~# 
root@mordor:~# docker stop docker-nginx
docker-nginx
root@mordor:~# 
root@mordor:~# docker start docker-nginx
docker-nginx
root@mordor:~# 
</code></pre>
<p>so that:</p>
<pre><code>nicholas@mordor:~$ 
nicholas@mordor:~$ lynx http://localhost/ --dump
   hello nginx docker world
nicholas@mordor:~$ 
</code></pre>

CC BY-SA 4.0
2020-11-14T04:36:41.913Z
2020-11-14T04:36:41.913Z
Nicholas Saunders
<p>The &quot;official&quot; 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.</p>
<p>To copy files, have a folder with your <code>index.html</code> and a <code>Dockerfile</code> that looks like this:</p>
<pre><code>FROM nginx
COPY index.html /usr/share/nginx/html
</code></pre>
<p>Then build a new image with <code>docker build -t my-nginx .</code></p>
<p>To start a container with your own files as a volume, you can run the container like this:</p>
<pre><code>docker run -p 8080:80 -v ${PWD}:/usr/share/nginx/html nginx
</code></pre>
<p>All the files in your current folder (including the <code>index.html</code>) will now be available via <code>http://localhost:8080/index.html</code>.</p>
<p>Most of this information is covered in the documentation for the image at <a href="https://hub.docker.com/_/nginx" rel="nofollow noreferrer">https://hub.docker.com/_/nginx</a></p>

CC BY-SA 4.0
2020-11-14T06:59:03.547Z
2021-03-10T18:58:36.333Z
2021-03-10T18:58:36.333Z
Evgeny Zislis
<p>How do I serve static content from the default <code>nginx</code> docker image?  Given:</p>
<pre><code>root@mordor:~# 
root@mordor:~# docker image ls nginx
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              c39a868aad02        8 days ago          133MB
root@mordor:~# 
</code></pre>
<p>where and how can a hello world be configured?</p>
<p>In the context of:</p>
<p><a href="https://askubuntu.com/q/1292182/847449">https://askubuntu.com/q/1292182/847449</a></p>

<docker><docker-compose><dockerfile><nginx><servers>
How to serve a custom index.html using the stock nginx docker image?
Nicholas Saunders
# How to serve a custom index.html using the stock nginx docker image?

(Asked by: username_0 on 2020-11-14)

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

**Tags:** docker, docker-compose, dockerfile, nginx, servers


---

## 2 answers

### Answer by username_1 on 2020-11-14. Score: 7 | Accepted answer

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


---

### Answer by username_0 on 2020-11-14. Score: 1

running and copying files into the container:
root@mordor:~# 
root@mordor:~# sudo docker run --name docker-nginx -p 80:80 -d nginx
29b4280504c2d46c10b8459de0844ba30bce0c1bc97b70521c3f812f29d17c27
root@mordor:~# 
root@mordor:~# docker cp index.html docker-nginx:/indx.html
root@mordor:~# 
root@mordor:~# docker exec -it docker-nginx bash
root@29b4280504c2:/# 
root@29b4280504c2:/# ls
bin   dev          docker-entrypoint.sh  home       lib    media  opt   root  sbin  sys  usr
boot  docker-entrypoint.d  etc           indx.html  lib64  mnt    proc  run   srv   tmp  var
root@29b4280504c2:/# 
root@29b4280504c2:/# cd /usr/share/nginx/html/
root@29b4280504c2:/usr/share/nginx/html# 
root@29b4280504c2:/usr/share/nginx/html# ls
50x.html  index.html
root@29b4280504c2:/usr/share/nginx/html# 
root@29b4280504c2:/usr/share/nginx/html# mv index.html index.html.bkup
root@29b4280504c2:/usr/share/nginx/html# 
root@29b4280504c2:/usr/share/nginx/html# cp /indx.html ./index.html
root@29b4280504c2:/usr/share/nginx/html# 
root@29b4280504c2:/usr/share/nginx/html# exit
exit
root@mordor:~# 
root@mordor:~# docker container list
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
29b4280504c2        nginx               "/docker-entrypoint.…"   2 minutes ago       Up 2 minutes        0.0.0.0:80->80/tcp   docker-nginx
root@mordor:~# 
root@mordor:~# docker stop docker-nginx
docker-nginx
root@mordor:~# 
root@mordor:~# docker start docker-nginx
docker-nginx
root@mordor:~# 

so that:
nicholas@mordor:~$ 
nicholas@mordor:~$ lynx http://localhost/ --dump
   hello nginx docker world
nicholas@mordor:~$


---

