Building Docker Images – Maven

Summary:
Step 1: Create a Dockerfile Step 2: Build the image Step 3: List the images: Step 4: Tag the images Step 5: Login to docker Step 6: Push the Image

Detailed Steps:
After creating your web application till Controller Layer,
Step 1: Add Dockerfile(file) in src -> main -> Dockerfile

FROM java:8
VOLUME /tmp
ADD lmsapp-web-0.0.1-SNAPSHOT.jar app.jar
ADD setenv.sh setenv.sh
ADD setenv2.sh setenv2.sh
CMD ./setenv.sh
RUN bash -c ‘touch /app.jar’
ENTRYPOINT [“java”,”-Djava.security.egd=file:/dev/./urandom”,”-jar”,”/app.jar”]

Step 2: Changes in properties of pom.xml

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<docker.image.prefix>lmsapp</docker.image.prefix>
	</properties>

Step 3: Changes in plugin of pom.xml

<plugin>
				<groupId>com.spotify</groupId>
				<artifactId>docker-maven-plugin</artifactId>
				<version>0.4.13</version>
				<configuration>
					<serverId>docker-hub</serverId>
					<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
					<dockerDirectory>src/main/docker</dockerDirectory>
					<resources>
						<resource>
							<targetPath>/</targetPath>
							<directory>${project.build.directory}</directory>
							<include>${project.build.finalName}.jar</include>
						</resource>
					</resources>
				</configuration>
			</plugin>

Note : After changes, pom.xml looks like,

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.lmsapp</groupId>
	<artifactId>lmsapp-web</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>lmsapp-web</name>
	<description>leave management system</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.4.3.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<docker.image.prefix>lmsapp</docker.image.prefix>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>


		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-dbcp2</artifactId>

		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>6.0.5</version>
		</dependency>



	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<plugin>
				<groupId>com.spotify</groupId>
				<artifactId>docker-maven-plugin</artifactId>
				<version>0.4.13</version>
				<configuration>
					<serverId>docker-hub</serverId>
					<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
					<dockerDirectory>src/main/docker</dockerDirectory>
					<resources>
						<resource>
							<targetPath>/</targetPath>
							<directory>${project.build.directory}</directory>
							<include>${project.build.finalName}.jar</include>
						</resource>
					</resources>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>

Step 4: Right click project -> Run as -> Maven install

Step 5: Right click project -> Run as -> Maven Build(2nd option)

Step 6: In goals, type -> clean package docker:build -> Click Run

Step 7: Once BUILD SUCCESS, open command prompt and execute,

C:\Users\EP>docker images

C:\Users\EP>docker system df

C:\Users\EP>docker system prune

C:\Users\EP>docker run -p 8080:8080 -t lmsapp/lmsapp-web

Step 8: Access localhost:8080/employees/(url returning JSON)

Step 9: To list containers,
C:\Users\EP>docker ps
Note the Container Id(525ed17a65d3)

Step 10: To stop one or more containers,
C:\Users\EP>docker stop 525ed17a65d3

Step 11: To remove one or more containers,
C:\Users\EP>docker rm 525ed17a65d3

Step 12: C:\Users\EP>docker tag 525ed17a65d3 jananiv/lmsapp-web:latest

Step 13: C:\Users\EP>docker login

Step 14: docker push jananiv/lmsapp-web

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s