Docker入門

Dockerの開発環境をローカル環境に構築する手順や、 Docker を使ったプログラムの記述方法や実行までをサンプルを使いながら順に学習していきます。

docker-composeでnginxサーバーを起動する【3秒で完了】

立ち上げ手順

ソースコード入手

git clone https://github.com/minegishirei/DockerImages.git

ディレクトリ移動

cd DockerImages/nginx

コンテナ立ち上げ

docker-compose up

http://localhost/index.html

にアクセス。

以上。終わり。

解説

docker-comopse.ymlファイル

version: "3"

services:
  nginx:
    container_name: nginx
    build:
      ./nginx
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx/html:/usr/share/nginx/html
      - ./nginx/conf.d:/etc/nginx/conf.d
  • portsについて

portは80番と443番を開けている。 これはそれぞれHTTPとHTTPSで使用するポート番号に対応しており、 これがなければブラウザからアクセスすることができない。

  • volumesについて

nginxで必要なフォルダーは主に二つ。

  • htmlフォルダー。ここにはウェブサイトに表示されるhtmlを格納する。
  • conf.dフォルダー。ここにはnginxで使用する設定ファイルを格納する。

htmlにはindex.htmlのみを入れている。

conf.dにはdefault.confファイルを入れており、この中にnginxの設定内容を書き込んでいる。

nginx/conf.d/default.confについて

この設定ファイルには次の内容を書き込んでいる。

server {
    listen       80;
    server_name  localhost;
    location / {
        # ゲストOS上のWebアプリ(index.html等)カレントディレクトリ
        root /usr/share/nginx/html;
        index  index.html index.htm;
    }
}