Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy file to a container in docker

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

like image 697
Nesan Mano Avatar asked Dec 01 '25 08:12

Nesan Mano


1 Answers

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

like image 105
earlonrails Avatar answered Dec 04 '25 09:12

earlonrails



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!