简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM