Using Docker pipeline inside a containerized Jenkins on Docker for Mac - macos

I'm trying to run Jenkins inside of Docker and use the Jenkins Docker Pipeline. I'm using 17.12.0-ce-mac45 (21669) which is the edge edition of Docker. I follow the tutorial on Jenkins's website and run:
docker run \
--rm \
-u root \
-p 8080:8080 \
-v jenkins-data:/var/jenkins_home \
-v /var/run/docker.sock:/var/run/docker.sock \
-v "$HOME":/home \
jenkinsci/blueocean
Everything starts up ok but when I try to run just a simple pipeline such as
pipeline {
agent {
docker { image 'node:7-alpine' }
}
stages {
stage('Test') {
steps {
sh 'node --version'
}
}
}
}
I get an error saying
sh: can't create /var/jenkins_home/workspace#tmp/durable-070664e3/jenkins-log.txt: nonexistent directory
sh: can't create /var/jenkins_home/workspace#tmp/durable-070664e3/jenkins-result.txt: nonexistent directory
Something else comes up before that which says:
Jenkins does not seem to be running inside a container
Anyone else run into this problem? When I use docker-machine, it works but just not on Docker for Mac edge. I'm not sure if it works on Docker for Mac stable. My only guess it that the error that comes up saying that Jenkins doesn't seem to think it's in a container is the root of the problem. I did notice in the Github repository on this line that it expects it to look like "/docker/" and that's what makes Jenkins think it's in a container or not. In docker-machine, it looks exactly like that, but on Docker for Mac Edge, it looks like /docker-ce/docker/

I'm seeing the same issue with Docker version 17.12.0-ce, build c97c6d6 on macOS, somebody has already reported it as a bug in the docker-workflow-plugin https://issues.jenkins-ci.org/browse/JENKINS-49005, for now I'm using docker-machine as a workaround until there is an update to the docker-workflow-plugin or Docker change the cgroup naming.

Related

Docker image for executing gradle bootBuildImage command

I'm looking for a docker image to build my gradle project which also need a docker engine to execute gradle bootBuildImage command. Any recommandation ?
Thanks,
Dan
If you use the Gradle wrapper scripts (which you should), you can use any image you like as long as it has Java on it. OpenJDK is a good match.
If you don't use the wrapper scripts, you need to have an image with Gradle installed. The official Gradle image should do.
But I think what you are really asking is how to build a docker image inside a container. The bootBuildImage task doesn't need the local Docker cli tools, and only needs to connect to a daemon. That daemon could be running on a remote host, but you can also make it connect to your local host outside the container. To do this, mount the local docker socket.
Here is an example that mounts the current directory inside a container and builds a Docker image in it through the Spring Boot plugin for Gradle:
docker run --rm \
-v gradle-cache:/home/gradle/.gradle \
-v /var/run/docker.sock:/var/run/docker.sock \
-v "$PWD":/home/gradle/project \
-w /home/gradle/project \
gradle:6.7.0-jdk11 \
gradle --no-daemon bootBuildImage
Note that it persists the Gradle home directory in a volume, which means you can't run this command concurrently. Delete the volume when no longer needed with docker volume rm gradle-cache.
Also note that it executes the build as root.

Unable to find docker image locally

I was following this post - the reference code is on GitHub. I have cloned the repository on my local.
The project has got a react app inside it. I'm trying to run it on my local following step 7 on the same post:
docker run -p 8080:80 shakyshane/cra-docker
This returns:
Unable to find image 'shakyshane/cra-docker:latest' locally
docker: Error response from daemon: pull access denied for shakyshane/cra-docker, repository does not exist or may require 'docker login'.
See 'docker run --help'.
I tried login to docker again but looks like since it belongs to #shakyShane I cannot access it.
I idiotically tried npm start too but it's not a simple react app running on node - it's in the container and containers are not controlled by npm
Looks like docker pull shakyshane/cra-docker:latest throws this:
Error response from daemon: pull access denied for shakyshane/cra-docker, repository does not exist or may require 'docker login'
So the question is how do I run this docker image on my local mac machine?
Well this is illogical but still sharing so future people like me don't get stuck.
The problem was that I was trying to run a docker image which doesn't exist.
I needed to build the image:
docker build . -t xameeramir/cra-docker
And then run it:
docker run -p 8080:80 xameeramir/cra-docker
In my case, my image had TAG specified with it and I was not using it.
REPOSITORY TAG IMAGE ID CREATED SIZE
testimage testtag 189b7354c60a 13 hours ago 88.3MB
Unable to find image 'testimage:latest' locally for this command docker run testimage
So specifying tag like this - docker run testimage:testtag worked for me
Posting my solution since non of the above worked.
Working on macbook M1 pro.
The issue I had is that the image was built as arm/64. And I was running the command:
docker run --platform=linux/amd64 ...
So I had to build the image for amd/64 platform in order to run it.
Command below:
docker buildx build --platform=linux/amd64 ...
In conclusion your docker image platform and docker run platform needs to be the same from what I experienced.
In my case, the docker image did exist on the system and still I couldn't run the container locally, so I used the exact image ID instead of image name and tag, like this:
docker run myContainer c29150c8588e
I received this error message when I typed the name/character wrong. That is, "name1\name2" instead of "name1/name2" (wrong slash).
In my case, I saw this error when I had logged in to the dockerhub in my docker desktop. The repo I was pulling was local to my enterprise. Once i logged out of dockerhub, the pull worked.
This just happened to me because my local docker vm on macos ran out of disk space.
I just deleted some old images using docker image prune and it started working correctly again.
shakyshane/cra-docker Does not exist in that user's repo https://hub.docker.com/u/shakyshane/
The problem is you are trying to run an imagen that does not exists. If you are executing a Dockerfile, the image was not created until Dockerfile pass with no errors; so when Dockerfile tries to run the image, it can't find it. Be sure you have no errors in the execution of your scripts.
The simplest answer can be the correct one!.. make sure you have permissions to execute the command, use:
sudo docker run -p 8080:80 shakyshane/cra-docker
In my case, I didn't realise there was a difference between docker run and docker start, and I kept using the run command when I should've been using the start command.
FYI, run is for building and creating the docker container, start is to just start a stopped container
Use -d
sudo docker run -d -p 8000:8000 rasa/duckling
learn about -d here
sudo docker run --help
At first, i build image on mac-m1-pro with this command docker build -t hello_k8s_world:0.0.1 ., when is run this image the issue appear.
After read Master Yi's answer, i realize the crux of the matter and rebuild my images like this docker build --platform=arm64 -t hello_k8s_world:0.0.1 .
Finally,it worked.

How can I find initial password for jenkins?

I already searched the related questions like here;
How do I get initial admin password for jenkins on Mac?
and here;
How to recover Jenkins password
However, I cannot find a solution for my problem.
I am following the instructions to install jenkins on this link;
https://jenkins.io/doc/book/installing/
and I have run the following commands to install and tried to make it run on my local machine (mac os);
docker run \
-u root \
--rm \
-d \
-p 8080:8080 \
-p 50000:50000 \
-v jenkins-data:/var/jenkins_home \
-v /var/run/docker.sock:/var/run/docker.sock \
jenkinsci/blueocean
It installs it properly but when I get to the login screen it asks for the initial admin password. Because the installation runs in (-d mode) backend, I cannot see the initial password after the installation completes. When I remove -d for the installation, then the installation does not work.
I also checked the shared folder (User/Shared/Jenkins/Home) directory and there was no secrets folder in it. So I created one manually and followed the instructions (in the answers) on this link again;
How do I get initial admin password for jenkins on Mac?
Afterwards, I removed the related docker process and restarted all the installation process from the beginning but I got the same result.
In this case, how can I find this initial admin password or how can I generate it again?
BTW: I am also checking the logs (where in /var/log/jenkins) but it seems that it stopped writing there after my first install attempt and I also couldn't find the initial password there).
docker exec <container_name> cat /var/jenkins_home/secrets/initialAdminPassword
I tried looking into the container's filesystem, but there's no secrets folder in it. But I found the solution in the jenkins documentation here
Docker outputs the initial secret to the console
To view the console use the command
docker logs <container id of jenkins>
output is somemthing like this:
If you are using Mac and Docker installation for Jenkins follow bellow steps to get initial administer password to start authentication in Jenkins Console. Type below command in Terminal.
(Note: This is working, if you have follow default steps in Jenkins documentation to install Jenkins in Docker environment)
Find the running containers
: docker ps
Copy the running containerID
: docker exec -it <containerID> bash
: cd /var/jenkins_home/secrets
: cat initialAdminPassword
Use secret password showing in terminal and used as initial password for Jenkins Console.
If you have installed Jenkins via docker, then the following command can give you the initial admin password. Assuming your container name/docker image name is jenkins
docker exec `docker ps | grep jenkins | awk '{ print $1}' ` cat /var/jenkins_home/secrets/initialAdminPassword
docker exec $(docker ps -q) cat /var/jenkins_home/secrets/initialAdminPassword
For me username was: admin
and you can find password by:
docker exec jenkins cat /var/jenkins_home/secrets/initialAdminPassword
my container name is jenkins
Can you install docker-compose and docker toolbox on your Mac?
https://docs.docker.com/compose/install/
Try to execute this docker-compose.yml file:
version: '3.1'
services:
blue-ocean:
image: jenkinsci/blueocean:latest
container_name: blue-ocean
restart: always
environment:
TZ: America/Mexico_City
ports:
- 8080:8080
- 50000:50000
tty: true
volumes:
- ./jenkins-data:/var/jenkins_home
- ./sock:/var/run/docker.sock
Only you need to create a folder with a docker-compose.yml file inside and execute the docker-compose up -d command in terminal, then the folders jenkins-data and sock will be created and inside of jenkins-data appear the directory ./jenkins-data/secrets/initialAdminPassword, open this file and copy the content and paste on the input of web view that requires it.

How can I use a docker image for ATS?

I have some knowledge of using docker images. I found one for ATS at:
https://hub.docker.com/r/dockerhwxi/ats2/
But I do not know how to use it (as there is current no documentation). Could someone kindly help?
You can follow below Steps :
Install Docker (Ref : https://docs.docker.com/install/ )
docker pull dockerhwxi/ats2:v0.3.9
docker run -ti -v dockerhwxi/ats2:v0.3.9
Optionally, after docker installation you can clone below repository and run install.sh file
https://github.com/steinwaywhw/docker-ats
Step 1: Download the image:
docker pull dockerhwxi/ats2:v0.3.9
Step 2: Run the image (to produce a container of the name myproject):
docker run --name myproject -ti -v /path/to/myproject:/MyRoot/myproject dockerhwxi/ats2:v0.3.9
You can exit the container and restart it later:
docker start -ai myproject

Jenkins tutorial maven project (with Docker) fails at Build stage

I'm using the current Jenkins Maven Project tutorial using Docker:
https://jenkins.io/doc/tutorials/build-a-java-app-with-maven/
I keep getting this error at the Build stage:
[simple-java-maven-app] Running shell script
sh: can't create
/var/jenkins_home/workspace/simple-java-maven-app#tmp/durable-bae402a9/jenkins-log.txt:
nonexistent directory
sh: can't create
/var/jenkins_home/workspace/simple-java-maven-app#tmp/durable-bae402a9/jenkins-result.txt:
nonexistent directory
I've tried setting least restrictive permissions with chmod -R 777, chown -R nobody and chown -R 1000 on the listed directories, but nothing seems to work.
This is happening with the jenkins image on Docker version 17.12.0-ce, build c97c6d6 on Windows 10 Professional.
As this is happening with the Maven project tutorial on the Jenkins site, I'm wondering how many others have run into this issue.
I had also the same problem on MacOSX.
After few hours of research, I have finally find the solution.
To solve the problem, it's important to understand that Jenkins is inside a container and when the docker agent inside this container talk to your docker engine, it give path to mount volume matching inner the container. But your docker engine is outer. So to allow to work correctly path inner the container must match the same path outer the container in your host.
To allow working correctly, you need to change 2 things.
docker run arguments
Jenkinsfile docker agent arguments
For my own usage, I used this
docker run -d \
--env "JENKINS_HOME=$HOME/Library/Jenkins" \
--restart always \
--name jenkins \
-u root \
-p 8080:8080 \
-v /var/run/docker.sock:/var/run/docker.sock \
-v $HOME/Library/Jenkins:$HOME/Library/Jenkins \
-v "$HOME":/home \
jenkinsci/blueocean
In the Jenkinsfile
Replace the agent part
agent {
docker {
image 'maven:3-alpine'
args '-v /root/.m2:/root/.m2'
}
by
agent {
docker {
image 'maven:3-alpine'
args '-v <host_home_path>/.m2:/root/.m2'
}
It's quite likely that this issue resulted from a recent change in Docker behaviour, which was no longer being handled correctly by the Docker Pipeline plugin in Jenkins.
Without going into too much detail, the issue was causing Jenkins to no longer be able to identify the container it was running in, which results in the errors (above) that you encountered with these tutorials.
A new version (1.15) of the Docker Pipeline plugin was released yesterday (https://plugins.jenkins.io/docker-workflow).
If you upgrade this plugin on your Jenkins (in Docker) instance (via Manage Jenkins > Manage Plugins), you'll find that these tutorials should start working again (as documented).
The error message means that the directory durable-bae402a9 was not created.
Walk back through the tutorial to find the step that should have created that directory, and make whatever changes are needed to make sure it succeeds.

Resources