DEV Community

Gustavo Jhon
Gustavo Jhon

Posted on

Project Java Web with docker, maven

sudo apt-get install default-jdk
sudo apt install maven
mvn archetype:generate -DgroupId=com.example -DartifactId=my-spring-project -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

cd my-spring-project
Enter fullscreen mode Exit fullscreen mode
<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>my-spring-project</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>my-spring-project</name>
  <url>http://maven.apache.org</url>
  <properties>
    <maven.compiler.source>1.6</maven.compiler.source>
    <maven.compiler.target>1.6</maven.compiler.target>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.2.0</version>
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <classpathPrefix>lib/</classpathPrefix>
              <mainClass>com.example.App</mainClass> <!-- Reemplaza con la ruta de tu clase principal -->
            </manifest>
          </archive>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>
Enter fullscreen mode Exit fullscreen mode

Dockerfile

# Imagen base de Java
FROM openjdk:11

# Directorio de trabajo dentro del contenedor
WORKDIR /app

# Copiar el JAR o WAR de la aplicación a la carpeta de trabajo
COPY target/my-spring-project-1.0-SNAPSHOT.jar /app

# Exponer el puerto en el que se ejecuta la aplicación Spring
EXPOSE 7070

# Comando para ejecutar la aplicación
CMD ["java", "-jar", "my-spring-project-1.0-SNAPSHOT.jar"]
Enter fullscreen mode Exit fullscreen mode

image

docker build -t spring .
Enter fullscreen mode Exit fullscreen mode

run

docker run -p 7070:7070 spring
Enter fullscreen mode Exit fullscreen mode

Top comments (0)