DEV Community

Theodor Heiselberg
Theodor Heiselberg

Posted on

VS Code devcontainer and Nginx

Let's get started with using Nginx

The purpose of this post is to show how one can get up and running with a playground for Nginx.

This is not a toturial for usning Nginx.

(# TODO: Add ssh using uptime robot)

You'll need these ingredients to get up and running:

Image description

.devcontainer/devcontainer.json

{
    "name": "nginx-playground",
    "service": "dev-machine",
    "dockerComposeFile": "docker-compose.yml",
    "workspaceFolder": "/workspace",
    "mounts": [
        "source=${localWorkspaceFolder}/.devcontainer/.nginx,target=/usr/share/nginx/,type=bind"
    ],
    "postStartCommand": "nginx"
}
Enter fullscreen mode Exit fullscreen mode

.devcontainer/docker-compose.yml

name: nginx-playground-dc

services:
  dev-machine:
    build: 
      context: .
      dockerfile: Dockerfile.nginx
    volumes:
      - ..:/workspace
    networks:
      - internal
    ports:
      - "8472:80"
    command: ["sleep", "infinity"]

networks:
  internal:
    driver: bridge
Enter fullscreen mode Exit fullscreen mode

.devcontainer/Dockerfile.nginx

FROM nginx:alpine

EXPOSE 80
Enter fullscreen mode Exit fullscreen mode

.devcontainer/.nginx/html/index.html

<!DOCTYPE html>
    <body>Hello</body>
</html>
Enter fullscreen mode Exit fullscreen mode

.devcontainer/.nginx/nginx.conf/nginx.conf

events {}

http {
    server {
        listen       80;
        server_name  localhost;

        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    }
}

location /nginx_status {
    stub_status on;
    allow 127.0.0.1;  # Restrict access to localhost for security
    deny all;
}
Enter fullscreen mode Exit fullscreen mode

Now run that devcontainer and open a browser on your host machine:

Image description

Or from the host machines terminal run:
curl http://localhost:8472

Image description

Top comments (0)