node-healthcheck用 DockerImage 作成

# はじめに

KubernetesのオンプレミスPod公開で使用している、DockerImageを作成する手順の記録します。

該当の記事は以下
https://qiita.com/sugimount/items/d7f884ad08d761714556

DockerHubで公開されている Official の Nginx イメージ (alpine base) を、改変します。
Nginxアプリケーション自体が80ポートを公開しているので、何でも良いのですが29499ポートで公開するように変更します。



#  DockerHubでRepository作成

以下作業を実施

- アカウント作成
- Repository作成 (name : node-healthcheck)



# Imageを作成

作業ディレクトリ作成

```
mkdir ~/myimages
cd ~/myimages
```



nginx:1.15.2 をBaseImageにして、公開Portを変更する、DockerFileを作成します

```
cat <<'EOF' > ~/myimages/Dockerfile
FROM nginx:1.15.2
MAINTAINER sugimount <https://twitter.com/sugimount>
COPY default.conf /etc/nginx/conf.d/default.conf

EXPOSE 29499
EOF
```



nginx のdefault port を変更するためのファイルを作成します

```
cat <<'EOF' > ~/myimages/default.conf
server {
    #listen       80;
    listen       29499;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}
EOF
```





DockerFileから作成する

```
cd /root/myimages
docker build --tag=sugimount/node-healthcheck:0.0.1 .
```



実行例

```
[root@calico-k8s-master01 myimages(demo1 kubernetes-admin)]# docker build --tag=sugimount/node-healthcheck:0.0.1 .
Sending build context to Docker daemon 4.096 kB
Step 1/4 : FROM nginx:1.15.2
 ---> c82521676580
Step 2/4 : MAINTAINER sugimount <https://twitter.com/sugimount>
 ---> Running in dda66ff3ba3c
 ---> a7d06c6496cf
Removing intermediate container dda66ff3ba3c
Step 3/4 : COPY default.conf /etc/nginx/conf.d/default.conf
 ---> 2b87925388e7
Removing intermediate container 1bac74b639d6
Step 4/4 : EXPOSE 29499
 ---> Running in 26df4bee5eb7
 ---> 6fa95df8a395
Removing intermediate container 26df4bee5eb7
Successfully built 6fa95df8a395
```



確認

```
[root@calico-k8s-master01 myimages(demo1 kubernetes-admin)]# docker image ls
REPOSITORY                                 TAG                 IMAGE ID            CREATED             SIZE
sugimount/node-healthcheck                 0.0.1               6fa95df8a395        14 seconds ago      109 MB
docker.io/nginx                            1.15.2              c82521676580        4 days ago          109 MB
```



# DockerRunでコンテナ起動

以下コマンドで、ローカルのイメージから起動します

```
docker run -p 29499:29499 --rm -it sugimount/node-healthcheck:0.0.1 /bin/bash
```



ファイルを確認します

```
root@a27fb176d34f:/etc/nginx/conf.d# cat /etc/nginx/conf.d/default.conf     
server {
    #listen       80;
    listen       29499;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}
```



コンテナ内部でNginxを起動します

```
nginx
```



Host側からCurlでアクセスします

```
[root@calico-k8s-master01 ~(demo1 kubernetes-admin)]# curl http://localhost:29499
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
```



コンテナ内部から抜けます

```
exit
```



# DockerHub へ Push

DockerHub へ Login

```
# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: sugimount
Password: 
Login Succeeded
```



作成したImageをPushします

```
docker push sugimount/node-healthcheck:0.0.1
```







# 参考URL

Nginx Image

https://hub.docker.com/_/nginx/



DockerFile

http://docs.docker.jp/engine/userguide/eng-image/dockerfile_best-practice.html

https://qiita.com/tanan/items/e79a5dc1b54ca830ac21#from

https://gendosu.jp/archives/2838

https://qiita.com/pottava/items/452bf80e334bc1fee69a



DockerHub Push

https://qiita.com/moru3/items/32931813db81d891effb