CentOS7 - Dockerイメージを作る(Dockerfile)

環境

OS: CentOS Linux 7 (Core)
VCPU: 2
MEM: 2GB
Docker API Ver: 1.29

Dockerのインストールがまだの場合は、下記を参考にインストール。
DockerをCentOS7に30分でインストールする

手順

Dockerfileを作成
Dockerfileをビルドしてイメージ作成
Dockerイメージからコンテナ起動
DockerコンテナのNginxにアクセス

Dockerfileを作成

1.「Dockerfile」と「hello.html」を作成します。
下記では、CentOS7のDockerイメージを元にNginxをインストールしてプロセス起動する内容を記述しています。

# vim Dockerfile
FROM centos:centos7
RUN yum update -y && \
    yum install -y epel-release && \
    yum upgrade -y && \
    yum install -y nginx

ADD hello.html /usr/share/nginx/html/

EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

2.HTMLファイルを作成

# echo 'Hello!!' > hello.html

3.ディレクトリ構造を確認

# tree
.
|-- Dockerfile
`-- hello.html

Dockerfileをビルドしてイメージ作成

# docker build -t nginx:centos7 .

Dockerイメージからコンテナ起動

1.イメージの一覧を取得

# docker images
REPOSITORY                                      TAG                 IMAGE ID            CREATED             SIZE
nginx                                           centos7             e9c983020f72        6 seconds ago       400MB

2.Dockerコンテナを起動

# docker run -t -p 80:80 -d nginx:centos7
11fb0be9e4360e5092754b85ba4d1301a3210b5627dc54163596df21d3cc1341

3.Dockerコンテナの状態を確認

# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
11fb0be9e436        nginx:centos7       "nginx -g 'daemon ..."   5 seconds ago       Up 3 seconds        0.0.0.0:80->80/tcp

DockerコンテナのNginxにアクセス

CurlコマンドでNginxにアクセスしてHello!!と表示があれば終わり。

# curl -s 127.0.0.1/hello.html
Hello!!