gpt4 book ai didi

php - 如何在 Azure 管道内的 Docker 容器中运行命令?

转载 作者:行者123 更新时间:2023-12-02 06:00:47 25 4
gpt4 key购买 nike

我有一个容器化的 PHP 应用程序。我正在尝试在 Azure 中设置管道,但我很难在构建的容器内运行命令(这对于完全安装和搭建应用程序至关重要,以便我可以运行代码分析和测试)。

我已经设置了 make 命令,所以安装应用程序的方法很简单

make up
make install

这些正在有效运行:

docker-compose up --build -d
docker-compose exec -T {containerName} composer install
docker-compose exec -T {containerName} php artisan migrate:fresh
docker-compose exec -T {containerName} php artisan passport:install
docker-compose exec -T {containerName} php artisan db:seed
...

要构建镜像,安装依赖项(laravel),搭建应用程序后端。

我的测试azure-pipelines.yml设置如下:

# Docker
# Build a Docker image
# https://learn.microsoft.com/azure/devops/pipelines/languages/docker

trigger:
- develop

resources:
- repo: self

variables:
tag: '$(Build.BuildId)'

stages:
- stage: Build
displayName: Build image
jobs:
- job: Build
displayName: Build
pool:
vmImage: ubuntu-latest
steps:
- task: Docker@2
displayName: Build an image
inputs:
command: build
dockerfile: '$(Build.SourcesDirectory)/infrastructure/php/Dockerfile'
tags: |
$(tag)
- job: Install
displayName: Install app
dependsOn: Build
steps:
- script: |
docker-compose exec -T api composer install
- job: Database
displayName: Scaffold Database
dependsOn: Install
steps:
- script: |
make db_reset
- job: Analyse
displayName: Analyse backend
dependsOn: Database
steps:
- script: |
make analyse
- job: Test
displayName: Run tests
dependsOn: Analyse
steps:
- script: |
php artisan test

但是在Install作业上失败,错误日志如下:

Starting: CmdLine
==============================================================================
Task : Command line
Description : Run a command line script using Bash on Linux and macOS and cmd.exe on Windows
Version : 2.201.1
Author : Microsoft Corporation
Help : https://learn.microsoft.com/azure/devops/pipelines/tasks/utility/command-line
==============================================================================
Generating script.
Script contents:
docker-compose exec -T api composer install
========================== Starting Command Output ===========================
/usr/bin/bash --noprofile --norc /home/vsts/work/_temp/a440fb6a-e03b-4da2-b958-1fa3aefa2084.sh
##[error]Bash exited with code '1'.
Finishing: CmdLine
Verifying lock file contents can be installed on current platform.
Package operations: 163 installs, 0 updates, 0 removals

In Filesystem.php line 268:

/var/www/app/vendor does not exist and could not be created.

在构建镜像后尝试安装 Composer 依赖项时。设置非常标准。

Dockerfile

FROM php:8.1-apache

RUN apt update
RUN apt-get install -y --no-install-recommends \
g++ \
libicu-dev \
libpq-dev \
libzip-dev \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev \
git \
lsb-release \
gnupg \
zip

RUN docker-php-ext-install \
intl \
opcache \
pdo \
pdo_mysql \
zip \
exif \
gd
RUN pecl install \
pcov \
xdebug


WORKDIR /var/www/app

RUN usermod -a -G www-data www-data
RUN chown root:root /var/www

RUN chown -R www-data:www-data /var/www/app
RUN chmod -R 755 /var/www/app
RUN mkdir /var/www/app/vendor
RUN chmod -R 775 /var/www/app/vendor
RUN chown -R www-data:www-data /var/www/app/vendor
RUN a2enmod rewrite

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

USER www-data

docker-compose

version: '3.8'

services:
database:
image: mysql:8.0
container_name: database
environment:
- MYSQL_DATABASE=app
- MYSQL_ROOT_PASSWORD=root
volumes:
- ./infrastructure/mysql-data:/var/lib/mysql
ports:
- '3306:3306'

api:
container_name: api
build:
context: ./infrastructure/php
ports:
- '8080:80'
volumes:
- ./server:/var/www/app
- ./infrastructure/apache/default.conf:/etc/apache2/sites-enabled/000-default.conf
depends_on:
- database

mailhog:
image: mailhog/mailhog:latest
container_name: mailhog
ports:
- '1025:1025'
- '8025:8025'

生成文件

DOCKER_COMPOSE = docker-compose
COMPOSER ?= composer
PHP_CMD = php

PHP_SERVICE = api

up:
@echo "\n==> Docker container building and starting ..."
$(DOCKER_COMPOSE) up --build -d

db_reset:
$(DOCKER_COMPOSE) exec -T api php artisan migrate:fresh
$(DOCKER_COMPOSE) exec -T api php artisan passport:install
$(DOCKER_COMPOSE) exec -T api php artisan db:seed

analyse:
$(DOCKER_COMPOSE) exec -T -u root api php artisan ide-helper:models -W
$(DOCKER_COMPOSE) exec -T -u root api ./vendor/bin/php-cs-fixer fix app
$(DOCKER_COMPOSE) exec -T -u root api ./vendor/bin/phpstan analyse --memory-limit=2G -c phpstan.neon

我假设问题出在尝试通过容器运行 Composer install 时的权限,但是,我实际上如何使用该容器呢?本地没有权限问题。构建显然成功了,这是构建作业的最后一个片段:

 ---> Running in b83b9f8e8010
All settings correct for using Composer
Downloading...
Composer (version 2.3.5) successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer

Removing intermediate container b83b9f8e8010
---> bc104ff13e89
Step 11/22 : USER www-data
---> Running in 200f01ed5b5f
Removing intermediate container 200f01ed5b5f
---> 2431fb9a77ae
Step 12/22 : LABEL com.azure.dev.image.build.buildnumber=20220509.10
---> Running in 8183d45902d5
Removing intermediate container 8183d45902d5
---> d1a9ed6d2dc6
Step 13/22 : LABEL com.azure.dev.image.build.builduri=vstfs:///Build/Build/14
---> Running in 316a03a492e7
Removing intermediate container 316a03a492e7
---> 8bd620fc9792
Step 14/22 : LABEL com.azure.dev.image.build.definitionname=REDACTED
---> Running in 75f66a32856a
Removing intermediate container 75f66a32856a
---> 4daf41f08c6c
Step 15/22 : LABEL com.azure.dev.image.build.repository.name=REDACTED
---> Running in 707ba3523e83
Removing intermediate container 707ba3523e83
---> 7084ad9947a7
Step 16/22 : LABEL com.azure.dev.image.build.repository.uri=REDACTED
---> Running in b08c87ff8818
Removing intermediate container b08c87ff8818
---> 83a6531aef80
Step 17/22 : LABEL com.azure.dev.image.build.sourcebranchname=develop
---> Running in 0d69670ee481
Removing intermediate container 0d69670ee481
---> 6389f09560dc
Step 18/22 : LABEL com.azure.dev.image.build.sourceversion=dab0a85595268c16a05c5292adc1e0340c13818f
---> Running in 3b58e7fe5640
Removing intermediate container 3b58e7fe5640
---> e495c2eeab89
Step 19/22 : LABEL com.azure.dev.image.system.teamfoundationcollectionuri=REDACTED
---> Running in 2e04a85c91b9
Removing intermediate container 2e04a85c91b9
---> 4570213d4e47
Step 20/22 : LABEL com.azure.dev.image.system.teamproject=REDACTED
---> Running in f4c914d2522c
Removing intermediate container f4c914d2522c
---> 0ee813e09dbf
Step 21/22 : LABEL image.base.digest=sha256:2891049f5a33bd4ac61ea28b4d4f9144468e50369b7bc29ff61e2dd3089f5bb6
---> Running in 564f498fb0c4
Removing intermediate container 564f498fb0c4
---> 039be559c0d4
Step 22/22 : LABEL image.base.ref.name=php:8.1-apache
---> Running in 60c373b879be
Removing intermediate container 60c373b879be
---> c10cb44c17d6
Successfully built c10cb44c17d6
Finishing: Build an image

...或者,我这样做完全错误吗?任何帮助将不胜感激。

最佳答案

权限问题发生在您的 ./server 目录上,您应该检查该目录的所有者和权限。

关于php - 如何在 Azure 管道内的 Docker 容器中运行命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72332702/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com