Search code examples
azure-devopsazure-pipelinesdocker-container

How to run web server docker container in Azure devops pipeline and HTTP access it?


Please help understand the cause and solution for the issue in Azure Devops pipeline.

Trying to run a docker container which runs a web server inside inside a Azure devops pipeline as a step.

docker pull ${CONTAINER_IMAGE}

CONTAINER_ID=$(docker run -d --rm \
  --cidfile cid.log \
  -p 108080:8080 \
  ${CONTAINER_IMAGE}
)
echo "container id is ${CONTAINER_ID}"
docker ps

echo "--------------------------------------------------------------------------------"
echo "docker container ip"
IPS=$(docker container inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}' ${CONTAINER_ID})
HOST=$(echo ${IPS[0]} | xargs)
echo $HOST

echo "--------------------------------------------------------------------------------"
echo "Testing web /health response from the container..."
curl -v http://${HOST}:8080/health

When run on a laptop, it works.

pulling the container image ****
...
Digest: sha256:9edc6a55118f0909cf7120a53837ae62b8e65154bc30b89630f4f82bc0c4add7
...
**Starting the container ****...
container id is 088a28329d236582f0757862cb5bba2172ddb40c3315394db11ab39265f155b3

CONTAINER ID   IMAGE                                                                                               COMMAND                  CREATED         STATUS         PORTS                     NAMES
088a28329d23   ****   "/bin/sh -c 'gunicor…"   6 seconds ago   Up 5 seconds   0.0.0.0:18080->8080/tcp   quirky_murdock
ccfc76e321aa   gcr.io/inverting-proxy/agent                                                                        "/bin/sh -c '/opt/bi…"   5 minutes ago   Up 5 minutes                             proxy-agent
--------------------------------------------------------------------------------
docker container ip
172.17.0.2

--------------------------------------------------------------------------------
Testing web /health response from the container...
* Expire in 0 ms for 6 (transfer 0x55c1870780f0)
*   Trying 172.17.0.2...
* TCP_NODELAY set
* Expire in 200 ms for 4 (transfer 0x55c1870780f0)
* Connected to 172.17.0.2 (172.17.0.2) port 8080 (#0)
> GET /health HTTP/1.1
> Host: 172.17.0.2:8080
> User-Agent: curl/7.64.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< Server: gunicorn
< Date: Mon, 08 Aug 2022 06:44:12 GMT
< Connection: close
< Content-Type: application/json
< Content-Length: 0
< 
* Closing connection 0**

However, it does not work in Azure DevOps CI pipeline.

++ docker run -d --rm --cidfile cid.log -p 18080:8080 *****
+ CONTAINER_ID=9c0f7c528b979651079d9f066c80cb9a26ec1af18415a0df5d269d252dfad0cb
+ echo 'container id is 9c0f7c528b979651079d9f066c80cb9a26ec1af18415a0df5d269d252dfad0cb'
container id is 9c0f7c528b979651079d9f066c80cb9a26ec1af18415a0df5d269d252dfad0cb
+ echo --------------------------------------------------------------------------------
+ echo 'listing docker processes...'
+ docker ps
--------------------------------------------------------------------------------
listing docker processes...
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
+ echo --------------------------------------------------------------------------------
--------------------------------------------------------------------------------
docker container ip
+ echo 'docker container ip'
+ docker container inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}' 9c0f7c528b979651079d9f066c80cb9a26ec1af18415a0df5d269d252dfad0cb

Error: No such container: 9c0f7c528b979651079d9f066c80cb9a26ec1af18415a0df5d269d252dfad0cb

Related issues

There is an issue reported.

The problem is that the VSTS build agent runs in a Docker container. When the Docker container for Apache is started, it runs on the same level as the VSTS build agent Docker container, not nested inside the VSTS build agent Docker container.
There are two possible solutions:

  • Replacing localhost with the ip address of the docker host, keeping the port number 8083
  • Replacing localhost with the ip address of the docker container, changing the host port number 8083 to the container port number 80.

Followed the solution as in the code above but did not work.


Solution

  • Please change docker ps with docker ps -a and check the logs of the died container for the reason of failure.

    you can see the logs using docker logs <container-id>