LoginSignup
0
0

More than 1 year has passed since last update.

DockerComposeでCentOS8+Nginxの開発環境を作る

Last updated at Posted at 2021-11-13

最初に手動でDocker+CentOS8+Nginxの環境を構築し、その後で、同じ環境をDockerComposeで構築します。

前提

  • Docker Desktop for Macがインストール済

環境

  • macOS Big Sur
  • Docker version 20.10.8
  • docker-compose version 1.29.2

構築手順

手動でDocker+CentOS8+Nginxの環境を構築

CentOS8イメージを取得、コンテナを作成してバックグラウンドで起動

% docker run -it -d --privileged -p 8080:80 --name="centos8_nginx" centos:centos8 /sbin/init

  

CentOS8のコンテナにログイン

% docker exec -it centos8_nginx /bin/bash

  

CentOS8で初期設定とNginxのインストール、ドキュメントルートの変更

※以降は、ログイン後のCentOS8で作業 

一応、バージョン確認
# cat /etc/redhat-release
CentOS Linux release 8.4.2105
パッケージを最新にする
# dnf -y update
ロケールの設定
# dnf -y install langpacks-ja && \cp /usr/share/zoneinfo/Asia/Tokyo /etc/localtime
# LANG="ja_JP.UTF-8"
# LANGUAGE="ja_JP:ja"
# LC_ALL="ja_JP.UTF-8"
Nginxのインストール
# dnf -y install nginx
which、vimはよく使うのでインストールしておく
# dnf -y install which
# dnf -y install vim
よく使うaliasを設定
# echo 'alias ll="ls -l"' >> ~/.bashrc
# . ~/.bashrc
Nginxのドキュメントルートを/home/htmlに変更

ディレクトリ作成

# mkdir /home/html

以下のindex.htmlを作成する

/home/html/index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>test</title>
</head>
<body>
<p>Hello World</p>
</body>
</html>

nginx.confのserverブロックのrootディレクティブのパスを/home/htmlに変更する

/etc/nginx/nginx.conf
server {
    listen       80 default_server;
    listen       [::]:80 default_server;
    server_name  _;
    # root         /usr/share/nginx/html;
    root         /home/html;

Nginx起動
# systemctl start nginx
確認

ホストOSのブラウザでhttp://localhost:8080/にアクセスし、index.htmlのページが表示されればOK。

コンテナから抜ける
# exit
コンテナ停止
% docker stop centos8-nginx

  
手動での構築はここまで。

コンテナを削除する場合
% docker rm centos8-nginx

  

docker-composeで同じ環境を構築する

ディレクトリ構成

centos8_nginx/
      ├─ docker-compose.yml
      ├─ nginx/
      │  └─ Dockerfile
      │  └─ conf
      │     └─ nginx.conf
      └─ html/
         └─ index.html

各ファイルの内容

docker-compose.yml

version: '3'
services:
    nginx:
        build:
            context: ./nginx
        ports:
            - 8080:80
        volumes:
            - ./html:/home/html
            - ./nginx/conf/nginx.conf:/etc/nginx/nginx.conf

nginx/Dockerfile

FROM centos:8

RUN dnf -y update
RUN dnf -y install langpacks-ja && cp /usr/share/zoneinfo/Asia/Tokyo /etc/localtime

ENV LANG="ja_JP.UTF-8" LANGUAGE="ja_JP:ja" LC_ALL="ja_JP.UTF-8"

RUN dnf -y install nginx
RUN dnf -y install which vim

RUN echo 'alias ll="ls -l"' >> ~/.bashrc

ENTRYPOINT /usr/sbin/nginx -g 'daemon off;' -c /etc/nginx/nginx.conf

nginx/conf/nginx.conf
手動で作ったCentOS8環境の時の/etc/nginx/nginx.conf をコピーする

html/index.html
手動で作ったCentOS8環境の時のhtml/index.htmlをコピーする

起動

% docker-compose up -d

確認

ホストOSのブラウザでhttp://localhost:8080/にアクセスし、index.htmlのページが表示されればOK。
お疲れ様でした。

この後で使いそうなコマンドも書いておきます。

コンテナ一覧表示

% docker-compose ps

コンテナに入る

% docker-compose exec nginx bash

停止してコンテナとイメージ削除

% docker-compose down --rmi all
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