Docker和php“ standard_init_linux.go:207:exec用户进程导致“ exec格式错误”。入口点也无法读取exec的参数

时间:2019-06-14 12:37:39

标签: php docker docker-compose dockerfile

我有以下Dockerfile

FROM php:7.2-fpm-alpine

ARG USERID=1000
ARG GROUPID=1000

ENV DOCKER_UID=${USERID} \
    DOCKER_GID=${GROUPID} \
    PHP_CONF_DIR="/usr/local/etc/php/conf.d" \
    XDEBUG_CONF_FILE=${PHP_CONF_DIR}/docker-php-ext-xdebug.ini \
    XDEBUG_HOST="" \
    XDEBUG_IDE_KEY="" \
    XDEBUG_PORT=9000 \
    XDEBUG_DBGP=FALSE



RUN echo "Installing Development Dpendencies \n" &&\
    echo http://dl-2.alpinelinux.org/alpine/edge/community/ >> /etc/apk/repositories &&\
    apk add --update --virtual build-dependencies build-base gcc wget autoconf &&\
    apk add --update bash shadow git bash-completion bash-doc &&\
    echo "Installing composer \n" &&\
    php -r "copy('https://getcomposer.org/installer', '/tmp/composer-setup.php');" &&\
    php -r "if (hash_file('sha384', '/tmp/composer-setup.php') === '48e3236262b34d30969dca3c37281b3b4bbe3221bda826ac6a9a62d6444cdb0dcd0615698a5cbe587c3f0fe57a54d8f5') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" &&\
    php /tmp/composer-setup.php --install-dir=/bin --filename=composer &&\
    rm -rf /tmp/cpmposer-setup.php &&\
    chmod +x /bin/composer &&\
    echo "Installing xdebug" &&\
    pecl install xdebug  &&\
    docker-php-ext-enable xdebug &&\
    echo "Configuring Xdebug \n" &&\
    echo "xdebug.remote_enable=1" >> ${XDEBUG_CONF_FILE} &&\
    echo "xdebug.max_nesting_level = 1000" >> ${XDEBUG_CONF_FILE} &&\
    echo "xdebug.remote_mode=req" >> ${XDEBUG_CONF_FILE} &&\
    echo "xdebug.remote_log=xdebug.log" >> ${XDEBUG_CONF_FILE} &&\
    cp ${XDEBUG_CONF_FILE} ${XDEBUG_CONF_FILE}.orig &&\
    echo "Making a Development User \n" &&\
    addgroup -g ${DOCKER_GID} developer &&\
    mkdir -p /home/developer/code &&\
    adduser -D -u ${DOCKER_UID} -G developer -h /home/developer -s /bin/bash developer &&\
    chown developer:developer -R /home/developer/code &&\
    apk del build-dependencies &&\
    rm -rf /var/cache/apk/*

# I make seperate layer because Entrypoint is most likely to change
COPY ./entrypoint/develop_entrypoint.sh /usr/local/bin/entrypoint.sh

RUN echo "Fixing Permissions on Entrypoint Script \n" &&\
    chown root:root /usr/local/bin/entrypoint.sh &&\
    chmod +x /usr/local/bin/entrypoint.sh &&\
    echo "Fixing Permissions on Entrypoint Script \n" &&\
    chown root:root /usr/local/bin/entrypoint.sh &&\
    chmod +x /usr/local/bin/entrypoint.sh

VOLUME /home/developer/code
WORKDIR /home/developer/code

ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
CMD ["php-fpm"]

然后我使用以下docker-compose.yml来启动它:

version: '3'
services:
  develop:
    build:
      context: .
      dockerfile: ./dockerfiles/Dockerfile_base
    image: myapp/php:dev-n-build
    volumes:
       - ${CODE_BASE_PATH}:/home/developer/code

  nginx:
    image: nginx:alpine
    ports:
      - 7080:7080
    links:
      - develop
    volumes:
      - ${CODE_BASE_PATH}:/home/developer/code

但是由于某些原因,我收到以下错误消息:

Creating network "dist_default" with the default driver
Creating dist_develop_1 ... done
Creating dist_nginx_1   ... done
Attaching to dist_develop_1, dist_nginx_1
develop_1  | standard_init_linux.go:207: exec user process caused "exec format error"
dist_develop_1 exited with code 1

我的Docker版本是:

$ docker -v
Docker version 18.09.6, build 481bc77

docker-compose版本是:

docker-compose version 1.24.0, build 0aa5906

我的entrypoint.sh也给我们以下内容:

#/bin/sh

USER_ID=$(id -u developer)
GROUP_ID=$(id -g developer)

echo "Setting the correct user and group id for shell use"
if [ ${DOCKER_UID} != ${USER_ID} ]; then
  usermod -u ${DOCKER_UID} developer
fi

if [ ${DOCKER_GID} != ${GROUP_ID} ]; then
  groupmod -g ${DOCKER_GID} developer
fi

echo "Setup xdebug"
cp ${XDEBUG_CONF_FILE}.orig ${XDEBUG_CONF_FILE}

if [ -z ${XDEBUG_HOST} ]; then
    ip=$(netstat -rn | grep "^0.0.0.0 " | cut -d " " -f10)
    XDEBUG_HOST=${ip}
fi

echo "xdebug.remote_host=${XDEBUG_HOST}" >> ${XDEBUG_CONF_FILE}
echo "xdebug.repomote_port=${XDEBUG_PORT}" >> ${XDEBUG_CONF_FILE}

if [ XDEBUG_DBGP = TRUE ]; then
    echo "xdebug.remote.handler=dbgp" >> ${XDEBUG_CONF_FILE}
fi

if [ ! -z "${XDEBUG_IDE_KEY}" ]; then
    echo "xdebug.idekey=\"${XDEBUG_IDE_KEY}\"" >> ${XDEBUG_CONF_FILE}
fi

echo "Launch application"
exec "$@"

所以我想知道:

  • standard_init_linux.go:207: exec user process caused "exec format error"表示什么。
  • 我如何解决此问题。

编辑1

我设法通过在ENTRYPOINT的{​​{1}}中生成外壳来修复它:

Dockerfile

我还将错别字固定在Entrypoint脚本中:

CMD ["/bin/sh","-c","/usr/local/bin/entrypoint.sh", "php-fpm"]

但是由于某些尴尬的原因, #!/bin/sh USER_ID=$(id -u developer) GROUP_ID=$(id -g developer) echo "Setting the correct user and group id for shell use" if [ ${DOCKER_UID} != ${USER_ID} ]; then usermod -u ${DOCKER_UID} developer fi if [ ${DOCKER_GID} != ${GROUP_ID} ]; then groupmod -g ${DOCKER_GID} developer fi echo "Setup xdebug" cp ${XDEBUG_CONF_FILE}.orig ${XDEBUG_CONF_FILE} if [ -z ${XDEBUG_HOST} ]; then ip=$(netstat -rn | grep "^0.0.0.0 " | cut -d " " -f10) XDEBUG_HOST=${ip} fi echo "xdebug.remote_host=${XDEBUG_HOST}" >> ${XDEBUG_CONF_FILE} echo "xdebug.repomote_port=${XDEBUG_PORT}" >> ${XDEBUG_CONF_FILE} if [ XDEBUG_DBGP = TRUE ]; then echo "xdebug.remote.handler=dbgp" >> ${XDEBUG_CONF_FILE} fi if [ ! -z "${XDEBUG_IDE_KEY}" ]; then echo "xdebug.idekey=\"${XDEBUG_IDE_KEY}\"" >> ${XDEBUG_CONF_FILE} fi echo "Launch application" exec "$@" 没有启动。你知道为什么吗?

如果我将php-fpm的最后一行从以下位置更改:

entrypoint.sh

进入

exec "$@"

我得到以下输出:

echo "Params $@"

exec "$@"

较早的尝试,例如:https://github.com/ellakcy/docker-moodle/blob/master/dockerfiles/fpm_alpine/Dockerfile_mysql似乎可以使入口点发挥作用:https://github.com/ellakcy/docker-moodle/blob/master/scripts/entrypoint.sh

Docker是否发生了某些变化以及入口点的处理方式?

甚至可以这样做:

develop_1  | Setting the correct user and group id for shell use
develop_1  | Setup xdebug
develop_1  | Launch application
develop_1  | Params 
nginx_1    | 2019/06/14 14:00:04 [emerg] 1#1: host not found in upstream "develop" in /etc/nginx/nginx.conf:32
nginx_1    | nginx: [emerg] host not found in upstream "develop" in /etc/nginx/nginx.conf:32
php-docker_base_1 exited with code 0
php-docker_develop_1 exited with code 0
php-docker_nginx_1 exited with code 1

也不起作用。

2 个答案:

答案 0 :(得分:1)

您的entrypoint.sh中有一个错字

#/bin/sh => #!/bin/sh

答案 1 :(得分:0)

最好的方法是从exec格式转换为shell格式以启动入口点:

ENTRYPOINT /usr/local/bin/entrypoint.sh php-fpm

还从中删除了最后一个CMD,因为您可以看到ENTRYPOINT DIRECTLY 直接返回所需的参数。您的情况是传递要执行的命令,即php-fpm

正如您所看到的,正如@Ntwobike指出的那样,可以解决打字错误。

结果Dockerfile将是:

FROM php:7.2-fpm-alpine

ARG USERID=1000
ARG GROUPID=1000

ENV DOCKER_UID=${USERID} \
    DOCKER_GID=${GROUPID} \
    PHP_CONF_DIR="/usr/local/etc/php/conf.d" \
    XDEBUG_CONF_FILE=${PHP_CONF_DIR}/docker-php-ext-xdebug.ini \
    XDEBUG_HOST="" \
    XDEBUG_IDE_KEY="" \
    XDEBUG_PORT=9000 \
    XDEBUG_DBGP=FALSE



RUN echo "Installing Development Dpendencies \n" &&\
    echo http://dl-2.alpinelinux.org/alpine/edge/community/ >> /etc/apk/repositories &&\
    apk add --update --virtual build-dependencies build-base gcc wget autoconf &&\
    apk add --update bash shadow git bash-completion bash-doc &&\
    echo "Installing composer \n" &&\
    php -r "copy('https://getcomposer.org/installer', '/tmp/composer-setup.php');" &&\
    php -r "if (hash_file('sha384', '/tmp/composer-setup.php') === '48e3236262b34d30969dca3c37281b3b4bbe3221bda826ac6a9a62d6444cdb0dcd0615698a5cbe587c3f0fe57a54d8f5') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" &&\
    php /tmp/composer-setup.php --install-dir=/bin --filename=composer &&\
    rm -rf /tmp/cpmposer-setup.php &&\
    chmod +x /bin/composer &&\
    echo "Installing xdebug" &&\
    pecl install xdebug  &&\
    docker-php-ext-enable xdebug &&\
    echo "Configuring Xdebug \n" &&\
    echo "xdebug.remote_enable=1" >> ${XDEBUG_CONF_FILE} &&\
    echo "xdebug.max_nesting_level = 1000" >> ${XDEBUG_CONF_FILE} &&\
    echo "xdebug.remote_mode=req" >> ${XDEBUG_CONF_FILE} &&\
    echo "xdebug.remote_log=xdebug.log" >> ${XDEBUG_CONF_FILE} &&\
    cp ${XDEBUG_CONF_FILE} ${XDEBUG_CONF_FILE}.orig &&\
    echo "Making a Development User \n" &&\
    addgroup -g ${DOCKER_GID} developer &&\
    mkdir -p /home/developer/code &&\
    adduser -D -u ${DOCKER_UID} -G developer -h /home/developer -s /bin/bash developer &&\
    chown developer:developer -R /home/developer/code &&\
    apk del build-dependencies &&\
    rm -rf /var/cache/apk/*

# I make seperate layer because Entrypoint is most likely to change
COPY ./entrypoint/develop_entrypoint.sh /usr/local/bin/entrypoint.sh

RUN echo "Fixing Permissions on Entrypoint Script \n" &&\
    chown root:root /usr/local/bin/entrypoint.sh &&\
    chmod +x /usr/local/bin/entrypoint.sh &&\
    echo "Fixing Permissions on Entrypoint Script \n" &&\
    chown root:root /usr/local/bin/entrypoint.sh &&\
    chmod +x /usr/local/bin/entrypoint.sh

VOLUME /home/developer/code
WORKDIR /home/developer/code

ENTRYPOINT /usr/local/bin/entrypoint.sh php-fpm

并专注于解决其余的98个问题(您有99个问题))。

PS 还是IDK为什么会发生这种情况。