Docker因配置错误无法启动容器时该咋整

1. 借鉴

【总结】Docker无法启动容器时如何修改配置

2. 开始

在docker中因为配置错误,导致容器一直无法启动,使用docker logs -f containerId发现报错但是只能隔岸观火噶咋整。

【缘起】

nginx因为配置问题导致无法启动,docker ps查看不到进程号
我们使用docker logs查看一下日志

docker logs -f nginx01

172.17.0.1 - - [02/Dec/2019:09:47:29 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36" "-"
172.17.0.1 - - [02/Dec/2019:09:47:29 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36" "-"
172.17.0.1 - - [02/Dec/2019:09:47:29 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36" "-"
172.17.0.1 - - [02/Dec/2019:09:47:29 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36" "-"
2019/12/02 09:49:45 [error] 6#6: *6 open() "/usr/share/nginx/html/aaa" failed (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET /aaa HTTP/1.1", host: "localhost:8080"
172.17.0.1 - - [02/Dec/2019:09:49:45 +0000] "GET /aaa HTTP/1.1" 404 555 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36" "-"
2019/12/02 09:50:08 [error] 6#6: *6 open() "/usr/share/nginx/html/50x" failed (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET /50x HTTP/1.1", host: "localhost:8080"
172.17.0.1 - - [02/Dec/2019:09:50:08 +0000] "GET /50x HTTP/1.1" 404 555 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36" "-"
172.17.0.1 - - [04/Dec/2019:07:00:31 +0000] "GET /swagger/index.html HTTP/1.1" 404 555 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36" "-"
2019/12/04 07:00:31 [error] 6#6: *12 open() "/usr/share/nginx/html/swagger/index.html" failed (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET /swagger/index.html HTTP/1.1", host: "localhost:8080"
2020/07/24 15:17:12 [emerg] 1#1: invalid number of arguments in "use" directive in /etc/nginx/nginx.conf:11
nginx: [emerg] invalid number of arguments in "use" directive in /etc/nginx/nginx.conf:11
2020/07/24 15:17:45 [emerg] 1#1: invalid number of arguments in "use" directive in /etc/nginx/nginx.conf:11
nginx: [emerg] invalid number of arguments in "use" directive in /etc/nginx/nginx.conf:11

由此我们可以看到是因为/etc/nginx/nginx.conf这个文件的11行有问题导致的。

【解决】

  1. 如果配置了路径映射,直接修改对应的文件,重启即可。
  2. 如果没有设置路径映射,将容器中的文件复制到主机,修复之后覆盖回去即可。

我的这个问题是属于第二类,所以依次执行以下步骤:
① 将/etc/nginx/nginx.conf配置文件cp到主机

docker cp nginx01:/etc/nginx/nginx.conf /Users/xxx/Desktop/

② 修改配置文件

③ 将配置文件写回容器

docker cp /Users/xxx/Desktop/nginx.conf nginx01:/etc/nginx/nginx.conf

④ 启动容器

docker start nginx01

⑤ 查看进程号

 CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
 27b898aa3e89        nginx               "nginx -g 'daemon of…"   7 months ago        Up 5 seconds        0.0.0.0:8080->80/tcp   nginx01

3. 大功告成

你可能感兴趣的:(Docker因配置错误无法启动容器时该咋整)