How to set up a simple web server development container for VS Code
Sometimes it’s nice to have a lightweight development container that automatically serves the contents of a directory, for rapid prototyping or building static web pages—without having to setup a tool chain on your local environment or install web server software (and its dependencies) on your local machine.
If you’re using VS Code’s extension for developing inside containers, below is a recipe for a comfortable development setup containing these three main attributes:
- Lightweight; small image footprint and minimal container boot up time
- Automatic; once the container starts, the files are being served on a local port without the need to run another command
- Complete; has all the dependencies required by VS Code for the full development lifecycle
To achieve this, we’ll use the Alpine Linux operating system with Nginx as our development web server.
Step 1: define development container in a Dockerfile
We’ll include the dependencies required by VS Code’s remote containers extension as well as the specific tools we need for development.
.devcontainer/Dockerfile
:
You'll want to replace /workspaces/my-app
with whatever directory VS Code mounts your source code to inside the container.
Step 2: configure VS Code to utilize our image
With our image definition done, we’ll add some configuration to instruct VS Code to run it.
.devcontainer/devcontainer.json
:
Step 3: profit
With those files in place, VS Code has everything it needs to spin up the development container with the source code mounted inside. Once it’s running, you can load up your project’s files in the browser (or even try VS Code’s built-in development browser) on port 8080
for easy side-by-side development:
Happy prototyping!