Jenkins not starting in docker (Dockerfile included) - docker

I am attempting to build a simple app with Jenkins in a docker container. I have the following Dockerfile:
FROM ubuntu:trusty
# Install dependencies for Flask app.
RUN sudo apt-get update
RUN sudo apt-get install -y vim
RUN sudo apt-get install -y curl
RUN sudo apt-get install -y python3-pip
RUN pip3 install flask
# Install dependencies for Jenkins (Java).
# Install Java 1.8.
RUN sudo apt-get install -y python-software-properties debconf-utils
RUN sudo apt-get install -y software-properties-common
RUN sudo add-apt-repository -y ppa:webupd8team/java
RUN sudo apt-get update
RUN echo "oracle-java8-installer shared/accepted-oracle-license-v1-1 select true" | sudo debconf-set-selections
RUN sudo apt-get install -y oracle-java8-installer
# Install, start Jenkins.
RUN sudo apt-get install -y wget
RUN wget -q -O - https://pkg.jenkins.io/debian/jenkins-ci.org.key | apt-key add -
RUN echo deb http://pkg.jenkins-ci.org/debian binary/ > /etc/apt/sources.list.d/jenkins.list
RUN sudo apt-get update
RUN sudo apt-get install -y jenkins
RUN sudo /etc/init.d/jenkins start
COPY ./app /app
CMD ["python3","/app/main.py"]
I run this container with the following:
docker build -t jenkins_test .
docker run --name jenkins_test_container -tid -p 5000:5000 -p 8080:8080 jenkins_test:latest
I am able to start flask and install Jenkins, however, when running, Jenkins is not running. curl localhost:8080 is not successful.
In the log output, I am able to see:
Correct java version found
* Starting Jenkins Automation Server jenkins [ OK ]
However, it's still not running.
I can ssh into the container and manually run sudo /etc/init.d/jenkins start to start it, but I want it to start on docker run or docker build.
I have also tried putting sudo /etc/init.d/jenkins start in the CMD portion of the Docker file:
CMD python3 /app/main.py; sudo /etc/init.d/jenkins start
With this, I am able to curl Flask, but still not Jenkins.
How can I get Jenkins to start automatically?

You have some points that you need to be aware of:
No need to use sudo as the default user is root already.
In order to run multiple service in the same container you need to use any kind of service manager like Supervisord. Jenkins is not running because the CMD is the main entry point for your container so only flask should be running. Check the following link in order to know how to start multiple service in docker.
RUN will be executed only during the build process unlike CMD which will be executed each time you start a container from that image.
Combine all the RUN lines together as possible in order to minimize the build layers which lead to a smaller docker image.
Regarding the usage of this:
CMD python3 /app/main.py; sudo /etc/init.d/jenkins start
It does not work for you because this command python3 /app/main.py is not running as a background process so this command sudo /etc/init.d/jenkins start wont run until the previous command is done.

I was only able to get this to work by starting Jenkins in the CMD portion, but needed to start Jenkins before Flask since Flask would continuously run and the next command would never execute:
Did not work:
CMD python3 /app/main.py; sudo /etc/init.d/jenkins start
This did work:
CMD sudo /etc/init.d/jenkins start; python3 /app/main.py
EDIT:
I believe putting it in the RUN portion would not work because container would build but not save the any running services. I'm not sure if containers can be saved and loaded with running processes like that but I might be wrong. Would appreciate clarification if so.
It seems like a thing that should be in RUN so if anyone knows why that didn't work or some best practices, would also appreciate the info.

Related

I have created custom docker image for jenkins server If I use it to run normal docker container it gives no error but in k8s cluster CrashLoopBackOff

DockerFile
FROM centos
RUN yum install java-1.8.0-openjdk-devel -y
RUN curl --silent --location http://pkg.jenkins-ci.org/redhat-stable/jenkins.repo | tee /etc/yum.repos.d/jenkins.repo
RUN rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key
RUN yum install jenkins --nogpgcheck -y
RUN yum install jenkins -y
RUN yum install -y initscripts
CMD /etc/init.d/jenkins start && /bin/bash
the output of describing command
enter image description here
output of logs
Starting Jenkins [ OK ]
There isn't an init system inside a container so this isn't going to work. Likely the specific issue is that with plain Docker you are using docker run -it so there is a stdin, so bash starts in interactive mode and keeps running. In Kubernetes there is no input so bash exits immediately and the container exits with it. You can't run stuff in the background like that. Maybe just use the official jenkins/jenkins image? Or at least check out how it's built.

How do I access the frontend of Jenkins running in a Ubuntu Docker container?

I'm setting up Jenkins on my local machine to get it ready for a production deployer environment. I need to make sure that my setup steps mirror the production setup. Production is running Ubuntu 16.04, while my local machine is running macOS Catalina.
To make sure I can walk through setup as will be necessary on production, I'm using Docker to run the same OS as prod and installing Jenkins in that container.
I have installed Jenkins in the Docker container (which is FROM ubuntu:16.04). I'm unsure of the next steps though. How to I expose the Jenkins frontend so that I may access it in my browser?
This may not be necessary to answer the question, but here's by Dockerfile:
FROM ubuntu:16.04
RUN apt-get update
# Install Jenkins dependencies and Jenkins
RUN apt-get install -y wget sudo vim apt-transport-https ca-certificates apt-utils
RUN wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -
RUN sh -c 'echo deb https://pkg.jenkins.io/debian-stable binary/ > \
/etc/apt/sources.list.d/jenkins.list'
RUN apt-get update
RUN apt-get install -y jenkins
# Install Java
RUN apt-get -o Dpkg::Options::="--force-overwrite" install -y openjdk-8-jdk
# add the java binaries to jenkins PATH
RUN sed -i "s|PATH=/bin:/usr/bin:/sbin:/usr/sbin|PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/lib/jvm/java-8-openjdk-amd64/bin|g" \
/etc/init.d/jenkins
After building that, I exec in and run service jenkins start to start Jenkins.
Newbie to Docker, thanks for the help!
To get access to jenkins web interface you need to expose it's default port (8080) while running container with jenkins master.
For example:
docker run -dit -p 8080:8080 your_jenkins_image

"Can not connect to Docker Daemon"

I have a project in which I need to use CircleCi to build a docker application image, and then upload it to the Amazon container repository.
Given that CircleCI also runs on Docker, I created a Docker image for it, which containers a version of Ubuntu, together with AWS CLI, Node and Docker. See Dockerfile below:
FROM ubuntu:16.04
# update libraries
RUN apt-get update
RUN apt-get install -y apt-transport-https ca-certificates curl software-properties-common
RUN curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
RUN add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
# install docker
RUN apt-get update
RUN apt-cache policy docker-ce
RUN apt-get install -y docker-ce
# <---
RUN systemctl status docker # <--- TROUBLE HERE
# <---
# install node
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash -
RUN apt install -y nodejs
# install aws cli
RUN apt-get install -y python-pip python-dev build-essential
RUN pip install --upgrade pip
RUN pip install awscli --upgrade
I am currently having some problems working with this CircleCi docker image, because, if i keep the command RUN systemctl status docker I get the following error:
Failed to connect to bus: No such file or directory The command '/bin/sh -c systemctl status docker' returned a non-zero code: 1
If, on the other, I remove that command, the build is sucessful. However, when I go inside docker sudo docker run -it unad16 and run any docker command, as, f.e., docker images, I get the following error:
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
I have been trying to debug this error since yesterday, but have been unsucessfull. Thus, any help would be truly appreciated.
Notes:
the "daemon" error occurs even when I run docker in priviled mode with sudo docker run -ti --privileged=true unad16
You don't need to run a docker daemon if you want to build a docker image in circleci. Instead you just need an image with docker client, and a circle config with - setup_remote_docker.
Read more in
https://circleci.com/docs/2.0/building-docker-images/
If for some other reason you still want to run a docker service in a docker image, please refer to DockerInDocker repo, especially the README.md part.

boot2docker / docker "Error. image library/.:latest not found"

I'm trying to create a VM with docker and boot2docker. I've made the following Dockerfile, which I'm trying to run through the command line
docker run Dockerfile
Immidiatly it says exactly this:
Unable to find image 'Dockerfile:latest' locally
FATA[0000] Invalid repository name <Dockerfile>, only [a-z0-9_.] are allowed
Dockerfile:
FROM ubuntu:latest
#Oracle Java7 install
RUN apt-get install software-properties-common -y
RUN apt-get update
RUN add-apt-repository -y ppa:webupd8team/java
RUN apt-get update
RUN echo oracle-java7-installer shared/accepted-oracle-license-v1-1 select true | /usr/bin/debconf-set-selections
RUN apt-get install -y oracle-java7-installer
#Jenkins install
RUN wget -q -O - http://pkg.jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add -
RUN sudo echo "deb http://pkg.jenkins-ci.org/debian binary/" >> /etc/apt/sources.list
RUN apt-get update
RUN apt-get install --force-yes -y jenkins
RUN sudo service jenkins start
#Zip support install
RUN apt-get update
RUN apt-get -y install zip
#Unzip hang.zip
RUN unzip -o /var/jenkins/hang.zip -d /var/lib/jenkins/
RUN chown -R jenkins:jenkins /vaR/lib/jenkins
RUN service jenkins restart
EXEC tail -f /etc/passwd
EXPOSE 8080
I am in the directory where the Dockerfile is, when trying to run this command.
Ignore the zip part, as that's for later use
You should run docker build first (which actually uses your Dockerfile):
docker build --tag=imagename .
Or
docker build --tag=imagename -f yourDockerfile .
Then you would use that image tag to docker run it:
docker run imagename
There are tools that can provide this type of feature.
We have achieved using docker compose, though you have to go through
(https://docs.docker.com/compose/overview/)
docker-compose up
but you can also do as work around
$ docker build -t foo . && docker run foo.

Beanstalkd in docker

I'm building a Docker image with this Dockerfile
FROM ubuntu:12.04
ENV DEBIAN_FRONTEND noninteractive
ENV PATH /usr/local/rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# update apt
RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list
RUN apt-get update
RUN apt-get -y dist-upgrade
RUN apt-get install -y beanstalkd
RUN sed -i 's/\#START=yes/START=yes/g' /etc/default/beanstalkd
EXPOSE 11300
ENTRYPOINT service beanstalkd start
The image is successfully built and then I want to create an instance:
docker run -i -d -p 11300:11300 beanstalk /bin/bash
However, when I do docker ps -a, the instance has status Exit 0. I'm assuming that this means that the instance is not running. When I try to start it or attach to it, nothing seems to be happening.
So the question is why is the container not running?
Thanks, Michal
With service beanstalkd start you are starting the server, and then exiting. You will want to run the program directly - ENTRYPOINT /usr/local/bin/beanstalkd -l 0.0.0.0 -p 11300 -b .... (etc)

Resources