码头工人:码头工人exec-它在Dockerfile或YML内部吗 [英] Docker: docker exec -it inside the Dockerfile or YML?

查看:60
本文介绍了码头工人:码头工人exec-它在Dockerfile或YML内部吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是docker的新手,试图了解如何执行PHP脚本.我创建了一个Ubuntu容器,并使用以下示例指示其执行PHP脚本:

  RUN/bin/bash -c设置-x/usr/bin/php7.1 ./x_test/php_version.php"CMD ["php"]" ./x_test/php_version.php"] 

执行脚本后,日志或终端中均未显示任何内容.

但是当我在执行"attach shell"之后执行完全相同的操作时,( docker exec -it )一切正常,并产生输出.

我在作曲家中尝试了 command:tail -F any ,并向命令中添加了各种标志,例如 set -x ,但是仍然没有任何反应,并且日志中也没有任何内容.

是否可以将docker exec it命令放入Dockerfile中?

当我在Java应用程序中使用类似类型的命令时,会得到输出等,但在PHP中却没有.

这是Dockerfile

 从FROM开始添加ubuntu-xenial-core-cloudimg-amd64-root.tar.gz/添加selenium-server-standalone-3.8.1.jar/#一些特定于Docker的小调整#参见https://github.com/docker/docker/blob/9a9fc01af8fb5d98b8eec0740716226fadb3735c/contrib/mkimage/debootstrap运行设置-xe \\#https://github.com/docker/docker/blob/9a9fc01af8fb5d98b8eec0740716226fadb3735c/contrib/mkimage/debootstrap#L40-L48&&回声'#!/bin/sh'>/usr/sbin/policy-rc.d \&&回声'退出101'>/usr/sbin/policy-rc.d \&&chmod + x/usr/sbin/policy-rc.d \\#https://github.com/docker/docker/blob/9a9fc01af8fb5d98b8eec0740716226fadb3735c/contrib/mkimage/debootstrap#L54-L56&&dpkg-divert --local --rename --add/sbin/initctl \&&cp -a/usr/sbin/policy-rc.d/sbin/initctl \&&sed -i's/^ exit.*/exit 0/'/sbin/initctl \\#https://github.com/docker/docker/blob/9a9fc01af8fb5d98b8eec0740716226fadb3735c/contrib/mkimage/debootstrap#L71-L78&&回声'force-unsafe-io'>/etc/dpkg/dpkg.cfg.d/docker-apt-speedup \\#https://github.com/docker/docker/blob/9a9fc01af8fb5d98b8eec0740716226fadb3735c/contrib/mkimage/debootstrap#L85-L105&&echo'DPkg :: Post-Invoke {'rm -f/var/cache/apt/archives/*.deb/var/cache/apt/archives/partial/*.deb/var/cache/apt/*.bin||真实";};'>/etc/apt/apt.conf.d/docker-clean \&&echo'APT :: Update :: Post-Invoke {'rm -f/var/cache/apt/archives/*.deb/var/cache/apt/archives/partial/*.deb/var/cache/apt/* .bin ||真实";};'>>/etc/apt/apt.conf.d/docker-clean \&&echo'Dir :: Cache :: pkgcache";Dir :: Cache :: srcpkgcache;";'>>/etc/apt/apt.conf.d/docker-clean \\#https://github.com/docker/docker/blob/9a9fc01af8fb5d98b8eec0740716226fadb3735c/contrib/mkimage/debootstrap#L109-L115&&echo'Acquire :: Language'none';'>/etc/apt/apt.conf.d/docker-no-languages \\#https://github.com/docker/docker/blob/9a9fc01af8fb5d98b8eec0740716226fadb3735c/contrib/mkimage/debootstrap#L118-L130&&回声'Acquire :: GzipIndexes'true';Acquire :: CompressionTypes :: Order ::"gz";'>/etc/apt/apt.conf.d/docker-gzip-indexes \\#https://github.com/docker/docker/blob/9a9fc01af8fb5d98b8eec0740716226fadb3735c/contrib/mkimage/debootstrap#L134-L151&&echo'Apt :: AutoRemove :: Suggests重要的"false";">/etc/apt/apt.conf.d/docker-autoremove-suggests#删除所有apt列表文件,因为它们很大并且很快就过时了运行rm -rf/var/lib/apt/lists/*#这会强制"apt-get update";在依赖的图像中,这也很好#(另请参阅https://bugs.launchpad.net/cloud-images/+bug/1699913)#使systemd-detect-virt返回"docker"#参见:https://github.com/systemd/systemd/blob/aa0c34279ee40bce2f9681b496922dedbadfca19/src/basic/virt.c#L434运行mkdir -p/run/systemd&&回声'泊坞窗'>/运行/系统/容器#安装更新运行apt-get update -y&&apt-get install -y软件属性-通用语言包-en-base#安装Java运行apt-get install -y default-jre#获取PHP库运行LC_ALL = en_US.UTF-8 add-apt-repository ppa:ondrej/php运行apt-get -y update&&apt-get install -y \php7.1 \php7.1-pgsql \php-梨\php7.1-curl \php7.1-sqlite3 \php7.1-xml \php7.1-bcmath \php7.1-zip \php7.1-mbstring \php-xdebug \php-ast运行mkdir/appWORKDIR/app运行/bin/bash -c设置-x/usr/bin/php7.1 ./x_test/php_version.php"CMD ["php"]" ./x_test/php_version.php"] 

解决方案

完全是新手错误..终端窗口不显示脚本返回的输出.为此,您需要其他日志记录命令.

我正在VS Code中执行以下操作:

  docker组成 

^这等效于在终端中执行 docker-compose up -d --build

此文件以分离模式运行,显然不会从容器中输出日志.

所以我需要执行以下操作来查看容器中的日志

  docker容器查看日志 

^这等效于在终端中执行 docker logs --tail

然后,我看到了它返回的所有错误.原来我没有以正确的格式执行命令.

简单的解决方法是将所有命令写入bash脚本,然后使用以下

Dockerfile 的末尾调用该脚本

  CMD ["./start.sh"] 

start.sh看起来像

 #!/usr/bin/env bash设置-ePHP脚本1.phpPHP脚本2.php 

P.S:我希望VS代码中的docker扩展具有类似"docker compose up with logs"的命令.或其他东西,以便您可以立即查看容器内部正在发生什么.

I'm new to docker and trying to understand how executing a PHP script works. I created an Ubuntu container and instructed it to execute a PHP script using examples like:

RUN /bin/bash -c "set -x /usr/bin/php7.1 ./x_test/php_version.php"
CMD ["php" "./x_test/php_version.php"]

After executing the script nothing is shown in the logs or in the terminal.

But when I do the exact same thing after doing "attach shell" (docker exec -it) everything works fine and it produces output.

I tried command: tail -F anything in composer and adding various flags to the command like set -x but still nothing is happening and nothing is in the logs.

Is there a way to put the docker exec it command inside the Dockerfile?

When I use a similar type of command with a Java app, I get output etc but not with PHP.

Here is the Dockerfile

FROM scratch
ADD ubuntu-xenial-core-cloudimg-amd64-root.tar.gz /
ADD selenium-server-standalone-3.8.1.jar /

# a few minor docker-specific tweaks
# see https://github.com/docker/docker/blob/9a9fc01af8fb5d98b8eec0740716226fadb3735c/contrib/mkimage/debootstrap
RUN set -xe \
    \
# https://github.com/docker/docker/blob/9a9fc01af8fb5d98b8eec0740716226fadb3735c/contrib/mkimage/debootstrap#L40-L48
    && echo '#!/bin/sh' > /usr/sbin/policy-rc.d \
    && echo 'exit 101' >> /usr/sbin/policy-rc.d \
    && chmod +x /usr/sbin/policy-rc.d \
    \
# https://github.com/docker/docker/blob/9a9fc01af8fb5d98b8eec0740716226fadb3735c/contrib/mkimage/debootstrap#L54-L56
    && dpkg-divert --local --rename --add /sbin/initctl \
    && cp -a /usr/sbin/policy-rc.d /sbin/initctl \
    && sed -i 's/^exit.*/exit 0/' /sbin/initctl \
    \
# https://github.com/docker/docker/blob/9a9fc01af8fb5d98b8eec0740716226fadb3735c/contrib/mkimage/debootstrap#L71-L78
    && echo 'force-unsafe-io' > /etc/dpkg/dpkg.cfg.d/docker-apt-speedup \
    \
# https://github.com/docker/docker/blob/9a9fc01af8fb5d98b8eec0740716226fadb3735c/contrib/mkimage/debootstrap#L85-L105
    && echo 'DPkg::Post-Invoke { "rm -f /var/cache/apt/archives/*.deb /var/cache/apt/archives/partial/*.deb /var/cache/apt/*.bin || true"; };' > /etc/apt/apt.conf.d/docker-clean \
    && echo 'APT::Update::Post-Invoke { "rm -f /var/cache/apt/archives/*.deb /var/cache/apt/archives/partial/*.deb /var/cache/apt/*.bin || true"; };' >> /etc/apt/apt.conf.d/docker-clean \
    && echo 'Dir::Cache::pkgcache ""; Dir::Cache::srcpkgcache "";' >> /etc/apt/apt.conf.d/docker-clean \
    \
# https://github.com/docker/docker/blob/9a9fc01af8fb5d98b8eec0740716226fadb3735c/contrib/mkimage/debootstrap#L109-L115
    && echo 'Acquire::Languages "none";' > /etc/apt/apt.conf.d/docker-no-languages \
    \
# https://github.com/docker/docker/blob/9a9fc01af8fb5d98b8eec0740716226fadb3735c/contrib/mkimage/debootstrap#L118-L130
    && echo 'Acquire::GzipIndexes "true"; Acquire::CompressionTypes::Order:: "gz";' > /etc/apt/apt.conf.d/docker-gzip-indexes \
    \
# https://github.com/docker/docker/blob/9a9fc01af8fb5d98b8eec0740716226fadb3735c/contrib/mkimage/debootstrap#L134-L151
    && echo 'Apt::AutoRemove::SuggestsImportant "false";' > /etc/apt/apt.conf.d/docker-autoremove-suggests

# delete all the apt list files since they're big and get stale quickly
RUN rm -rf /var/lib/apt/lists/*
# this forces "apt-get update" in dependent images, which is also good
# (see also https://bugs.launchpad.net/cloud-images/+bug/1699913)

# make systemd-detect-virt return "docker"
# See: https://github.com/systemd/systemd/blob/aa0c34279ee40bce2f9681b496922dedbadfca19/src/basic/virt.c#L434
RUN mkdir -p /run/systemd && echo 'docker' > /run/systemd/container


# Install updates
RUN apt-get update -y && apt-get install -y software-properties-common language-pack-en-base 

# Install Java
RUN apt-get install -y default-jre


# Get the PHP library
RUN LC_ALL=en_US.UTF-8 add-apt-repository ppa:ondrej/php

RUN apt-get -y update && apt-get install -y \
    php7.1 \
    php7.1-pgsql \
    php-pear \
    php7.1-curl \
    php7.1-sqlite3 \
    php7.1-xml \
    php7.1-bcmath \
    php7.1-zip \
    php7.1-mbstring \
    php-xdebug \
    php-ast

RUN mkdir /app

WORKDIR /app
 
RUN /bin/bash -c "set -x /usr/bin/php7.1 ./x_test/php_version.php"
CMD ["php" "./x_test/php_version.php"]

解决方案

Total newbie mistake.. The terminal window doesn't show output returned by the script. You need additional logging commands for that.

I'm executing the following in VS Code:

docker compose up

^ this is the equivalent of executing docker-compose up -d --build in terminal

This ran in detached mode which apparently doesn't output logs from the container.

So I needed to do the following to see logs from the container

docker containers view logs

^ this is the equivalent of executing docker logs --tail in terminal

Then I saw all the errors it was returning. Turned out I was not executing the commands in the right format.

Easy fix was to write all commands into a bash script that was then called at the end of the Dockerfile using the following

CMD ["./start.sh"]

start.sh looks like

#!/usr/bin/env bash
set -e
php script1.php
php script2.php

P.S: I wish docker extension in VS code had a command like "docker compose up with logs" or something so you can right away see what's happening inside the container.

这篇关于码头工人:码头工人exec-它在Dockerfile或YML内部吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆