【Docker】查看Docker运行时的日志~在搭建private-isu环境时遇到的问题~

首先

当我在使用docker-compose进行环境配置时,容器无法成功启动。
为了查找原因,我查看了日志并将当时的命令输出。

總結

查看Docker日志的命令是docker logs [容器名称]。即使要检查未启动的容器日志,也需要提供容器名称(准确地说是启动容器时的名称)。

我看了一下日志。

既然有这个机会,我也将说明一下我使用这个命令的经过。

Background -> 背景

大家好,你们是否了解ISUCON呢?
ISUCON是LINE株式会社举办的一个网页性能调优竞赛。
参赛队伍需要在规定的时间内对一个Web服务进行调优,竞争谁能提升性能最大。

这次我们尝试着利用过去在该比赛中使用过的名为private-isu的源代码来学习WEB性能调优。

启动环境和软件

    • Windows11

 

    • Docker desktop(4.24.1)

 

    • Docker Engine(v24.0.6)

 

    • nginx(1.24)

 

    • mysql(8.0)

 

    memcached(1.6)

尝试使用Docker在容器中启动。

幸运的是,此源码中已经包含了docker-compose.yml文件的定义。因此,如果使用默认设置,您可以通过docker compose up命令启动容器。启动容器后,您可以通过访问localhost来查看网站。

我從默認設置中做了以下的更改。

    • 使用言語をrubyからphpに変更

 

    アクセスログ出力フォーマットを変更

将编程语言从Ruby更改为PHP。

我根据readme的指示对docker-compose.yml进行了修改。
我只是将build的值更改为php/。

  app:
    # Go実装の場合は golang/ PHP実装の場合は php/
    build: php/
    environment:
      ISUCONP_DB_HOST: mysql
      ISUCONP_DB_PORT: 3306
      ISUCONP_DB_USER: root
      ISUCONP_DB_PASSWORD: root
      ISUCONP_DB_NAME: isuconp
      ISUCONP_MEMCACHED_ADDRESS: memcached:11211
    links:
      - mysql
      - memcached
    volumes:
      - ./public:/home/public
    init: true
    deploy:
      resources:
        limits:
          cpus: '1'
          memory: 1g

另外,在使用php时,需要使用专门为php准备的conf文件,而不是nginx的默认conf文件。我会根据readme进行相应修改。

cd webapp/etc
mv nginx/conf.d/default.conf nginx/conf.d/default.conf.org
mv nginx/conf.d/php.conf.org nginx/conf.d/php.conf

更改访问日志输出格式

由于默认的访问日志难以阅读,我改为了使用json格式的日志格式。我将以下内容记录在nginx/conf.d/php.conf中,这是在上述修改中创建的。

server {
    # ログフォーマットの設定
    log_format json escape=json '{'
      '"time_local":"$time_local",'
      '"remote_addr":"$remote_addr",'
      '"request_method":"$request_method",'
      '"request_uri":"$request_uri",'
      '"status":$status,'
      '"body_bytes_sent":$body_bytes_sent,'
      '"http_referer":"$http_referer",'
      '"http_user_agent":"$http_user_agent",'
      '"http_x_forwarded_for":"$http_x_forwarded_for",'
      '"request_time":$request_time,'
      '"upstream_response_time":$upstream_response_time,'
      '"upstream_addr":"$upstream_addr",'
      '"upstream_status":$upstream_status'
    '}';

    # 設定したログフォーマットを使うように指定
    access_log /var/log/nginx/access.log json;

    listen 80;

    client_max_body_size 10m;
    root /public;

    location / {
        try_files $uri /index.php$is_args$args;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php {
        #fastcgi_split_path_info ^(.+\.php)(/.+)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        fastcgi_index index.php;
        fastcgi_pass app:9000;
    }
}

除此之外,我还按照自述文件中的步骤进行了操作。

立即创建容器!

让我们使用 `docker compose up -d` 命令来实际运行。

$ docker compose up -d
[+] Running 5/5
 ✔ Network webapp_default        Created                                                                                                                                                                              0.0s 
 ✔ Container webapp-mysql-1      Started                                                                                                                                                                              0.0s 
 ✔ Container webapp-memcached-1  Started                                                                                                                                                                              0.0s 
 ✔ Container webapp-app-1        Started                                                                                                                                                                              0.0s 
 ✔ Container webapp-nginx-1      Started  

检查完状态后发现,虽然容器看起来已经成功启动了,但是找不到webapp-nginx-1。

$ docker ps
CONTAINER ID   IMAGE           COMMAND                   CREATED              STATUS              PORTS                               NAMES
d6196bc4255d   webapp-app      "docker-php-entrypoi…"   About a minute ago   Up About a minute   9000/tcp                            webapp-app-1
62a9396d09c5   mysql:8.0       "docker-entrypoint.s…"   About a minute ago   Up About a minute   0.0.0.0:3306->3306/tcp, 33060/tcp   webapp-mysql-1
2de9b089c6b1   memcached:1.6   "docker-entrypoint.s…"   About a minute ago   Up About a minute   11211/tcp                           webapp-memcached-1

那么我们看一下日志吧

手順漏れがないかなどしばらく確認していたのですが、一向に解決しません。
そのため、起動時のdockerのログを確認してみることにしました。

$ docker logs webapp-nginx-1
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
nginx: [emerg] "log_format" directive is not allowed here in /etc/nginx/conf.d/php.conf:21

なんかよくわからんけどlog_formatが認識されてないっぽい。
その辺に当たりをつけて調べてみると、「アクセスログ出力フォーマットを変更」の手順で記述した書き方が間違っていたみたいです。

听起来应该是这样的。

# log_formatはserverの外に定義しないとだめらしい。
log_format json escape=json '{'
  '"time_local":"$time_local",'
  '"remote_addr":"$remote_addr",'
  '"request_method":"$request_method",'
  '"request_uri":"$request_uri",'
  '"status":$status,'
  '"body_bytes_sent":$body_bytes_sent,'
  '"http_referer":"$http_referer",'
  '"http_user_agent":"$http_user_agent",'
  '"http_x_forwarded_for":"$http_x_forwarded_for",'
  '"request_time":$request_time,'
  '"upstream_response_time":$upstream_response_time,'
  '"upstream_addr":"$upstream_addr",'
  '"upstream_status":$upstream_status'
'}';

server {

    # ここは変更なし
    access_log /var/log/nginx/access.log json;

    listen 80;

    client_max_body_size 10m;
    root /public;

    location / {
        try_files $uri /index.php$is_args$args;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php {
        #fastcgi_split_path_info ^(.+\.php)(/.+)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        fastcgi_index index.php;
        fastcgi_pass app:9000;
    }
}

再做一次

在进行修正后,再次执行时,可以确认容器已经能够成功启动。

$ docker compose up -d
[+] Running 5/5
 ✔ Network webapp_default        Created   0.0s 
 ✔ Container webapp-memcached-1  Started   0.0s 
 ✔ Container webapp-mysql-1      Started   0.1s 
 ✔ Container webapp-app-1        Started   0.0s 
 ✔ Container webapp-nginx-1      Started   0.0s 
$
$ docker ps      
CONTAINER ID   IMAGE           COMMAND                   CREATED         STATUS         PORTS                               NAMES
4355c5e62cbe   nginx:1.24      "/docker-entrypoint.…"   8 seconds ago   Up 6 seconds   0.0.0.0:80->80/tcp                  webapp-nginx-1
9e58a7987b4e   webapp-app      "docker-php-entrypoi…"   8 seconds ago   Up 7 seconds   9000/tcp                            webapp-app-1
5cd407caa6c3   mysql:8.0       "docker-entrypoint.s…"   8 seconds ago   Up 7 seconds   0.0.0.0:3306->3306/tcp, 33060/tcp   webapp-mysql-1
91d75ff910e1   memcached:1.6   "docker-entrypoint.s…"   8 seconds ago   Up 7 seconds   11211/tcp                           webapp-memcached-1
PS C:\Users\rogto\workspace\isucon\private-isu\webapp> 
スクリーンショット 2023-10-06 202405.png

最后

今回はISUCONのソースを用いてdockerのログの確認方法を学びました。
環境構築が済んだので、WEBパフォーマンスチューニングを勉強していきます。
またアウトプットします。

可以参考

    • private-isu

 

    • Docker環境構築で困ったらログを見る!

 

    達人が教えるWebパフォーマンスチューニング〜ISUCONから学ぶ高速化の実践
广告
将在 10 秒后关闭
bannerAds