【问题标题】:Docker run fails error ' Extra argument XXX'Docker 运行失败错误“额外参数 XXX”
【发布时间】:2019-06-17 02:56:36
【问题描述】:

我正在尝试运行我的本地 docker 文件/docker compose 设置。

我收到以下错误。

docker-compose.exe up
Starting docker_sshd_1 ... done
Attaching to docker_sshd_1
sshd_1  | Extra argument /usr/sbin/sshd.
docker_sshd_1 exited with code 1

当我查看日志时

docker logs 76d6c9682749
Extra argument /usr/sbin/sshd.

当我尝试启动或运行它时

docker start -ai 76d6c9682749
Extra argument /usr/sbin/sshd.

Docker ps-a 展示

docker ps -a
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS                      PORTS               NAMES
76d6c9682749        ansible-test:latest   "/usr/sbin/sshd -D -…"   17 seconds ago      Exited (1) 15 seconds ago                       docker_sshd_1

docker-compose up --build

docker-compose.exe up --build
Building sshd
Step 1/6 : FROM ubuntu:18.04
---> 1d9c17228a9e
Step 2/6 : RUN apt-get update && apt-get install -y                         net-tools                         netcat                         openssh-server                         curl
---> Using cache
---> a1c69db6f87d
Step 3/6 : RUN mkdir /var/run/sshd &&     echo 'root:ansibletest' | chpasswd &&     echo '\n#allow root\nPermitRootLogin yes\n' >> /etc/ssh/sshd_config
---> Using cache
---> 8af61a1ff284
Step 4/6 : EXPOSE 22
---> Using cache
---> 7483e7b442b4
Step 5/6 : CMD ["/usr/sbin/sshd", "-D" ]
---> Using cache
---> 67634af48cd9
Step 6/6 : ENTRYPOINT ["/usr/sbin/sshd", "-D", "-d"]
---> Running in 304247678be0
Removing intermediate container 304247678be0
---> e8c85c2deea0
Successfully built e8c85c2deea0
Successfully tagged ansible-test:latest
Recreating docker_sshd_1 ... done
Attaching to docker_sshd_1
sshd_1  | Extra argument /usr/sbin/sshd.
docker_sshd_1 exited with code 1

Docker 检查显示以下路径和参数

λ docker inspect --format='{{.Path}}' e8c85c2deea0
'/usr/sbin/sshd'

λ docker inspect --format='{{.Args}}' e8c85c2deea0
'[-D /usr/sbin/sshd -D]'

我的 docker compose 文件是这样的

version: '3'
services:
  sshd:
    build: .
    image: ansible-test:latest
    ports:
    - "2022:22" # bines local port 2022 to container port 22 / sshd
    - "8080:80" # binds local port 8080 to container port 80 / httpd

我的 docker 文件是这样的

# borrowed https://docs.docker.com/engine/examples/running_ssh_service/
FROM ubuntu:18.04

# some useful debuging tools to troubleshoot on these containers
RUN apt-get update && apt-get install -y \
                        net-tools \
                        netcat \
                        openssh-server \
                        curl

# configure sshd to work as we need it in 18.04
# sets the rootpassword to ansibletest
RUN mkdir /var/run/sshd && \
    echo 'root:ansibletest' | chpasswd && \
    echo '\n#allow root\nPermitRootLogin yes\n' >> /etc/ssh/sshd_config

EXPOSE 22

ENTRYPOINT ["/usr/sbin/sshd", "-D" ]
#ENTRYPOINT ["/usr/sbin/sshd", "-D", "-d"]
# for production system remove "-d"
#  -D      When this option is specified, sshd will not detach and does not become a daemon.
#          This allows easy monitoring of sshd.
#  -d      Debug mode.  The server sends verbose debug output to standard error, and does not put itself in the background.
#          The server also will not fork and will only process one connection.
#          This option is only intended for debugging for the server.
#          Multiple -d options increase the debugging level.  Maximum is 3.

# by default start sshd as background daemon
CMD ["/usr/sbin/sshd", "-D" ]
# used for debugging lets you pass options to sshd on startup
#CMD ["/usr/sbin/sshd", "-D", '-d']

【问题讨论】:

    标签: docker docker-compose openssh docker-run


    【解决方案1】:

    经过大量的反复试验。

    上面的 docker inspect 对实际发生的事情给出了最清晰的答案。

    它正确地说有额外的参数

    λ docker inspect --format='{{.Path}}' 1be69f7e6140
    '/usr/sbin/sshd'
    
    λ docker inspect --format='{{.Args}}' 1be69f7e6140
    '[-D /usr/sbin/sshd -D]'
    

    这里它试图执行路径和参数为 '/usr/sbin/sshd' '[-D /usr/sbin/sshd -D]

    我希望它像这样出现。

    λ docker inspect --format='{{.Path}}' cced1543f71e
    '/usr/sbin/sshd'
    
    λ docker inspect --format='{{.Args}}' cced1543f71e
    '[-D]'
    

    它将执行为'/usr/sbin/sshd' '[-D]'

    基本上我误解了ENTRYPOINTCMD 是如何协同工作的。

    来自the docs for CMD

    CMD 指令有三种形式:

    CMD ["executable","param1","param2"](执行形式,这是首选形式)
    CMD ["param1","param2"](作为 ENTRYPOINT 的默认参数)

    我需要使用第二种形式才能正常工作。

    如果我在同一个 docker 文件中使用 ENTRYPOINTCMD。 我不能将executable 作为参数放在CMD 部分中。 它需要只是我想传递给入口点的参数。

    所以我发送了 docker run 的默认值以使用来自 ENTRYPOINT 的 exec 和来自 CMD 的参数,但我可以直接从命令行覆盖它。作为 docker run image-name arg1 agr2

    我的解决方法是

    ENTRYPOINT ["/usr/sbin/sshd"]
    CMD ["-D" ]
    

    这个链接帮助我理解

    https://medium.com/@oprearocks/how-to-properly-override-the-entrypoint-using-docker-run-2e081e5feb9d

    【讨论】:

      猜你喜欢
      • 2021-06-03
      • 1970-01-01
      • 1970-01-01
      • 2018-04-26
      • 1970-01-01
      • 2015-08-23
      • 2019-04-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多