1

我不知道我错过了什么。我有一个docker image, 和一个docker compose文件。

我的目标是在Jenkins

我猜这个

volumes:
    - .:/var/www/html

是问题的根源,但容器最后一个图像不应该已经有供应商目录吗?

Dockerfile

FROM quay/php-base:7.2.
COPY . /var/www/html

RUN curl -sS https://getcomposer.org/installer | php
RUN php composer.phar install
RUN rm composer.phar

EXPOSE 9000

CMD ["php-fpm"]

build.testing.yml

version: "3"

services:
    phpfpm:
        build:
            context: .
            dockerfile: Dockerfile
        image: xxx-phpfpm:${GIT_COMMIT}
        environment:
            - APP_ENV=testing
        volumes:
            - .:/var/www/html

Jenkins 构建 shell 命令

printenv
echo $GIT_BRANCH
echo $GIT_COMMIT
docker-compose -f build.testing.yml up -d --build --remove-orphans
docker-compose -f build.testing.yml exec -T phpfpm bash build/phpunit.sh

phpunit.sh

#!/usr/bin/env bash
cd /var/www/html;
ls -la;
ls -la vendor;
ls -la vendor/bin;
php vendor/bin/phpunit;
exit $?

构建日志(最后几行)

phpunit/php-code-coverage suggests installing ext-xdebug (^2.6.0)
phpunit/phpunit suggests installing ext-soap (*)
phpunit/phpunit suggests installing ext-xdebug (*)
phpunit/phpunit suggests installing phpunit/php-invoker (^2.0)
Generating optimized autoload files

Removing intermediate container 56245736b26b
 ---> 818e587ac18a
Step 6/8 : RUN rm composer.phar
 ---> Running in 5ccbffc37e05

Removing intermediate container 5ccbffc37e05
 ---> 086e88f369fc
Step 7/8 : EXPOSE 9000
 ---> Running in 1c10e32ba134
Removing intermediate container 1c10e32ba134
 ---> 84eb9cbc3477
Step 8/8 : CMD ["php-fpm"]
 ---> Running in b6381d9b0f9a
Removing intermediate container b6381d9b0f9a
 ---> 40b32fa81d5f
Successfully built 40b32fa81d5f
Successfully tagged xxx-phpfpm:aa01355d1e8ab6d0f3daf87fb8f1f3b1be3e45be
Recreating xxxmicroservicetesting_phpfpm_1 ... 

Recreating xxxmicroservicetesting_phpfpm_1 ... done
+ docker-compose -f build.testing.yml exec -T phpfpm bash build/phpunit.sh
total 224
drwxrwxr-x   13 1002     1002          4096 Jul 29 19:35 .
drwxr-xr-x    1 root     root            18 May  5  2018 ..
-rw-rw-r--    1 1002     1002           213 Jul 26 15:38 .editorconfig
-rw-rw-r--    1 1002     1002           388 Jul 26 15:38 .env.example
drwxrwxr-x    8 1002     1002           162 Jul 29 19:41 .git
-rw-rw-r--    1 1002     1002            55 Jul 26 15:38 .gitignore
-rw-rw-r--    1 1002     1002            71 Jul 26 15:38 .styleci.yml
-rw-rw-r--    1 1002     1002           232 Jul 26 15:38 Dockerfile
-rw-rw-r--    1 1002     1002           895 Jul 29 18:25 Jenkinsfile
drwxrwxr-x   16 1002     1002           240 Jul 26 15:38 app
-rw-rw-r--    1 1002     1002          1094 Jul 26 15:38 artisan
drwxrwxr-x    2 1002     1002            21 Jul 26 15:38 bootstrap
drwxrwxr-x    2 1002     1002            24 Jul 29 19:41 build
-rw-rw-r--    1 1002     1002           250 Jul 29 19:35 build.testing.yml
-rw-rw-r--    1 1002     1002          1256 Jul 26 15:38 composer.json
-rw-rw-r--    1 1002     1002        179335 Jul 26 15:38 composer.lock
drwxrwxr-x    2 1002     1002            45 Jul 26 15:38 config
drwxrwxr-x    5 1002     1002            54 Jul 26 15:38 database
-rw-rw-r--    1 1002     1002           193 Jul 26 15:38 phpcs.xml
-rw-rw-r--    1 1002     1002           924 Jul 26 15:38 phpunit.xml
drwxrwxr-x    2 1002     1002            40 Jul 26 15:38 public
drwxrwxr-x    3 1002     1002            19 Jul 26 15:38 resources
drwxrwxr-x    2 1002     1002            40 Jul 26 15:38 routes
drwxrwxr-x    5 1002     1002            46 Jul 26 15:38 storage
drwxrwxr-x    4 1002     1002           113 Jul 26 15:38 tests
ls: vendor: No such file or directory
ls: vendor/bin: No such file or directory
Could not open input file: vendor/bin/phpunit
Build step 'Execute shell' marked build as failure
Finished: FAILURE
4

1 回答 1

1

当你有volumes:一个类似你展示的指令时docker-compose.yml,它告诉 Docker 完全忽略 Docker 映像的内容并使用本地文件系统中的任何内容。你应该删除它。

如果您在 Jenkins 或其他持续集成 (CI) 系统中运行此程序,则此行为意味着来自构建系统的映像实际上并未经过测试。您已经在 J​​enkins 签出的任何本地源代码树上运行测试,粘在构建图像的顶部。如果主机系统和映像在库版本或供应商库树方面存在任何类型的不兼容,您很容易陷入交付完全损坏的映像的情况,而该映像恰好通过了这个人工测试环境。

在这种情况下,我建议这样的顺序:

  1. 查看您的源代码树。

  2. 运行任何构建步骤和单元测试步骤。Jenkins 有一个在 Docker 容器中运行这些的路径,这比使用您需要的语言运行时维护 Jenkins 映像更方便,但这不是“构建和运行映像”。你也可以在没有 Docker 的情况下完全做到这一点。

  3. 实际构建 Docker 映像。我会使用Jenkins Docker 工作流插件或手动sh "docker build"步骤来执行此操作。

  4. 针对构建的映像运行集成测试。这可以使用docker-compose.yml上面显示的文件,但没有build:orvolumes:块。测试本身不会内置到映像中,但它会对映像的已发布 API 进行外部调用。

  5. 将构建的映像推送到注册表。如果合适,触发自动部署序列。

于 2019-07-29T22:58:41.427 回答