I have the following dockerfile.
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
ADD ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
This is build.gradle.
buildscript {
ext {
springBootVersion = '2.0.0.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath('se.transmode.gradle:gradle-docker:1.2')
}
}
apply plugin: 'java'
sourceCompatibility = 1.8
targetCompatibility = 1.8
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'docker'
bootJar {
baseName = 'gs-spring-boot-docker'
version = '0.1.0'
}
group = 'com.pai'
version = '0.0.1-SNAPSHOT'
task buildDocker(type: Docker, dependsOn: build){
push = true
applicationName = jar.baseName
dockerfile = file('src/main/docker/Dockerfile')
doFirst {
copy {
from jar
into stageDir
}
}
}
repositories {
mavenCentral()
}
dependencies {
compile 'org.springframework.boot:spring-boot-starter-data-jpa'
compile 'mysql:mysql-connector-java'
compile group: 'org.apache.commons', name: 'commons-io', version: '1.3.2'
compile('org.springframework.boot:spring-boot-starter-security')
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.springframework.security:spring-security-test')
}
Process sync of build.gradle is successful.
When I run the following command.
sudo ./gradlew build buildDocker
I get the following error message.
Execution failed for task ':buildDocker'.
> Docker execution failed
Command line [docker push com.pai/pai201808:0.0.1-SNAPSHOT] returned:
Get https://com.pai/v2/: dial tcp: lookup com.pai on 127.0.1.1:53: no such host
I think that this could be the issue of wrong import or invalid configurated dockerfile. How can I solve this problem?
Gradle script tries to push docker image to repository which doesn't exist. Do you intend to do that? If not - then just set push = false.
If you want to push the image to some repository, then you have to setup it. You have to check plugin specific settings of pushing image to repository you want.
More info:
Creating new repository on Docker Hub, Setting up own registry
Related
I have a working Jenkinsfile that uses node and a small Makefile to build a small static web site.
pipeline {
agent {
docker {
image 'node'
}
}
stages {
stage('Build') {
steps {
script {
sh 'make'
}
}
}
}
}
But then I wanted to publish the static web site as a self-contained Docker image so that I could deploy it frozen in time with the site as it was on the day it was built. So I added a small Dockerfile:
FROM nginx:alpine
COPY . /usr/share/nginx/html
And I updated the Jenkinsfile
pipeline {
environment {
dockerCredentials = 'omitted'
dockerRegistryUrl = 'omitted'
imageName = "our.path.example.com/myproject"
}
agent {
docker {
image 'node'
}
}
stages {
stage('Build') {
steps {
script {
sh 'make'
app = docker.build(imageName, "-f site/Dockerfile site/dist/")
withDockerRegistry(credentialsId: dockerCredentials, url: dockerRegistryUrl) {
app.push('latest')
}
}
}
}
}
}
What happened next was unexpected. It doesn't like the docker build command. It says docker: not found. Apparently, the docker command is not installed.
I then attempted to apt/apk install it ran into access denied errors.
00:00:01.432 E: Could not open lock file /var/lib/dpkg/lock-frontend - open (13: Permission denied)
00:00:01.432 E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), are you root?
Sudo is also not available apparently in that "node" image. I tried other images as well. It seems that I am never allowed to install any dependencies into the docker image listed under agent.
I also tried using the scripted pipeline and attempted to install thing and was met with the same issue:
stage ('Install') {
sh 'apt install npm'
}
It seems that I cannot install anything.
How can I install the tools I need to build my application?
Iam trying to build a docker image for a Kotlin http4k backend but i cant get it quite working.
I can't create a fat jar so my dependencies are missing when i try to run the image.
So i get a ClassNotFound exception.
Here is my build.gradle file:
buildscript {
ext.kotlinVersion = "1.4.31"
ext.http4kVersion = "4.5.0.1"
repositories {
mavenCentral()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}"
}
}
repositories {
mavenCentral()
}
apply plugin: 'kotlin'
apply plugin: 'application'
compileKotlin.kotlinOptions.jvmTarget = "11"
compileTestKotlin.kotlinOptions.jvmTarget = "11"
mainClassName = 'com.scalangular.LauncherKt'
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
test {
useJUnitPlatform()
}
compileTestKotlin.kotlinOptions {
jvmTarget = "11"
}
dependencies {
implementation "org.http4k:http4k-contract:${http4kVersion}"
implementation "org.http4k:http4k-core:${http4kVersion}"
implementation "org.http4k:http4k-format-jackson:${http4kVersion}"
implementation "org.http4k:http4k-server-jetty:${http4kVersion}"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.4.30"
// testImplementation ...
}
jar {
manifest {
attributes 'Main-Class': mainClassName
}
}
And here is my Dockerfile:
FROM gradle:latest as builder
COPY --chown=gradle:gradle . /home/gradle/src
WORKDIR /home/gradle/src
RUN gradle build --no-daemon
FROM openjdk:latest
EXPOSE 9000
RUN mkdir /app
COPY --from=builder /home/gradle/src/build/libs/*.jar /app/ShoppingListApi.jar
CMD [ "java", "-jar", "-Djava.security.egd=file:/dev/./urandom", "/app/ShoppingListApi.jar" ]
I also tryed some gradle plugins which should enable me to build a fatjar but i didnt get it working.
You need to use the shadowjar plugin to create a FatJar. The easiest thing here is to use the http4k toolbox to generate a template project using Shadow and then just copy in the gradle magic from there: https://toolbox.http4k.org/
Hi I am using gradle palantir plugin in order to build a docker image from dockerfile
when i run the gw build docker its throwing Task 'docker' not found in project ':hop-service'.
when i ran gw tasks i m not able to see the docker tasks not sure what mistakes i have done build.gradle file below is my gradle file
buildscript {
repositories {
mavenLocal()
maven {
url 'https://artifactory.codetest.com/artifactory/sf-microservices-hop'
}
}
dependencies {
classpath "com.codetest.platform.foundation:platform-build-template:1.0.37.BUILD"
}
}
plugins {
id 'org.springframework.boot' version '2.1.7.RELEASE'
id 'io.spring.dependency-management' version '1.0.7.RELEASE'
id 'java'
id 'com.palantir.docker' version '0.22.1'
}
ext{
appProjects=['hop-service']
mapstructVersion = '1.3.1.Final'
annotationProcessVersion = '0.21'
}
apply from: project.buildscript.classLoader.getResource("scripts/build.gradle")
subprojects {
version = '1.0.0-SNAPSHOT'
group = 'com.xyz'
}
docker {
name "mycoolapp"
dockerfile file('Dockerfile')
copySpec.from(jar).rename(".*","app.jar")
buildArgs(['JAR_FILE': "app.jar"])
}
Dockerfile code
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
I have got a solution to this
Actually there are two build.gradle in project directory one is parent build gradle which i have mentioned above and another is child project build.gradle.
I have to mention these line of code in child project not in parent project after providing in child project build.gradle now i see docker tasks
plugins {
id 'org.springframework.boot' version '2.1.7.RELEASE'
id 'io.spring.dependency-management' version '1.0.7.RELEASE'
id 'java'
id 'com.palantir.docker' version '0.22.1'
}
docker {
name "mycoolapp"
dockerfile file('Dockerfile')
copySpec.from(jar).rename(".*","app.jar")
buildArgs(['JAR_FILE': "app.jar"])
}
Using the Gradle Docker plugin, I am trying to build a Docker image with a Dockerfile I already created. I got this far
plugins {
id 'com.bmuschko.docker-remote-api' version '6.4.0'
}
import com.bmuschko.gradle.docker.tasks.image.DockerBuildImage
import com.bmuschko.gradle.docker.tasks.image.DockerPushImage
task dockerBuildImage(type: DockerBuildImage) {
inputDir = projectDir
images.add('trajano/cloud-auth')
}
task dockerPushImage(type: DockerPushImage) {
dependsOn dockerBuildImage
images.add('trajano/cloud-auth')
}
task build {
dependsOn tasks.dockerPushImage
}
I am getting this error though
Building image using context 'D:\p\spring-cloud-demo\cloud-auth'.
Using images 'trajano/cloud-auth'.
Error during callback
com.bmuschko.gradle.docker.shaded.org.apache.http.conn.HttpHostConnectException: Connect to 127.0.0.1:2375 [/127.0.0.1] failed: Connection refused: connect
In the end I used the command line to do the build, here's my build.gradle file where I commented out the plugin code. It solves my immediate problem for the builds, but it is more ideal than exposing 2375.
plugins {
// id 'com.bmuschko.docker-remote-api' version '6.4.0'
}
//import com.bmuschko.gradle.docker.tasks.image.DockerBuildImage
//import com.bmuschko.gradle.docker.tasks.image.DockerPushImage
task dockerBuildImage(type: Exec) {
// inputDir = projectDir
// images.add('trajano/cloud-auth')
commandLine "docker", "build", ".", "-t", "trajano/cloud-auth"
}
task dockerPushImage(type: Exec) {
dependsOn dockerBuildImage
commandLine "docker", "push", "trajano/cloud-auth"
}
task build {
dependsOn tasks.dockerPushImage
}
I am trying to migrate from ant to gradle to build my rpm-package. I found one plugin gradle-ospackage-plugin, but I am not able to understand how to use any of commands which I have in ant task: specFile, topDir, command, cleanbuildDir, failonError
Is it not possible to have them in gradle?
Update: Basically I am trying to replicate the following in gradle
<target name="myrpm">
<rpm specFile = "topdir"
topDir = "topdir" />
</target>
I was also looking into running ant tasks from gradle such as ant.echo(message: "hello).
But ant.rpm is not resolving.
A simple example that creates two RPMs. If you want a single RPM then use one task. Your build.gradle file should be like this.
plugins {
id 'java'
id "nebula.ospackage" version "3.2.0"
}
task one(type: Rpm) {
packageName = 'one-pack'
version = '1.0.0'
release = '1'
arch = I386
os = LINUX
from('./src/main/resources') {
into 'apps/lib/'
}
}
task two(type: Rpm) {
packageName = 'two-pack'
version = '2.2.0'
release = '2'
arch = I386
os = LINUX
}
apply plugin: 'nebula.ospackage'
group 'com.avishek'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile 'org.assertj:assertj-core:3.2.0'
}
Run the project with the following command:
./gradlew clean two one