skip to Main Content

I try build online compiler c++. This my dockerfile:

FROM alpine:latest

COPY . .
RUN apk update 
&& apk add build-base 
&& apk add g++


RUN [ "chmod","+x", "./judge.sh" ]
ENTRYPOINT [ "./judge.sh" ]

I write judge.sh file this for show what happening:

#!/bin/sh

echo 1

After successfully building image I run this command: docker run -v $(pwd)/judge.sh test-judge or docker run test-judge this command, I get this same error:

docker: Error response from daemon: cannot mount volume over existing file, file exists /var/lib/docker/overlay2/b064ecb567a905938f02177dcc73f8532556d24abb87dc10cd491cb5406548c6/merged/judge.sh

How to fix this error? And how to use judge.sh file in docker volume?

2

Answers


  1. You have COPY . . instruction in your dockerfile, which will copy all the contents where dockerfile is located, inside your image, including file judge.sh.

    Now, when you run a container, it already has this judge.sh file inside it and you are also trying to mount the same file from outside the container and hence you are getting the error of

    cannot mount volume over existing file, file exists

    Any reason why you need to both copy AND mount ?

    Login or Signup to reply.
  2. volumes and mounts are used on directories that contain files, not a single file.

    in the Dockerfile, set the workdir before copying

    FROM alpine:latest
    
    WORKDIR /app
    COPY . /app/
    RUN apk update 
    && apk add build-base 
    && apk add g++
    
    
    RUN [ "chmod","+x", "./judge.sh" ]
    ENTRYPOINT [ "./judge.sh" ]
    

    to run the copied judge.sh:

    docker run test or docker run test ./judge.sh

    to bind a directory with custom scripts

    docker run -it -v $(pwd):/app test ./judge.sh or docker run -it -v $(pwd):/app test ./custom_script.sh

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search