1

I'm trying to figure out the docker for the hundredth time. Now I create spring-boot simple application and create docker image for it:

FROM openjdk:8
EXPOSE 8080
ADD /build/libs/hello-docker-0.0.1-SNAPSHOT.jar hello-docker.jar
ENTRYPOINT ["java", "-jar", "hello-docker.jar"]

What did i do step by step:

1) Build my app(as a result, my hello-docker-0.0.1-SNAPSHOT.jar appeared in the build folder)

2) moved to the directory where the Docker file is located and executed the command: docker build -f Dockerfile -t springdocker .

3) Run this image as container: docker run -p 8080:8080 springdocker

As a result, my application started successfully.

But now I decided to change something in my application. I did it and tried to repeat all the steps. 1-> 2-> error

On 2 stepa new image was created and the old one was in the active state. It turns out that I need to stop the old one and start a new one.

it is not comfortable. I intuitively understand that this is not how it should work. I do not understand how to properly update the container. I constantly change my application and if I need to stop the container then create an image and then launch it - this is not a convenience but a complication. With the same success, I can stop the my application and launch a new one without a docker. I understand that I do not understand how this works, and I want someone to explain to me how to do it.

ip696
  • 6,574
  • 12
  • 65
  • 128
  • How about using `docker exec`? – Mayur Jan 31 '19 at 09:58
  • 1
    The setup you describe `docker stop; docker rm; docker build; docker run` is absolutely the standard "iterate developing for Docker" sequence. I would develop your application normally using your host's JVM, and look into Docker mostly as a fairly late-stage packaging/deployment solution. – David Maze Jan 31 '19 at 11:54

2 Answers2

2

You can use docker-compose, this will save you a lot of time.

Just write a docker-compose.yml file in the same folder as Dockerfile:

version: '3.5'
services:
  yourapplication:
    build: . -> this is the location of your dockerfile
    image: yourapplication_imagename
    container_name: yourapplication_containername
    ports:
       - "8080:8080"
    tty: true

And, to update the application you only have to run:

docker-compose build --no-cache yourapplication
docker-compose up -d

This will recreate the image and replace the container with your updated app.

Without docker-compose: you already have a good thread about this here

andre-lx
  • 312
  • 3
  • 14
0

You should automate your build process. If the desired result of this process is a docker image, your automated build process should not stop after creating the .jar

Fortunately, there is a good tutorial for building a Docker image for running a Spring Boot application using different build tools (Maven, Gradle): https://spring.io/guides/gs/spring-boot-docker/

In the end, you will be able to deploy your application to a docker image using just

./mvnw install dockerfile:build
Thomas Kainrad
  • 2,542
  • 21
  • 26