How to run a webserver for development in seconds

How to run a webserver for development in seconds

As a web developer you have to run a local webserver from time to time. I show you how to run a webserver for development in seconds with no dependencies but Docker.

As a web developer I often come in the situation where I want to serve local files on my computer through a webserver. I searched for an easy way to do this without bloating up my system by installing additional software like a webserver and without having to spent time in editing config files or something.

I found a simple way to run a webserver within seconds to serve local files on my computer and I want to share it with you. The only requirement for this to work is Docker. So make sure you have installed Docker before continuing. This approach will only work for static files. If you want to have a local webserver running PHP or something this guide is not for you.

How to run a webserver with Docker in seconds

We will use the Docker package tobilg/mini-webserver which provides a webserver to expose static files from the Docker host via HTTP. I prepared a few examples to show how you can run a local webserver with this Docker package. The webserver will serve the content of your directory at http://127.0.0.1:8080 (you may have to replace 8080 with the port you choose).

1) The following command will start a docker webserver listening on port 8080 serving static files from the /tmp/website directory. The web server will run in the foreground and will be deleted after it has been stopped:

docker run --rm -p 8080:3000 -v /tmp/website:/app/public:ro tobilg/mini-webserver:0.5.1

2) The following command will start a docker webserver listening on port 8090 serving static files from current working directory. The web server will run in the foreground and will be deleted after it has been stopped:

docker run --rm -p 8090:3000 -v "$PWD":/app/public:ro tobilg/mini-webserver:0.5.1

3) The following command will start a docker webserver listening on port 8085 serving static files from the current working directory. The web server will run in the background and will be deleted after it has been stopped:

docker run --rm -p 8085:3000 -v "$PWD":/app/public:ro -d tobilg/mini-webserver:0.5.1

How to run multiple webserver with Docker at the same time

Using the commands above you can easily run multiple local webservers with Docker at the same time. The simplest way is to execute the command to start a Docker webserver in the background multiple times. Just make sure you define a unique port for every webserver.

How to stop a local webserver

If you want to stop a local webserver running inside a Docker container you first have to get a list of the Docker containers currently running. You can get this list with the following command:

docker ps

The list shows the Docker containers currently running and their ID. You can stop a Docker container using its ID. To stop a Docker container with the ID "1a2b3c" you can use the following command:

docker stop 1a2b3c

Final Thoughts

That's it so far. I hope this has been helpful to you. Please share it to help others as well. If you have any questions or recommendations please comment below or message me at Twitter. Thank you.

About

Hi! My Name is Bastian. I am a Software-Developer & IT-Freelancer located in Germany.

Please leave me a comment:

 
Homepage Impressum Datenschutz