Sometimes people just want to share some files that can be publicly available via broswer, there’s nothing confidential about the html or pdf, You just want to share the files via an url that can be viewed directly inside the broswer, while hosting a ftp server seems like an overkill. It turns out this is quite easy to do. In this post Docker is used to handle this issue.
Docker-Compose File
I really like to use a docker-compose.yaml file than typing a concatenation of commands like docker exec. In order for our static site to work, we will use Nginx based docker image, the beautiful thing about this image is that you don’t have to do anything extra, so here’s docker-compose.yaml file in your site folder.
version: '3.8'
services:
web:
image: nginx:alpine
restart: always # This will ensure the container always restarts
ports:
- "8080:80"
volumes:
- ./Files:/usr/share/nginx/html
This docker-compose.yaml will use nginx:alpine image as alpine is a very small Linux distro which will save some storage space. There are some other options placed in the yaml, one thing you should be aware of is called the mounted volumes, you want to make sure to edit the ./Files string to the string of the folder you want to share the documents. So you put the documents in for example ./Files wtih name my_page.html, you can access that file with the url suppose the container is running at your local machnie, otherwise you could type in the specific ip address or domain name of the server which the container runs: http://localhost:8080/my_page.html
.
Running Container
After the docker files is properly taken care of, it is time to running the container and use the functionality of the site. Running container using docker-compose is really easy.
docker-compose up -d
Now you have your static server managed by nginx, I will not dive into the details of nginx, you don’t have to know it in order for it to work.
You can try to put some files like pdfs, htmls into the folder that is mounted to the docker container /usr/share/nginx/html
.
Conclusion
Hosting a static content server is not that difficult to do, nginx have already provided a docker image that is ready to be used, which you could use without knowing how nginx works. But knowing how to use nginx is a task that allows you to do lots of things.
Leave a Reply