Maybe you need a web server for a simple static site. For instance, to test out the <script type="module">
. Perform a Lighthouse Audit. Or preview your HTML page through you phone.
But it can be tedious to install, configure & fire up a web server on your local machine.
Today I'm going to show you how you can save some precious time & serve your static assets with this simple Docker Compose configuration.
Steps
1 — Go back to your base directory
$ cd ~/
2 — Create the working directory
$ mkdir docker-nginx-demo
3 — Go into that directory
$ cd docker-nginx-demo
4 — Create the Docker & Docker Compose configuration
$ touch docker-compose.yaml
5 — Create the directory that will hold your HTML files
$ mkdir src
6 — Write something for your first page
$ echo "Hello world!" > src/index.html
7 — Edit your Docker Compose configuration file
$ vim docker-compose.yaml
version: "3"
services:
client:
image: nginx
ports:
- 8000:80
volumes:
- ./src:/usr/share/nginx/html
8 — Start the server
$ docker-compose up --detach
9 — Open your page
$ chromium http://localhost:8000
10 — Enjoy!
That's it! You can enjoy your HTML page with your favorite browser at http://localhost:8000.
No need for any Apache, NGINX, MAMP, XAMPP, LAMP or any web server installed on your machine. Only Docker & Docker Compose are needed!
Top comments (6)
Then you can add a simple healthcheck and everything will be monitored forever :)
Thanks! Simple and effective! :-)
Very Useful . I have a doubt. How do we load modules of Nginx like monicalent.com/blog/2019/01/06/res... .
this is the way!
Nice post! I think the Dockerfile contents are missing in the example.
Yes it's a mistake. At first I though I would need a Dockerfile but not at all. That's what made me happy about that! But I'll correct the article. Thanks for pointing that out!