I am quite new to Jenkins and Docker so I am stuck with trying to make them work together. What I want is to do next steps:
Build my project war-file on Jenkins (Done)
Update Docker image and container. In my case I want to stop running container (Tomcat on it), change war-file to the newest and then run it again.
I've already deploy my application on Docker, but this app is not updated by Jenkins.
I found some plugins, like docker-build-step or docker-plugin, however there are not enough information and tutorials about it and I find it really annoying spending hours and making random suggestions.
I would appetiate any useful tutorial as more spesific as possible.
Perhaps you don't need any of these plugins. You can implement it using command api of docker.
You need to install docker on the jenkins host machine.
Then in your Jenkins build config mvn plugin(or execute shell) to build target:
clean package assembly:assembly -Dmaven.test.skip=true
execute shell something like:
docker build --net=host -t reg.docker.xxx.com/xxx/xxx:latest ./
docker login --username=xxx --password=xxx reg.docker.xxx.com
docker push reg.docker.xxx.com/xxx/xxx:latest
execute shell bellow:
docker -H tcp://swarm.xxx.com:<port> --tlsverify --tlscacert=./ca.pem --tlscert=./cert.pem --tlskey=./key.pem run -v /home/admin/rc.local:/etc/rc.local:ro reg.docker.xxx.com/xxx/xxx:latest
------Edit-------
use docker upgrade if updating existing containers.
Related
I have a spring-boot maven application I've been running locally as:
mvn spring-boot:run
I want to be able to run this in GitLab runner so that when I push the code to master, it automatically copies the latest up there and runs the application.
My GitLab runner is configured in shell mode right now, and I have inside of the .gitlab-ci.yml file a deploy task that runs just that:
mvn spring-boot:run
The issue I am running into is after the application starts, I can see that it is running... but it never shows as success or completed. It just hangs there (because the terminal is still running when you execute that command?)
Question is, is there an alternate set of commands I should be running to get my spring-boot application to update and run each time I push to master? What is it i should be putting into my gitlab-ci.yml (or other files). Note that I am not using docker or kubernetes... just shell.
Sample gitlab CI:
run-deploy:
stage: deploy
script:
- mvn $MAVEN_CLI_OPTS spring-boot:run
Trying nohup with that also fails.
- nohup mvn $MAVEN_CLI_OPTS spring-boot:run &
I believe you can use the run stage for this. It would look something like
run:
stage: run
script:
- mvn $MAVEN_CLI_OPTS spring-boot:run
You can see an example of this here.
Make sure you also define the stages to include run as the docs state
If no stages are defined in .gitlab-ci.yml, then the build, test and deploy are allowed to be used as job’s stage by default. (see stages)
Is the sample file you provided above your entire configuration file or only a snippet of it? If so, I can adjust my answer to fit your needs. Thanks!
I'm sorry this is so late. I've recently just had the same problem. Gitlab runner blocks on child processes, and any process in the child tree. This makes the disown command impossible since you can't get to it. Forking, and nohup also don't work.
The only solution I could figure out was using the at command https://linux.die.net/man/1/at
Basically I put my command in a script then did:
at now < my_blocking_command_script.sh
That successfully complete the runner and kicked off my program in the background.
I checked the other posts but they are not what my issue represents.
I'm doing the Linuxacademy "Certified Jenkins Engineer" course and at the "Functional Testing" lesson, we add a docker agent with some steps in the Jenkinsfile but I am confused about the syntax used vs the syntax described in the official Jenkins Pipeline documentation Using Docker with Pipeline.
What the Jenkins tries to achieve is to run a test on a.jar file on a Jenkins node with CentOS but the test needs to be run on a Debian OS, in order to do that on the CentOS node, the Jenkinsfile has a stage with a Docker agent and a commmand that pulls the openjdk image from Dockerhub and run some command in it.
This is the syntax from the Lesson Repo:
stage("Test on Debian") {
agent {
docker 'openjdk:8u121-jre'
}
steps {
sh "wget http://brandon4231.mylabserver.com/rectangles/all/rectangle_${env.BUILD_NUMBER}.jar"
sh "java -jar rectangle_${env.BUILD_NUMBER}.jar 3 4"
Note that I simplified the file to match the work in progress, this one is the final version, but the focus is on the agent line.
My first question is that the Jenkins documentation syntax is different from the one used here but in the lesson video it runs with no issues, the correct syntax should be agent {
docker { image 'openjdk:8u121-jre' }
}
My second question is, wheter I use one or the other syntax, and also I used the openjdk:7u181-jre because the one from the lesson is not longer available, I get this error in the console log output:
If go to the node terminal and manually run
docker run openjdk:7u181-jre
it works fine, I run it as not sudo user.
Also I don't understand what the docker command does in the Jenkinsfile: Does it run the container after pulling it or just pull the container?
Any idea about what's going on?
Thanks.
1) Please install docker in the Jenkins server.
2) Please change the user-mode of Jenkins and docker same by running below command and restart both Jenkins and docker and now u will be able to access.
sudo usermod -aG docker jenkins-server-user-name
sudo systemctl restart docker
sudo systemctl restart jenkins
To install Jenkins: https://www.digitalocean.com/community/tutorials/how-to-install-jenkins-on-ubuntu-16-04
To install Docker: https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-16-04
Note: Verified on ubuntu server
I inherited Android Java-code in my company, without Gradle-files etc, and I want to be able to compile this on my dev-server (I program from a ChromeOS machine, hence a CLI SSH connection to a server where I do dev stuff). Now I found some Docker images like this one (which doesn't even have a working command line example) but I haven't managed to create an APK yet. What am I missing and how would you do this?
You have three steps to do:
Migrate your project to gradle.
It isn't too difficult since there are plenty of gradle project out there and you can try to follow them or just read "Migrating to Gradle" article.
Build project with gradle on local machine.
If you migrated properly you can build your project in terminal like:
./gradlew assembleDebug
but it might be also assembleDevDebug or assembleProdRelease which depends on your buildType and flavor in gradle. Check which assembles are available by running:
./gradlew tasks
Build project using Docker.
Based on image you linked:
docker run -t -i ksoichiro/android -v /path/to/project:/workspace -w workspace /bin/sh -c "./gradlew assembleDebug"
I'll start by saying that I'm not a Java developer and also I'm not a Docker expert.
In order to minimize the gap between frontend and backend (in this specific case, Java) developers I started to put some docker images in place with java and maven and after the build I start a docker container with a volume pointing to the java project (and frontend developers don't have to worry about dependencies or how to run backend services).
Already here I have a question.
I've seen other people building an image with the actual code inside instead of attaching it later, so what's the best case (if there's one)?
I've done this way since I can reuse that image for "every" project and avoid building different images.
For starting/stopping/restarting docker containers I created a script that does all of that, so I can make some changes to the code, bring it down and up again.
It kinda works, and what I mean is, I'm well aware this is not a normal workflow of a Java developer to do that kind of stuff from a console.
So now, to the most important question, how do you integrate docker with a Java IDE?
I know that you can create custom build/run commands but I also read that things like logs are not displayed on the IDE's.
Can someone explain me how are you using Docker + Java IDE's?
Note: Maven is also used for compiling java code, like mvn clean install (if this helps)
I do not use Docker with a Java IDE. I use the IDE (Eclipse) to write and test the code, with Maven to manage the build. Then I have a Dockerfile like this:
FROM java:8
RUN apt-get update || apt-get update
RUN apt-get install -y maven
# Maven installs Java 7, which set itself as the default...
RUN update-alternatives --remove java /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java
CMD java -jar target/main.jar
# Pull down dependencies here to allow Docker to cache more
ADD pom.xml /opt/app/pom.xml
WORKDIR /opt/app
RUN mvn dependency:go-offline -X
# I use the maven-shade-plugin to build a single jar
ADD src /opt/app/src
RUN mvn package
If you build all your images on one machine, then Docker will cache intelligently and you don't need to do anything more. If you want to run across more machines, or you just want to make it explicit, you could do something like this:
base/Dockerfile:
FROM java:8
RUN apt-get update || apt-get update
RUN apt-get install -y maven
RUN update-alternatives --remove java /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java
CMD java -jar target/main.jar
$ docker build -t yourorg/java-base:8 ./base/
myapp/Dockerfile:
FROM yourorg/java-base:8
ADD pom.xml /opt/app/pom.xml
WORKDIR /opt/app
RUN mvn dependency:go-offline -X
ADD src /opt/app/src
RUN mvn package
You don't get as big an effect from Docker with Java, because JARs are already pretty portable and well-contained. I suppose it makes it easy to run different Java versions side-by-side. I use it because it allows me to run applications in different languages without needing to know what's inside the container. I have some in Java, some in Python, some in JavaScript, some in Erlang, but they all get started as docker run -d <flags> myorg/myimage:someversion.
I would like to setup a Docker container using the following image: https://github.com/heroku/stack-images
I have installed the image inside Docker using
docker pull heroku/cedar:14
Which steps are required to start a Docker container that works in the same way Heroku does?
Communicate via Heroku ToolBelt cli in order to start and scale the application
Deploy applications using git and build using Java BuildPack
I've found an approach where you can turn an Heroku application into a Docker container but it is not the solution i'm looking for:
http://www.centurylinklabs.com/heroku-on-docker/
Checkout the Dokku project. It utilizes a docker project called buildstep to run Heroku buildpacks in a Cedar-like environment.
Have fun!