New docker Image from Running container doesnt hold updated values - docker

I have used Jenkins docker image from dockerhub(https://github.com/jenkinsci/docker)
FROM jenkins/jenkins:lts
USER root
ENV http_proxy http://bc-proxy-vip.de.pri.o2.com:8080
ENV https_proxy http://bc-proxy-vip.de.pri.o2.com:8080
RUN apt-get update
RUN apt-get install -y ldap-utils curl wget vim nano sudo
RUN adduser jenkins sudo
User jenkins
COPY plugins.txt /usr/share/jenkins/ref/plugins.txt
RUN /usr/local/bin/install-plugins.sh < /usr/share/jenkins/ref/plugins.txt
EXPOSE 8080
EXPOSE 50000
The docker build command was executed successfully and container also started successfully.
Docker build command :
docker build --no-cache -t myjenkins .
Docker container command :
docker run --net=host --name=my_jenkins -d -p 8080:8080 -p 50000:50000 myjenkins
Then I logged into Jenkins GUI , created a new user and updated the plugins.
Then created a new image using docker commit command. Master Image ID is c068f8d9a060. The newly created docker image ID is de0789b77703
docker commit c052fd7a26b3 almjenkins:version1
root#vagrant-ubuntu-trusty:~/jenkins# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
almjenkins version1 de0789b77703 13 minutes ago 1.04GB
myjenkins latest c068f8d9a060 4 hours ago 1.03GB
I executed docker run command to start the Jenkins from my new image.
docker run --net=host --name=alm_jenkins -d -p 8080:8080 -p 50000:50000 almjenkins:version1
When i accessed the Jenkins GUI, I'm unable to find the updates in new image.

As descibed in the offical docs for docker commit:
The commit operation will not include any data contained in volumes mounted inside the container.
The jenkins_home which holds all the jenkins configuration is declared as a volume in the Dockerfile for jenkins. Thus the commit command won't inlude can configuration (jobs, nodes, plugins ...)
The solution is to build a customized docker image that includes the configuration.
FROM jenkins/jenkins
COPY jobs /usr/share/jenkins/ref/jobs/
RUN /usr/local/bin/install-plugins.sh workflow-aggregator:2.5 ... # Install all the plugins that you need
You can extract the jobs folder from the old container and add it to the new on:
docker cp <container-name>:/var/jenkins_home/jobs jobs

Related

See image generated in docker

I created a Docker like:
FROM rikorose/gcc-cmake
RUN git clone https://github.com/hect1995/UBIMET_Challenge.git
WORKDIR /UBIMET_Challenge
RUN mkdir build
WORKDIR build
#RUN apt-get update && apt-get -y install cmake=3.13.1-1ubuntu3 protobuf-compiler
RUN cmake ..
RUN make
Afterwards I do:
docker build --tag trial .
docker run -t -i trial /bin/bash
Then I run an executable that saves a .png file inside the container.
How can I visualize the image?
You can execute something inside the container.
To see all containers you can run docker ps --all.
To execute something inside container you can run docker exec <container id> command.
Otherwise you can copy files from container to host, with docker cp <container id>:/file-path ~/target/file-path
Please mount a localhost volume(directory) with container volume(directory) in where you are saving your images.
now all of your images saved in container directory will be available in host or localhost mount directory. From there you can visualize or download to another machine.
Please follow this
docker run --rm -d -v host_volume_or-directory:container_volume_direcotory trial
docker exec -it container_name /bin/bash

Unable to find Jenkins config files inside docker container

I have used Jenkins docker image from dockerhub(https://github.com/jenkinsci/docker)
FROM jenkins/jenkins:lts
USER root
ENV http_proxy http://bc-proxy-vip.de.pri.o2.com:8080
ENV https_proxy http://bc-proxy-vip.de.pri.o2.com:8080
RUN apt-get update
RUN apt-get install -y ldap-utils curl wget vim nano sudo
RUN adduser jenkins sudo
User jenkins
COPY plugins.txt /usr/share/jenkins/ref/plugins.txt
RUN /usr/local/bin/install-plugins.sh < /usr/share/jenkins/ref/plugins.txt
EXPOSE 8080
EXPOSE 50000
The docker build command was executed successfully and container also started successfully.
Docker build command :
docker build --no-cache -t myjenkins .
Docker container command :
docker run --net=host --name=my_jenkins -d -p 8080:8080 -p 50000:50000 myjenkins
Then I logged in to the container via docker run -it myjenkins bash. I'm unable to find jenkins config files like config.xml, jenkins.xml etc.
I know this is an old issue, but I ran into this recently myself and found that when you run the containerized version of Jenkins, the configuration files are stored in:
/var/jenkins_home
A lot of people seem to be suggesting they're in /etc/sysconfig/jenkins for other Jenkins installs.

Airflow inside docker running a docker container

I have airflow running on an EC2 instance, and I am scheduling some tasks that spin up a docker container. How do I do that? Do I need to install docker on my airflow container? And what is the next step after. I have a yaml file that I am using to spin up the container, and it is derived from the puckel/airflow Docker image
I got a simpler solution working which just requires a short Dockerfile to build a derived image:
FROM puckel/docker-airflow
USER root
RUN groupadd --gid 999 docker \
&& usermod -aG docker airflow
USER airflow
and then
docker build -t airflow_image .
docker run -v /var/run/docker.sock:/var/run/docker.sock:ro \
-v /usr/bin/docker:/bin/docker:ro \
-v /usr/lib/x86_64-linux-gnu/libltdl.so.7:/usr/lib/x86_64-linux-gnu/libltdl.so.7:ro \
-d airflow_image
Finally resolved
My EC2 setup is running unbuntu Xenial 16.04 and using a modified the puckel/airflow docker image that is running airflow
Things you will need to change in the Dockerfile
Add USER root at the top of the Dockerfile
USER root
mounting docker bin was not working for me, so I had to install the
docker binary in my docker container
Install Docker from Docker Inc. repositories.
RUN curl -sSL https://get.docker.com/ | sh
search for wrapdocker file on the internet. Copy it into scripts directory in the folder where the Dockerfile is located. This starts the docker daemon inside airflow docker
Install the magic wrapper
ADD ./script/wrapdocker /usr/local/bin/wrapdocker
RUN chmod +x /usr/local/bin/wrapdocker
add airflow as a user to the docker group so the airflow can run docker jobs
RUN usermod -aG docker airflow
switch to airflow user
USER airflow
Docker compose file or command line arguments to docker run
Mount docker socket from docker airflow to the docker image just installed
- /var/run/docker.sock:/var/run/docker.sock
You should be good to go !
You can spin up docker containers from your airflow docker container by attaching volumes to your container.
Example:
docker run -v /var/run/docker.sock:/var/run/docker.sock:ro -v /path/to/bin/docker:/bin/docker:ro your_airflow_image
You may also need to attach some libraries required by docker. This depends on the system you are running Docker on. Just read the error messages you get when running a docker command inside the container, it will indicate you what you need to attach.
Your airflow container will then have full access to Docker running on the host.
So if you launch docker containers, they will run on the host running the airflow container.

Why can't docker commit a Jenkins container with customized configuration?

I pulled a Jenkins image and launched it. Then I did some configuration on that container. Now I want to save all my configuration into a new image. Below is the command I used:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f214096e4847 jenkins "/bin/tini -- /usr/lo" About an hour ago Up 1 seconds 50000/tcp, 0.0.0.0:8081->8080/tcp ci
From above output, you can see that the jenkins container f214096e4847 is running.
Now I use below command to commit my changes and create a new image:
$ docker commit f214096e4847 my_ci/1.0
sha256:d83801a700c4060326a5209b87281bfb0e93f46207d960038ba2d87628ddb90c
Then I stop the current container and run a new container from my_ci/1.0 image:
$ docker stop f214096e4847
f214096e4847
$ docker run -d --name myci -p 8081:8080 my_ci/1.0
aba1660be200291d499bf00d851a854c724193c0ee2afb3fd318c36320b7637e
But the new container doesn't include any changes I made. It looks like a container got created from original jenkins image. How to persist my data when using docker commit?
EDIT1
I know that I can add a volume to save the configuration data as below:
-v my_path:/var/jenkins_home
But I really want to save it on the docker image. So users don't need to provide the configuration from their host.
It's important to know that this isn't a good approach. As told you in the comments. The recommended way is to mount volumes.
But if you really want your volume in the image I can propose another way. You can create your own image derived from the official image:
Clone the git repo of the original image
git clone https://github.com/jenkinsci/docker.git
This contains the following:
CONTRIBUTING.md Jenkinsfile docker-compose.yml install-plugins.sh jenkins-volume plugins.sh update-official-library.sh
Dockerfile README.md init.groovy jenkins-support jenkins.sh tests weekly.sh
You just need to make one edit in the Dockerfile. Replace the VOLUME by a mkdir command
# Jenkins home directory is a volume, so configuration and build history
# can be persisted and survive image upgrades
#VOLUME /var/jenkins_home
RUN mkdir -p /var/jenkins_home
Rebuild your own image:
docker build -t my-jenkins:1.0
Start your own jenkins + install some plugins + create some jobs.
docker run -d -p 8080:8080 -p 50000:50000 my-jenkins:1.0
When you're ready with creating the desired jobs you can commit the container as an image.
docker commit 30c5889032a8 my-jenkins-for-developers:1.0
This newest jenkins container will contain your plugins + jobs by default.
docker run -d -p 8080:8080 -p 50000:50000 my-jenkins-for-developers:1.0
This will work in your case. But as I said. It's not recommended. It makes your content dependent of the image. So it's more difficult when you want to perform updates. Also your image can be too big (size).

Jenkins Docker container with root permissions?

I want to build a jenkins docker container with root permissions so that i can us apt-get feature to install gradle.
I am using this command to run jenkins on 8080 port but i also want to add gradle as enviornment variable :
docker run -p 8080:8080 -p 50000:50000 -v /var/jenkins_home:/var/jenkins_home jenkins
or what dockerfile i need to create and what to write in it so that jenkins also start running at 8080
I am now able to login into my docker container as root and apt-get can be used to install gradle or anything manually into the container.
Command i used to enter as root in container :
docker exec -u 0 -it mycontainer bash
Building an image that sets USER to root will make all interactive logins use root.
Dockerfile
FROM jenkins/jenkins
USER root
Then (setting your container ID):
docker exec -it jenkins_jenkins_1 bash
root#9e8f16419754:/$

Resources