spring-guides / gs-spring-boot-docker

Spring Boot with Docker :: Learn how to create a Docker container from a Spring Boot application with Maven or Gradle

Home Page:https://spring.io/guides/gs/spring-boot-docker/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to connect MySQL docker container with spring boot app image for production ?

DILEEP-YADAV opened this issue · comments

Hi! I want to run the same spring boot app on my laptop at the time of development with connecting my own installed MySQL and in production with MySQL docker container. I tried more but getting the issue with JDBC connection exception. Please help me.

I am using two files same project for that.

# application.properties for local MySQL connection

spring.datasource.platform=mysql
spring.datasource.driverClassName=com.mysql.jdbc.Driver

spring.datasource.url= jdbc:mysql://localhost:3306/tsbacked?useSSL=false
spring.datasource.username = root
spring.datasource.password = 12345
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

# application.yml for Docker container MySQL

spring:
    jpa:
        database: MYSQL
        hibernate:
            ddl-auto: validate

    datasource:
        url: jdbc:mysql://mysql:3306/tsbacked
        username: root
        password: 12345
        driver-class-name: com.mysql.jdbc.Driver

# Dockerfile

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD target/techlaviya-0.0.1.jar app.jar
ENV JAVA_OPTS=""
ENTRYPOINT exec java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar

docker run --name demo-mysql -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=tsbackedMYSQL_USER=root-e MYSQL_PASSWORD=12345 -d mysql:5.6

It's happened the same to me. I tried to use the --link docker parameter when running my app but continue not working. Do you already found any solution? Thanks

Ohhh I found the solution. You can read this tutorial: https://g00glen00b.be/docker-spring-boot/. The problem is your application load the application.properties.
You need to change the ENTRYPOINT to load the profile with the container configuration (application.yml). E.g "-Dspring.profiles.active=container"

spring:
    profiles: container
    jpa:
        database: MYSQL
        hibernate:
            ddl-auto: validate

    datasource:
        url: jdbc:mysql://mysql:3306/tsbacked
        username: root
        password: 12345
        driver-class-name: com.mysql.jdbc.Driver

thanks

docker run -p 8080:8080 -t 770727c11311
Error: Invalid or corrupt jarfile /app.jar

I am using the following sample app to make my own docker image
https://github.com/spring-guides/gs-spring-boot-docker.git

docker image builds successfully. and working fine with my laptop with docker terminal for windows 10.
but the same image pushed to gitlab docker registry. and using my VPS Centos server. it is getting the same error. Please help me. Or even pulling to same docker image to docker terminal for windows 10. getting the same error.
error

Could be a lot of problems...you can find many problem related to "INVALID OR CORRUPT JARFILE" on the internet!
I recommend extracting the jar file (downloaded) and compare to the files with the jar generated locally. (You can compare the size of jar).

Can you show me your dockerfile, please?

FROM openjdk:8-jre
VOLUME /tmp
ARG JAR_FILE
ADD ${JAR_FILE} techlaviya.jar
RUN bash -c 'touch /techlaviya.jar'
EXPOSE 8080
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-Dspring.profiles.active=container","-jar","/techlaviya.jar"]

version: '3'

services:
docker-mysql:
image: mysql:latest
environment:
- MYSQL_ROOT_PASSWORD=12345
- MYSQL_DATABASE=TechlaviyaDB
- MYSQL_PASSWORD=12345

phpmyadmin:
image: phpmyadmin/phpmyadmin
links:
- docker-mysql
environment:
PMA_HOST: docker-mysql
PMA_PORT: 3306
ports:
- '8081:80'
docker-webapp:
image: techlaviya/techlaviya:0.0.1-SNAPSHOT
depends_on:
- docker-mysql
ports:
- 8080:8080
environment:
- DATABASE_HOST=docker-mysql
- DATABASE_USER=root
- DATABASE_PASSWORD=12345
- DATABASE_NAME=TechlaviyaDB
- DATABASE_PORT=3306


4.0.0

<groupId>com.tech.techlaviya</groupId>
<artifactId>techlaviya</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>techlaviya</name>
<description>Techlaviya Rest API Project</description>

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>1.5.9.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>
	<spring-cloud.version>Dalston.RELEASE</spring-cloud.version>
	<start-class>com.tech.techlaviya.TechlaviyaApplication</start-class>
	<docker.image.prefix>techlaviya</docker.image.prefix>
</properties>

<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-data-jpa</artifactId>
	</dependency>

	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-devtools</artifactId>
		<scope>runtime</scope>
	</dependency>
	<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
	</dependency>

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

	<dependency>
		<groupId>com.jayway.jsonpath</groupId>
		<artifactId>json-path</artifactId>
		<scope>test</scope>
	</dependency>


	<dependency>
		<groupId>io.springfox</groupId>
		<artifactId>springfox-swagger2</artifactId>
		<version>2.4.0</version>
	</dependency>
	<dependency>
		<groupId>io.springfox</groupId>
		<artifactId>springfox-swagger-ui</artifactId>
		<version>2.4.0</version>
	</dependency>


	<dependency>
		<groupId>io.jsonwebtoken</groupId>
		<artifactId>jjwt</artifactId>
		<version>0.6.0</version>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-security</artifactId>
	</dependency>
	<dependency>
		<groupId>com.fasterxml.jackson.core</groupId>
		<artifactId>jackson-core</artifactId>
		<version>2.8.9</version>
	</dependency>

	<dependency>
		<groupId>org.springframework.mobile</groupId>
		<artifactId>spring-mobile-device</artifactId>
	</dependency>

	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-data-rest</artifactId>
	</dependency>

</dependencies>
<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-dependencies</artifactId>
			<version>${spring-cloud.version}</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement>

<build>
	<finalName>techlaviya</finalName>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
		</plugin>
		<plugin>
			<artifactId>maven-jar-plugin</artifactId>
			<configuration>
				<archive>
					<manifest>
						<addClasspath>true</addClasspath>
						<classpathPrefix>lib/</classpathPrefix>
						<mainClass>com.tech.techlaviya.TechlaviyaApplication</mainClass>
					</manifest>
				</archive>
			</configuration>
		</plugin>
		<!-- tag::plugin[] -->
		<plugin>
			<groupId>com.spotify</groupId>
			<artifactId>dockerfile-maven-plugin</artifactId>
			<version>1.3.6</version>
			<executions>
				<execution>
					<id>default</id>
					<goals>
						<goal>build</goal>
						<goal>push</goal>
					</goals>
				</execution>
			</executions>
			<configuration>
				<repository>${docker.image.prefix}/${project.artifactId}</repository>
				<tag>${project.version}</tag>
				<buildArgs>
					<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
				</buildArgs>
			</configuration>


		</plugin>
		<!-- end::plugin[] -->
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-dependency-plugin</artifactId>
			<executions>
				<execution>
					<id>unpack</id>
					<phase>package</phase>
					<goals>
						<goal>unpack</goal>
					</goals>
					<configuration>
						<artifactItems>
							<artifactItem>
								<groupId>${project.groupId}</groupId>
								<artifactId>${project.artifactId}</artifactId>
								<version>${project.version}</version>
							</artifactItem>
						</artifactItems>
					</configuration>
				</execution>
			</executions>
		</plugin>
	</plugins>
</build>

Running with a database is out of scope for this guide. If you want to see some guidance, maybe open an issue in the extended topical guide.