0

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

3

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

0

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:~$ 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.