Docker containers in Azure pipelines in privileged mode possible? - azure

we are starting to use docker containers in our Azure pipelines.
For one scenario we would need to run the docker container in privileged mode and I am wondering whether Azure pipelines supports "privileged" container execution?
Thank you

If docker containers are located in Docker Hub or Local Machineļ¼Œ you could run the docker containers in privileged mode.
You could try to run the following script: docker run --privileged [image_name]
steps:
- task: Docker#2
inputs:
containerRegistry: 'DockerServiceConnectionName'
command: 'login'
- script: docker run --privileged [image_name]
You could refer to this ticket about the Azure Container.
Azure Container Instances does not expose direct access to the underlying infrastructure that hosts container groups. This includes running privileged containers and thus it is not supported currently.

Related

Azure DevOps self-hosted build agent - Kaniko

We need to build docker images using self-hosted linux agent which is deployed as docker container (in Azure Container Instances).
As of now, the agent is Ubuntu image, however to enable building images inside this container I thought of using Kaniko image. However, I haven't figured out how to run Kaniko image without executing the kaniko itself right away (as we need to run devops agent primarily and run kaniko on-demand).
Any hints? Or better ideas how to build docker images in running docker container?
Solved with following code, however Kaniko does not work as expected when running inside my container (tested the same parameters with kaniko inside my container and in default container and in my container does not work (cannot authenticate to ACR)).
Might end up with the VMSS DevOps agent...
FROM whatever-base-image
...
COPY --from gcr.io/kaniko-project/executor /kaniko/executor /kaniko/executor
Ref: https://github.com/GoogleContainerTools/kaniko/issues/2058#issuecomment-1104666901

Connect Docker containers to same network as current Container in Azure Pipelines

I am running an Azure Container Job, where I spin up a different Docker container manually like this:
jobs:
- job: RunIntegrationTests
pool:
vmImage: "ubuntu-18.04"
container:
image: mynamespace/frontend_image:latest
endpoint: My Docker Hub Endpoint
steps:
- script: |
docker run --rm --name backend_container -p 8000:8000 -d backend_image inv server
I have to create the container manually since the image lives in AWS ECR, and the password authentication scheme that Azure provides for it can only be used with a token that expires, so it seems useless. How can I make it so that my_container is reachable from within subsequent steps of my job?. I have tried starting my job with:
options: --network mynetwork
And share it with "backend_container", but I get the error:
docker: Error response from daemon: Container cannot be connected
to network endpoints: mynetwork
While starting the "frontend" container, which might be because Azure is trying to start a container on multiple networks.
To run a container job, and attach a custom image to the created network, you can use a step as showed in the below example:
steps:
- task: DownloadPipelineArtifact#2
inputs:
artifactName: my-image.img
targetPath: images
target: host # Important, to run this on the host and not in the container
- bash: |
docker load -i images/my-image.img
docker run --rm --name my-container -p 8042:8042 my-image
# This is not really robust, as we rely on naming convections in Azure Pipelines
# But I assume they won't change to a really random name anyway.
network=$(docker network list --filter name=vsts_network -q)
docker network connect $network my-container
docker network inspect $network
target: host
Note: it's important the these steps run in the host, and not in the container (that is run for the container-job). This is done by specifying target: host for the task.
In the example the container from the custom image can the be addressed by my-container.
I ended up not using the container: property altogether, and started all containers manually, so that I can specify the same network:
steps:
- task: DockerInstaller#0
displayName: Docker Installer
inputs:
dockerVersion: 19.03.8
releaseType: stable
- task: Docker#2
displayName: Login to Docker hub
inputs:
command: login
containerRegistry: My Docker Hub
- script: |
docker network create integration_tests_network
docker run --rm --name backend --network integration_tests_network -p 8000:8000 -d backend-image inv server
docker run --rm --name frontend -d --network integration_tests_network frontend-image tail -f /dev/null
And run subsequents commands on the frontend container with docker exec

Azure App Service, docker container deployment, unable to pass cap-add=NET_ADMIN property during docker run

I was doing single container deployment in the azure app service. As my container needs to be run in the NET_ADMIN mode, i had to pass cap-add=NET_ADMIN during the docker run, something like this
docker run -e cap-add=NET_ADMIN -p 8080:8080 my_image:v1
In azure app service we have to pass the runtime arguments in the configurations.
But it is a known issue that we can't pass any key with - (hiphen) from configurations.
So i am unable run my container in the NET_ADMIN mode.
Is there any work around, so that i will be able to run with NET_ADMIN mode in azure app service?
Base image : alpine 4.1.4
PS: My requirement needs me to run a single container and not with docker-compose

Deploy a ASPNETAPP on a docker container to a Windows VM using Azure DevOps Server 2019

I'm currently using Azure DevOps Server 2019 (on-premise) to deploy an ASP.NET App (CI-CD).
Is it possible to deploy this app to run via a docker container to a Windows VM?
i'm currently following the examples on this link on how to run an ASP.NET App on a docker container.
https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/docker/building-net-docker-images?view=aspnetcore-3.1
How could i do the same by utilising Azure DevOps Server 2019 to do so.
basically most of not all of the resources/guides/how-to s saw are pointing deploy to the azure cloud or docker hub.
Is it possible to deploy this app to run via a docker container to a Windows VM?
Yes it is possible, You will need to create a self-hosted agent on the Windows VM to which you deploy your app. You can just use powershell task to run docker build and docker run on the self-hosted agent without the need to upload the image to ACR/dockerhub.
Of course You can aslo upload the built image to ACR/dockerhub as #Aravind mentioned. And have a powershell task that pull the image.
The main idea is to use a powershell task to run docker command on the agent hosted on the Windows VM. You can refer to below steps.
1,create a self-hosted agent. Please check the detailed steps here.
2,create a build pipeline.
Here is an example to create a yaml pipeline.
Here is an example to create a classic UI pipelie.
3, Customize your build pipeline, Use a single powershell task to run docker build and docker run command as described in the tutorial. (You can also use docker task to build and push image to ARC/Dockerhub, and then use powershell task to pull and run the image as #Aravind mentioned.)
steps:
- powershell: |
docker build -t aspnetapp .
docker run -it --rm -p 5000:80 --name aspnetcore_sample aspnetapp
displayName: 'PowerShell Script'
Noted: please make sure docker is installed on the Windows VM(the powershell task will invoke the docker cli installed on the VM). And choose the self-hosted agent(hosted on Windows VM) to run your pipeline by choosing the agent pool where the self-hosted agent resides(the agent pool that includes the self-hosted agent is decided at the creation of the agent.)

build and push docker images with GitLab CI

I would like to build and push docker images to my local nexus repo with GitLab CI
This is my current CI file:
image: docker:latest
services:
- docker:dind
before_script:
- docker info
- docker login -u some_user -p nexus-rfit some_host
stages:
- build
build-deploy-ubuntu-image:
stage: build
script:
- docker build -t some_host/dev-image:ubuntu ./ubuntu/
- docker push some_host/dev-image:ubuntu
only:
- master
when: manual
I also have a job for an alpine docker image, but when I want to run any of it it's failing with the following error:
Checking out 13102ac4 as master...
Skipping Git submodules setup
$ docker info
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
ERROR: Job failed: exit code 1
So technically the docker daemon in the image isn't running, but I have no idea why.
GitLab folks have a reference on their docs about using docker-build inside docker-based jobs: https://docs.gitlab.com/ce/ci/docker/using_docker_build.html#use-docker-in-docker-executor. Since you seem to have everything in place (i.e. the right image for the job and the additional docker:dind service), it's most likely a runner-config issue.
If you look at the second step in the docs:
Register GitLab Runner from the command line to use docker and privileged mode:
[...]
Notice that it's using the privileged mode to start the build and service containers. If you want to use docker-in-docker mode, you always have to use privileged = true in your Docker containers.
Probably you're using a runner that was not configured in privileged mode and hence can't properly run the docker daemon inside. You can directly edit the /etc/gitlab-runner/config.toml on your registered runner to add that option.
(Also, read on the section on the docs for some more info about the performance related to the storage driver you choose/your runner supports when using dind)

Resources