0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ubuntuコンテナにNginxを入れて静的サイトを表示する

0
Posted at

概要

ubuntuコンテナ上にnginxを入れて、サイトページを表示できるようにする。

構築環境のイメージ

環境イメージ図.png

DockerFile

FROM ubuntu:latest

# nginxインストールとvim(ファイル編集用エディタ)必要パッケージ
RUN apt-get update && \
  apt-get install -y nginx vim && \
  apt-get clean && \
  rm -rf /var/lib/apt/lists/*

# htmlをコンテナ内にコピー
COPY ./portforio.html /var/www/html/
COPY ./index.html /var/www/html/
# 画像、ファイルを格納するディレクトリを作成
RUN mkdir /var/www/html/JS
RUN mkdir /var/www/html/img
RUN mkdir /var/www/html/data
RUN mkdir /var/www/html/data/CSS
RUN rm /var/www/html/index.nginx-debian.html
# 作成したディレクトリに画像とファイルをコピー(格納)
COPY ./JS/ /var/www/html/JS/
COPY ./img/ /var/www/html/img/
COPY ./data/ /var/www/html/data/
COPY ./CSS/ /var/www/html/CSS/

# nginxポート開放
EXPOSE 80

# nginxをフォアグラウンドで起動(コンテナ起動時)
CMD ["nginx", "-g", "daemon off;"]

1.Ubuntuでapt install nginx したときのnginxのドキュメントルート(root)の場所は
/var/www/htmlなのでこの配下にhtmlファイルなどを格納する

(ドキュメントルートについて)
/etc/nginx/sites-available/defaultファイルに記載されているのでそこで確認する
(例)

root /var/www/html;

2.nginxのポートを解放する(今回は80番ポート)

3.プロセスが終わるとコンテナが閉じてしまうのでnginxを起動したままにしたいのでnginxをフォアグラウンドで起動する(通常はバックグラウンド)

手順

1.DockerFile配下で以下コマンドを実行し、イメージを作成する

docker build -t test . 

イメージ名をtestとして作成(このディレクトリにあるDockerFileを使用して)

2.以下コマンドを使用してコンテナを作成する

docker run -d -p 8080:80 my-nginx test

外(ホスト側)のポート8080 → 中(コンテナ側)のポート80
my-nginx:コンテナ名
test:イメージ名

3.サイトを表示する

http://localhost:8080

2でlocalhost8080にコンテナの80ポートをポートマッピングしているため上記URLでコンテナにアクセスできる

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?