LoginSignup
26
32

More than 5 years have passed since last update.

Dockerを使ってnginxのWebサーバを構築する

Posted at

Dockerを利用してnginxのWebサーバを構築してみる

さて、今回はDockerを利用してnginxをためしてみます。
はじめてのDockerはこちらをご参照ください。

Dockerをはじめからていねいに

pullする

まずはDockerイメージをおとしてきます。
今回はubuntuを使います。

$ docker pull docker.io/ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
124c757242f8: Pull complete
9d866f8bde2a: Pull complete
fa3f2f277e67: Pull complete
398d32b153e8: Pull complete
afde35469481: Pull complete
Digest: sha256:de774a3145f7ca4f0bd144c7d4ffb2931e06634f11529653b23eba85aef8e378
Status: Downloaded newer image for ubuntu:latest

dockerを起動する

次に、docker起動です。

$ docker run -td --name ubuntu-latest docker.io/ubuntu:latest
ea7e5b555f66cde78126d227f6fe62db8dfd19aef827d112370549a14e44bc3b

nginxコンテナを起動する

nginxコンテナを起動してみます。

$ docker pull docker.io/nginx
Using default tag: latest
latest: Pulling from library/nginx
802b00ed6f79: Pull complete
c16436dbc224: Pull complete
683eac851b28: Pull complete
Digest: sha256:e8ab8d42e0c34c104ac60b43ba60b19af08e19a0e6d50396bdfd4cef0347ba83
Status: Downloaded newer image for nginx:latest

$ docker run -d -p 8000:80 --name nginx-latest docker.io/nginx:latest
e3a624000ac66f2172d0ac936aa396906ca276a5a0ee9dbf57dc7836ac33fe67

今回は-pオプションを使っています。

p-:ポートフォワードを行う

Dockerホストマシンの8000番ポートが、nginx-latestコンテナの80番ポートにつながっていることになります。

psでみるとこんな感じ。

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                  NAMES
e3a624000ac6        nginx:latest        "nginx -g 'daemon of…"   About a minute ago   Up About a minute   0.0.0.0:8000->80/tcp   nginx-latest
ea7e5b555f66        ubuntu:latest       "/bin/bash"              5 minutes ago        Up 5 minutes                               ubuntu-latest

PORTSのカラムでポートフォワードの設定が確認できますね。

実際にnginxコンテナにアクセス確認してみる

実際にアクセスして確認してみましょう。

$ curl http://localhost:8000/index.html
<!DOCTYPE html>
<html>
...
...

index.htmlの結果がかえってくればOKです!

nginxログの確認

nginxのログも確認することができます。

$ docker logs -f nginx-latest
172.17.0.1 - - [29/Sep/2018:07:24:52 +0000] "GET /index.html HTTP/1.1" 200 612 "-" "curl/7.53.1" "-"

-fをつけることでtail- fのような見方ができます。

以上でnginxのWebサーバが構築できました。

OSインスタンスから構築した場合、OSをインストールして、nginxをインストールして、起動してと多くのステップ・時間がかかりますが、Dockerイメージを利用することでクイックに構築できますね。

26
32
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
26
32