1

I have a docker-compose and a docker file.

In the docker file, I have COPY instructions where I specify

The problem is, I have a new JAR file in the same folder as my dockerfile but it keeps copying the older jar. I have no idea of where it is getting the older jar. I have made sure to have only one jar.

COPY ./xxxx.jar /home/dev/xxxxd.jar

Nesan Mano
  • 1,892
  • 2
  • 26
  • 43
  • can you build the image with no cache docker-compose build --no-cache – Gonzales Gokhan Dec 17 '20 at 21:49
  • Exactly, I have found the culprit. the images were not rebuilt but rather taken from a previous build. I don't if we could force the images to be rebuild or delete the images when the containers are deleted – Nesan Mano Dec 17 '20 at 22:43

2 Answers2

0

So let's say your Dockerfile looks like this:

FROM openjdk:8
COPY ./xxxx.jar /home/dev/xxxxd.jar
CMD ["java", "-jar", "/home/dev/xxxxd.jar"]

You have some folder for your project like

project/
    Dockerfile
    docker-compose.yml
    xxxx.jar
    old-xxxx.jar

You can make your docker-compose.yml look like this:

version: "3.4"
services:
  project:
    build: .
    volumes:
      - ./:/home/dev

This line will copy the jar to the image at build time

COPY ./xxxx.jar /home/dev/xxxxd.jar

This docker-compose volume part is going to run docker container similar to using the following docker run command.

docker run -v ./:/home/dev project 

This will mount your project folder to the /home/dev before running. Which should have your most recent jar which is the same jar as in your project folder.

If you are noticing something unusual you can try what I do when things go wrong.

docker-compose up --build

and if all else fails:

docker-compose stop && docker-compose rm -sfv && docker-compose up

Similar issues:

How do I mount a host directory as a volume in docker compose

Auto remove container with docker-compose.yml

earlonrails
  • 4,966
  • 3
  • 32
  • 47
0

Docker might be using the cache layer. That's why the file doesn't change. Try to rebuild the image:

docker-compose build --no-cache

or, for building and running all together:

docker-compose up --build

Also, for those who want to copy a file from or to a container for a one-off task like testing you can use:

docker cp host_source_path container:destination_path

or

docker cp container:source_path host_destination_path
Maicon Mauricio
  • 2,052
  • 1
  • 13
  • 29