2

Hiho,

I am trying to build and run a Docker-Container in a Jenkins pipeline and afterwards do some testing on the deyployed web-app in the container. Problem is my container only lives for some seconds and then dies with exit code 0. Already tried to run it in interactive mode and with and without the Docker Groovy Plugin.

My Pipeline Code looks like the following:

stage("Docker Build&Run") {
    steps {
        script {
            def tag = "shop:${env.BUILD_ID}"
            docker.build(tag)
            //sh "docker run -d -p 8443:443/tcp ${tag}" Tried both
            docker.image(tag).run('-p 8443:443/tcp')
        }
    }
}
stage("Dummy Dynamic Analysis") {
    steps {
        echo "Dummy Dynamic Analysis" //to be integrated
        sleep 120 //application should be available here since it has to be theire for  testing as well
    }
}

Events look like these. Destroyed after some seconds:

2018-11-07T17:27:30.104407703+01:00 image tag sha256:21ae4ef9603d390d0244cee30651485f2fb517a47c9e9d2c38855d093349105c (name=shop:35)
2018-11-07T17:27:31.260144894+01:00 container create ade7069d9d391146611f4f658156554823cc8086bd6769bbc32c3949d5b1a694 (image=shop:35, name=suspicious_liskov)
2018-11-07T17:27:31.456730373+01:00 network connect 36c2d6617773a7c7075caece71bcd744c89009b3db1962328d0f9930d981238a (container=ade7069d9d391146611f4f658156554823cc8086bd6769bbc32c3949d5b1a694, name=bridge, type=bridge)
2018-11-07T17:27:31.966564913+01:00 container start ade7069d9d391146611f4f658156554823cc8086bd6769bbc32c3949d5b1a694 (image=shop:35, name=suspicious_liskov)
2018-11-07T17:27:35.761690515+01:00 container die ade7069d9d391146611f4f658156554823cc8086bd6769bbc32c3949d5b1a694 (exitCode=0, image=shop:35, name=suspicious_liskov)
2018-11-07T17:27:35.929597196+01:00 network disconnect 36c2d6617773a7c7075caece71bcd744c89009b3db1962328d0f9930d981238a (container=ade7069d9d391146611f4f658156554823cc8086bd6769bbc32c3949d5b1a694, name=bridge, type=bridge)

How can I deploy the container and let it running for either unlimited time until stopped or atleast for the whole time jenkins is running?

Thanks in advance to everybody. Greetings

Tobias K.
  • 83
  • 5
  • it seems you are using `sleep 120` on a different stage. your docker image build , run and complete on previous stage and then this sleep execute in later stage. – Emruz Hossain Nov 07 '18 at 17:35
  • Yep, but acutally it is not changing the behaviorur if I move the sleep up to the other stage. Furthermore I would really like to keep the testing step and the deplyoment step separated. – Tobias K. Nov 07 '18 at 17:42
  • you have to put sleep on the docker image. so that the docker image wait before completing. – Emruz Hossain Nov 07 '18 at 17:44
  • @EmruzHossain Means this is the intended behaviour? Since when I am running "docker run" with -d switch locally on the same build server its running without dieing forever. – Tobias K. Nov 07 '18 at 18:23
  • can you try this `def img = docker.build(tag)` then `img.run('-p 8443:443/tcp')` . Your docker image dies with `exitCode=0` which means there were no error. – Emruz Hossain Nov 07 '18 at 19:01
  • Unfortunately still the same behaviour – Tobias K. Nov 08 '18 at 08:21
  • 1
    What do container logs show ? Try docker logs container-id. Do you have a dockerfile for your docker image ? If yes, post the dockerfile here – ben5556 Nov 08 '18 at 10:31

1 Answers1

0

So apperently my docker file looked like this:

FROM ubuntu:14.04

RUN echo "#!/bin/sh\nexit 0" > /usr/sbin/policy-rc.d

ADD ./provision.sh /tmp/provision.sh
RUN /bin/bash /tmp/provision.sh

ENTRYPOINT service apache2 start && service mysql start && /bin/bash

EXPOSE 443

I just realized reading this article: How to keep Docker container running after starting services? that the docker container would exit when my entrypoint call was done and not stay alive as long as my services are alive. Which is what I thought would happen. I now switched to the proposed solution running supervisord and its running fine. Thanks all.

New Dockerfile:

FROM ubuntu:14.04

RUN echo "#!/bin/sh\nexit 0" > /usr/sbin/policy-rc.d

RUN apt-get update && apt-get install supervisor -y

ADD ./provision.sh /tmp/provision.sh
ADD ./supervisord.conf /etc/supervisor/conf.d/supervisord.conf
RUN /bin/bash /tmp/provision.sh

ENTRYPOINT ["/usr/bin/supervisord", "-n"]

EXPOSE 443

Supervisor Conf:

[supervisord]
nodaemon=true

[program:mysql]
command=/usr/bin/pidproxy /var/run/mysqld/mysqld.pid /usr/sbin/mysqld
autorestart=true

[program:apache]
command=/usr/sbin/apache2ctl -DFOREGROUND
autorestart=true
Tobias K.
  • 83
  • 5