skip to Main Content

So, the image I uploaded to the conter does not display.

Here are the steps I took, and I’ll add right away that I used gradle.

  1. I downloaded the Docker plugin to IntelliJ
  2. I created a Dockerfile in which I put:
  1. In the terminal, I typed the following commands to build the project:

  2. Then I typed (to create an image and display it):

  3. And here is where the problem arises, I entered the following commands:

    (I want to run on port 8081 because my app listens on port 8080. I don’t know if it matters but that’s how I did it)

I display the list using command: docker container ps

I have no idea what I’m doing wrong that it’s not working, where I’m making a mistake. Is there something else I should do? In general, the spring boot application at this time should be running? What does it look like, I read some tutorials but unfortunately nothing helped, and I was doing the same way as them

3

Answers


  1. You can also create a Docker image with Maven plugin by defining in pom.xml and run ‘mvn install’:

    <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <image>
                            <name>
                                ${project.groupId}/twitter.to.kafka.service:${project.version}
                            </name>
                        </image>
                    </configuration>
                    <executions>
                        <execution>
                            <!-- Create Docker image during mvn install -->
                            <phase>install</phase>
                            <goals>
                                <!-- Spring Boot use the Layered approach : to prevent creating a single fat jar, and use caching during image update -->
                                <goal>
                                    build-image
                                </goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    

    Can you try this instead of manually creating a Dockerfile ?

    Login or Signup to reply.
  2. try to run:

    docker run -p 8081:8081 springreadyapp
    

    i add a link to my repo which explain about that, in maven docker with spring

    Login or Signup to reply.
  3. A less laborious solution is using Jib under GoogleContainerTools. It is available as a Gradle plugin.

    Jib builds optimized Docker and OCI images for your Java applications
    without a Docker daemon – and without deep mastery of Docker
    best-practices.

    There are alternative options, here are the steps for using Jib Gradle plugin to build a container image and run it:

    1. Add the jib plugin to build.gradle:

      plugins {
        id 'com.google.cloud.tools.jib' version '3.2.1'
      }
      
    2. Customize the image build in build.gradle:

      Since your app listens on port 8080, the 8080 port on the container will need to be exposed

       jib.from {
           image = <YOUR BASE IMAGE>
       }
       jib.container {
           entrypoint = <YOUR CONTAINER ENTRY POINT>
           ports = <EXPOSED CONTAINER PORTS. Eg: ['8080']>
       }
       jib.to{
           image = <OUTPUT IMAGE. Eg: myapp:latest>
       }
      
    3. Build image from the command line:

      Open a command line under the root of your project

      3.1. Build image tarball:

      gradle jibBuildTar
      

      This builds and saves your image to build/jib-image.tar

      3.2. Load the image tarball into docker:

      docker load --input build/jib-image.tar
      
    4. Run image as a container from the command line:

      docker run <OUPUT IMAGE> -p <HOST PORT>:<EXPOSED CONTAINER PORT>
      

      Eg:

      docker run myapp -p 8081:8080
      

      Here, the 8080 port on the container is mapped to the 8081 port on the host (You can also map it to the 8080 port on the host if you want, the container has its own IP address separate from the host).

    More details can be found in the jib-gradle-plugin Quickstart

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search