Open7

DockerでNginxのソースコードをビルドして実行する

tommy34tommy34

Dockerfile

FROM ubuntu:24.04

ENV TZ=Asia/Tokyo
ENV PATH $PATH:/usr/local/nginx/sbin
ENV VERSION 1.25.3

RUN ln -snf /usr/share/zoneinfo/${TZ} /etc/localtime && echo ${TZ} > /etc/timezone && \
  sed -i 's@archive.ubuntu.com@ftp.jaist.ac.jp/pub/Linux@g' /etc/apt/sources.list && \
  echo "bind 'set bell-style none'" >> .bashrc && \
  apt-get update && \
  apt-get install --no-install-recommends -y wget build-essential libpcre3-dev zlib1g zlib1g-dev curl cloc && \
  apt-get clean && \
  rm -rf /var/lib/apt/lists/* && \
  wget -q http://nginx.org/download/nginx-${VERSION}.tar.gz && \
  tar xf nginx-${VERSION}.tar.gz

WORKDIR /nginx-${VERSION}

RUN ./configure && \
  make -j"$(nproc)" install
tommy34tommy34

コンテナのビルド

$ docker build -t nginx_build .
[+] Building 140.0s (9/9) FINISHED                                                                                                                                                    docker:desktop-linux
 => [internal] load build definition from Dockerfile                                                                                                                                                  0.0s
 => => transferring dockerfile: 753B                                                                                                                                                                  0.0s
 => [internal] load .dockerignore                                                                                                                                                                     0.1s
 => => transferring context: 2B                                                                                                                                                                       0.0s
 => [internal] load metadata for docker.io/library/ubuntu:24.04                                                                                                                                       3.3s
 => [auth] library/ubuntu:pull token for registry-1.docker.io                                                                                                                                         0.0s
 => [1/4] FROM docker.io/library/ubuntu:24.04@sha256:36fa0c7153804946e17ee951fdeffa6a1c67e5088438e5b90de077de5c600d4c                                                                                20.0s
 => => resolve docker.io/library/ubuntu:24.04@sha256:36fa0c7153804946e17ee951fdeffa6a1c67e5088438e5b90de077de5c600d4c                                                                                 0.0s
 => => sha256:36fa0c7153804946e17ee951fdeffa6a1c67e5088438e5b90de077de5c600d4c 1.13kB / 1.13kB                                                                                                        0.0s
 => => sha256:1548e64a88570f68083471d661806751441e57ce92522dfcd141c3b52f435d42 424B / 424B                                                                                                            0.0s
 => => sha256:786ea2fab1c1fa3103bd97c7142f34b326aa90536f02205acb65861035fbf89b 2.31kB / 2.31kB                                                                                                        0.0s
 => => sha256:b2dd9abd46c42521f99f1bf69948c3e6c15141e3d6f08e1b9b8fb77b478b90df 28.57MB / 28.57MB                                                                                                     18.3s
 => => extracting sha256:b2dd9abd46c42521f99f1bf69948c3e6c15141e3d6f08e1b9b8fb77b478b90df                                                                                                             1.4s
 => [2/4] RUN ln -snf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime && echo Asia/Tokyo > /etc/timezone &&   sed -i 's@archive.ubuntu.com@ftp.jaist.ac.jp/pub/Linux@g' /etc/apt/sources.list &&   e  103.0s
 => [3/4] WORKDIR /nginx-1.25.3                                                                                                                                                                       0.0s
 => [4/4] RUN ./configure &&   make -j"$(nproc)" install                                                                                                                                             11.9s
 => exporting to image                                                                                                                                                                                1.7s
 => => exporting layers                                                                                                                                                                               1.6s
 => => writing image sha256:0decb3a6e75ab7733879ea9851b958d5b3a48850f4599b0397a0f421755c15f4                                                                                                          0.0s
 => => naming to docker.io/library/nginx_build                                                                                                                                                        0.0s

What's Next?
  View a summary of image vulnerabilities and recommendations → docker scout quickview
tommy34tommy34

コンテナ内でNginxが動作することを確認できる

$ docker run -it --rm --name nginx_build_container nginx_build bash
root@cae10b6b6582:/nginx-1.25.3# nginx -V
nginx version: nginx/1.25.3
built by gcc 13.2.0 (Ubuntu 13.2.0-13ubuntu1)
configure arguments:
root@cae10b6b6582:/nginx-1.25.3# nginx -h
nginx version: nginx/1.25.3
Usage: nginx [-?hvVtTq] [-s signal] [-p prefix]
             [-e filename] [-c filename] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -T            : test configuration, dump it and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /usr/local/nginx/)
  -e filename   : set error log file (default: logs/error.log)
  -c filename   : set configuration file (default: conf/nginx.conf)
  -g directives : set global directives out of configuration file

root@cae10b6b6582:/nginx-1.25.3# nginx
root@cae10b6b6582:/nginx-1.25.3# ps afux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0   4204  3456 pts/0    Ss   11:22   0:00 bash
root        21  0.0  0.0   4280  1136 ?        Ss   11:23   0:00 nginx: master process nginx
nobody      22  0.0  0.0   4704  2160 ?        S    11:23   0:00  \_ nginx: worker process
root        23  0.0  0.0   7488  3328 pts/0    R+   11:23   0:00 ps afux
root@cae10b6b6582:/nginx-1.25.3# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
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>
root@cae10b6b6582:/nginx-1.25.3# nginx -s stop
root@cae10b6b6582:/nginx-1.25.3# ps afux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0   4204  3456 pts/0    Ss   11:22   0:00 bash
root        26  0.0  0.0   7488  3328 pts/0    R+   11:24   0:00 ps afux
root@cae10b6b6582:/nginx-1.25.3#
tommy34tommy34
$ docker scout quickview
    ! New version 1.4.1 available (installed version is 1.0.7) at https://github.com/docker/scout-cli
    ✓ Image stored for indexing
    ✓ Indexed 226 packages

  Target     │  local://nginx_build:latest  │    0C     0H     0M     0L
    digest   │  0decb3a6e75a                │
  Base image │  ubuntu:24.04                │    0C     0H     0M     0L

What's Next?
  Include policy results in your quickview by supplying an organization → docker scout quickview local://nginx_build:latest --org <organization>
tommy34tommy34

ソースコードの行数は約17万行

root@ef28df075033:/nginx-1.25.3# cloc .
     501 text files.
     414 unique files.
     210 files ignored.

github.com/AlDanial/cloc v 1.98  T=3.53 s (117.3 files/s, 69008.8 lines/s)
-----------------------------------------------------------------------------------
Language                         files          blank        comment           code
-----------------------------------------------------------------------------------
C                                  257          53980           6143         153757
C/C++ Header                       136           5038           1077          10373
Ruby                                 1           3519              0           5736
vim script                           4             90             74           1831
make                                 3            296              0           1033
Bourne Shell                         2             63              8            163
Perl                                 4             71             51            159
SKILL                                3             24              0             99
HTML                                 2              2              0             40
C++                                  1              9              3             19
Windows Resource File                1              3              2              1
-----------------------------------------------------------------------------------
SUM:                               414          63095           7358         173211
-----------------------------------------------------------------------------------
root@ef28df075033:/nginx-1.25.3#