docker composeでnginxコンテナ作成時に「ERROR: for nginx Cannot start service nginx: OCI runtime create failed:」が発生した場合の対処法

docker composeでnginxコンテナ作成時に「ERROR: for nginx  Cannot start service nginx: OCI runtime create failed:」が発生した場合の対処法

docker composeでnginxコンテナ作成時に「ERROR: for nginx Cannot start service nginx: OCI runtime create failed:」が発生した場合の原因と対処法を記述してます。

環境

  • OS MX linux 19.2
  • docker 18.09.1
  • docker compose 1.21.0

エラー全文

以下のdocker-compose.ymlをup時に発生

version: "3"
services:
  php:
    image: php:7.4-fpm
    volumes:
      - ./nginx/html:/usr/share/nginx/html
      - ./php/php.ini:/usr/local/etc/php/conf.d/php.ini

  nginx:
    image: nginx:latest
    volumes:
      - ./nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf
      - ./nginx/html:/usr/share/nginx/html
    restart: always
    ports: ["8080:80"]
    depends_on: ["php"]

エラー全文

container_linux.go:344: starting container process caused "process_linux.go:424                                                                          : container init caused \"rootfs_linux.go:58: mounting \\\"/home/mebee/nginx/con                                                                          f.d/default.conf\\\" to rootfs \\\"/var/lib/docker/overlay2/e7f7d6b0afba5995708f                                                                          cdca7502df6bd4db7bad17289adf5f4303d009193932/merged\\\" at \\\"/var/lib/docker/o                                                                          verlay2/e7f7d6b0afba5995708fcdca7502df6bd4db7bad17289adf5f4303d009193932/merged/                                                                          etc/nginx/conf.d/default.conf\\\" caused \\\"not a directory\\\"\"": unknown: Ar                                                                          e you trying to mount a directory onto a file (or vice-versa)? Check if the spec                                                                          ified host path exists and is the expected type

ERROR: for nginx  Cannot start service nginx: OCI runtime create failed: contain                                                                          er_linux.go:344: starting container process caused "process_linux.go:424: contai                                                                          ner init caused \"rootfs_linux.go:58: mounting \\\"/home/mebee/nginx/conf.d/defa                                                                          ult.conf\\\" to rootfs \\\"/var/lib/docker/overlay2/e7f7d6b0afba5995708fcdca7502                                                                          df6bd4db7bad17289adf5f4303d009193932/merged\\\" at \\\"/var/lib/docker/overlay2/                                                                          e7f7d6b0afba5995708fcdca7502df6bd4db7bad17289adf5f4303d009193932/merged/etc/ngin                                                                          x/conf.d/default.conf\\\" caused \\\"not a directory\\\"\"": unknown: Are you tr                                                                          ying to mount a directory onto a file (or vice-versa)? Check if the specified ho                                                                          st path exists and is the expected type
ERROR: Encountered errors while bringing up the project.

原因

default.confがディレクトリとして作成されるためマウントできなかったことが原因っぽい

対処法

docker-compose.ymlを以下のように修正して、そのままupしてもエラーとなりnginxが利用できないので、

  nginx:
    image: nginx:latest
    volumes:
      - ./nginx/conf.d:/etc/nginx/conf.d
      - ./nginx/html:/usr/share/nginx/html
    restart: always
    ports: ["8080:80"]

「./nginx/conf.d」に「default.conf」を作成する

server {
    listen       80;
    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;
    #}
}