简体   繁体   中英

How to copy files inside container with docker-compose

I have a simple image that runs a jar file. That jar file inside the image needs a special configuration file in order to run.

In the location with the docker-compose.yml I have a folder named "carrier" and under this folder I have that file.

The docker-compose.yml:

version: "3.3"
services:
    web:
        image: "myimage:1.80.0.0"
        ports:
            - "61003:61003"
        volumes:
            - ./carrier:/var/local/Config/
    

When I hit docker-compose up it complains that the file is not there, so it doesn't copy it. If I do another option like I did in the.sh command, something like this:

       volumes:
            - ./carrier:/var/local/Config/:shared

It complains about another error:

C:\Tasks\2246>docker-compose up
Removing 2246_web_1
Recreating 1fbf5d2bcea4_2246_web_1 ... error

ERROR: for 1fbf5d2bcea4_2246_web_1  Cannot start service web: path /host_mnt/c/Tasks/2246/carrier is mounted on / but it is not a shared mount
        

Can someone please help me?

Remove the last /

volumes:
  - ./carrier:/var/local/Config

You can use Dockerfile if it does not copy.

Dockerfile;

FROM image

COPY files /var/local/Config/

EXPOSE 61003

Docker-compose;

version: "3.3"
services:
    web:
        build: . (path contains Dockerfile)
        ports:
            - "61003:61003"
        volumes:
            - ./carrier:/var/local/Config/

I'm not sure but you can try to set full access permissions for all user to /carrier:

chmod -R 777 /carrier

Thanks all for all your answers. Seems like finally docker warned me with some comparisons over the windows files vs Linux files when building the image. (Not with docker compose but with Dockerfile).

SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.

Tried it on linux and works.

Copy the files using Dockerfile, use below;

FROM myimage:1.80.0.0
RUN mkdir -p /var/local/Config/
COPY carrier /var/local/Config/
EXPOSE 61003

docker-compose.yml

version: "3.3"
services:
  web:
    build:
      dockerfile: Dockerfile
      context: '.'
   ports:
    - "61003:61003"

In the end, run below command to build new image and start container

  • docker-compose up -d --build

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