DEV Community

Theodor Heiselberg
Theodor Heiselberg

Posted on

3

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

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay