英文:
How properly run and use Docker in Azure Devops Pipelines?
问题
我尝试在流水线中运行一个 Docker 容器,首先我尝试使用文档:https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/reference/docker-v2?view=azure-pipelines&viewFallbackFrom=azure-devops&tabs=yaml#troubleshooting 但仍然遇到错误。
看起来我的流水线成功下载了一个 Docker 文件,但当我尝试在 Docker 文件中执行任何命令时,我会遇到错误。
steps:
- checkout: self
clean: true
fetchDepth: full
- task: Docker@2
displayName: 登录到 ACR
inputs:
containerRegistry: 'Docker'
command: 'login'
- script: |
docker run -d docker.io/Docker/Sample-docker-image
displayName: '运行 Docker'
- task: Docker@2
inputs:
containerRegistry: 'Docker'
command: 'start'
container: 'Docker/Sample-docker-image'
当我运行这个时,我得到以下错误信息:
Error response from daemon: No such container: ***/Docker/Sample-docker-image
Error: failed to start containers: ***/Docker/Sample-docker-image
##[error]Error response from daemon: No such container: ***/Docker/Sample-docker-image
##[error]Error: failed to start containers: ***/Docker/Sample-docker-image
##[error]进程 '/usr/bin/docker' 以退出码 1 失败
如何处理和正确使用 Docker?我在这个 YAML 文件中做错了什么?
编辑:
我添加了以下内容:
container_id=$(docker run -d Docker/Sample-docker-image)
echo "Container ID: $container_id"
echo "##vso[task.setvariable variable=container_id]$container_id"
container_id=$(echo $container_id)
while [[ "$(docker inspect -f '{{.State.Status}}' $container_id)" != "running" ]]; do
docker inspect $container_id
docker logs $container_id
sleep 10
docker ps -a
done
它显示 Docker 启动后处于退出状态。我在本地尝试过,每个镜像都按预期工作。
英文:
I tried do run a docker in pipeline ,first i tried to use documentation : https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/reference/docker-v2?view=azure-pipelines&viewFallbackFrom=azure-devops&tabs=yaml#troubleshooting but still got errors
Its looks like my pipeline properly download a docker file but when i try to exec any command in docker file i got errors
steps:
- checkout: self
clean: true
fetchDepth: full
- task: Docker@2
displayName: Login to ACR
inputs:
containerRegistry: 'Docker'
command: 'login'
- script: |
docker run -d docker.io/Docker/Sample-docker-image
displayName: 'Run Docker'
- task: Docker@2
inputs:
containerRegistry: 'Docker'
command: 'start'
container: 'Docker/Sample-docker-image'
when i run this i got
Error response from daemon: No such container: ***/Docker/Sample-docker-image
Error: failed to start containers: ***/Docker/Sample-docker-image
##[error]Error response from daemon: No such container: ***/Docker/Sample-docker-image
##[error]Error: failed to start containers: ***/Docker/Sample-docker-image
##[error]The process '/usr/bin/docker' failed with exit code 1
How to handle and properly use a docker ? Im doing something wrong in this yml ?
Edit:
i added
container_id=$(docker run -d Docker/Sample-docker-image)
echo "Container ID: $container_id"
echo "##vso[task.setvariable variable=container_id]$container_id"
container_id=$(echo $container_id)
while [[ "$(docker inspect -f '{{.State.Status}}' $container_id)" != "running" ]]; do
docker inspect $container_id
docker logs $container_id
sleep 10
docker ps -a
done
and it shows docker starts as exited status. i tried locally and every image works as expected
答案1
得分: 0
以下是在 Docker 镜像上执行命令的步骤。
步骤 1: 如果容器注册表是私有的,请创建一个服务连接。对于公共注册表,不需要服务连接。
步骤 2: 在流水线的资源部分指定容器,并将服务连接的名称添加为如下所示的端点。
resources:
containers:
- container: acr-customimg
image: cleveracr.azurecr.io/customimg:latest
endpoint: ACR-endpoint
步骤 3: 将容器资源添加为要在 Docker 容器上执行的步骤的目标。
- script: echo hostname
displayName: '在 Docker 容器中运行脚本'
target:
container: acr-customimg
以下是完整的流水线代码。
trigger: none
pool:
vmImage: ubuntu-latest
resources:
containers:
- container: acr-customimg
image: cleveracr.azurecr.io/customimg:latest
endpoint: ACR-endpoint
steps:
- script: echo hostname
displayName: '在 Azure 代理中运行脚本'
- script: echo hostname
displayName: '在 Docker 容器中运行脚本'
target:
container: acr-customimg
当我运行此流水线时,它首先从容器注册表获取镜像,然后启动容器。第一步将在Azure 代理上执行,而第二步将在Docker 容器上执行。
参考资料:
英文:
Below are the steps to execute a command on the docker image.
Step 1: Create a service connection to the container registry if it is private. For public registries, a service connection is not required.
Step 2: Specify the container in the resources section of the pipeline and add the service connection name as the endpoint as shown below.
resources:
containers:
- container: acr-customimg
image: cleveracr.azurecr.io/customimg:latest
endpoint: ACR-endpoint
Step 3: Add the container resource as the target to the steps you would like to execute on the docker container.
- script: echo hostname
displayName: 'run script in docker container'
target:
container: acr-customimg
Below is the full pipeline code.
trigger: none
pool:
vmImage: ubuntu-latest
resources:
containers:
- container: acr-customimg
image: cleveracr.azurecr.io/customimg:latest
endpoint: ACR-endpoint
steps:
- script: echo hostname
displayName: 'run script in azure agent'
- script: echo hostname
displayName: 'run script in docker container'
target:
container: acr-customimg
When I run this pipeline, it first fetches the image from the container registry and then start the container. The first step will execute on the azure agent while the second step will execute on the docker container.
References:
答案2
得分: 0
谢谢你的回答。我只犯了一个错误,我漏掉的只是一个字母 -t
docker run -t -d --name my-container
这解决了一个问题,容器正在运行,我能够运行docker exec。
英文:
Thank you for answers. I made mistake only thing which i missed is a letter -t
docker run -t -d --name my-container
This solved a issue, container is running and im able to run docker exec
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论