本ドキュメントのゴール

  1. Dockerを使ってNGINXを稼働させる

  2. NGINXを稼働させるWebサーバー内のHTMLファイルを表示するところまで

環境

  • ハードウェア

    • Raspberry Pi 4

  • OS

    • Ubuntu 24.04.1 LTS

  • 前提条件

    • DockerおよびDocker Composeのインストール済みであること

手順

NGINXコンテナの起動

  1. コンテナ用のディレクトリを作成

    $ mkdir -p docker/nginx_sample
    $ cd docker/nginx_sample
  2. 任意のディレクトリに`docker-compose.yml`を作成

    version: "3"
    
    services:
      nginx:
        container_name: nginx
        image: nginx:latest
        build:
          ./nginx
        ports:
          - "14080:80"
        volumes:
          - .nginx/html:/usr/share/nginx/html
          - .nginx/conf.d:/etc/nginx/conf.d # nginxのconf.dをマウント
  3. マウント用のフォルダを作成

    $ mkdir -p ./nginx/html
    $ mkdir -p ./nginx/conf.d
  4. confディレクトリにnginxのconfファイルを作成

    $ nano ./nginx/conf.d/default.conf
  5. confファイルを編集する

    server {
        listen       80;
        server_name  localhost;
        location / {
            # docker内のディレクトリを指定
            root /usr/share/nginx/html;
            index  index.html index.htm;
        }
    }
  6. HTMLファイルを作成する

    $ nano ./nginx/html/index.html
  7. HTMLファイルを編集する

    <!DOCTYPE html>
    <html>
    <head>
        <title>Welcome to NGINX</title>
    </head>
    <body>
        <h1>Hello from NGINX running in Docker Compose!</h1>
    </body>
    </html>
  8. コンテナを起動する

    $ docker compose up -d
  9. 14080番ポートにアクセスして、 index.html の内容が表示されたことを確認