I am using jenkins image to run on docker container. I have a modified version of the image as below:
USER root
RUN apt-get update
RUN apt-get install -y sudo
RUN curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
RUN apt-get install -y nodejs
RUN npm -v
USER jenkins
when I run the container based on this image it all goes fine. I can go into the container and do npm -v and it all works just fine. However, the build script on my jenkins which is simply as
echo 'starting build'
npm -v
fails with error npm not found.
npm is not in the path of your jenkins' user.
You could get a shell on your container to figure out the npm path:
docker exec -it <CONTAINER_NAME> bash
which npm
Then you could run it with a full path in the jenkins script, symlink it, add it to $PATH etc...
Related
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.
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
When I install yarn using this command in CentOS terminal:
sudo wget https://dl.yarnpkg.com/rpm/yarn.repo -O /etc/yum.repos.d/yarn.repo
# Install yarn
yum install -y yarn
The yarn command run perfectly in terminal. But when the run the yarn install command in Docker's Jenkins(after docker's jenkins chekcout the project source code,compile react project),this is my build script:
yarn install
yarn build
it throw this error:
/bin/sh -xe /tmp/jenkins3735067167187767767.sh
+ yarn install
/tmp/jenkins3735067167187767767.sh: 2: /tmp/jenkins3735067167187767767.sh: yarn: not found
Build step 'Execute shell' marked build as failure
Finished: FAILURE
When I don't using docker,I know it runs as user jenkins,but now I could not switch to user jenkins because it does not have a jenkins user.How to fix it?
Docker version 1.13.1, build 07f3374/1.13.1
Node version:v10.15.3
yarn version:v1.15.2
Your host is isolated from the docker container, so whatever you need to install you have to install inside the container itself not on the actual host. In case you are using an ubuntu image you need to do the following steps inside your container or inside your Dockerfile if you are building your own image. Make sure to use the root user for these steps:
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
apt update
apt install yarn
If you are just getting started with docker I suggest that you take a look at the following tutorial to get a better view about how it works: https://docs.docker.com/get-started/
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.
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.