skip to Main Content

My Dockerfile is

FROM alpine
RUN apk update && apk add --no-cache curl ca-certificates unzip coreutils bash
WORKDIR /
COPY build/current/database .
COPY build/current/dist .
COPY build/current/queries .
COPY build/current/resources .
COPY build/current/storage .
COPY build/current/.env .
COPY build/current/app .
ENTRYPOINT ["./app artisan migrate"]
CMD ["./app"]

But it’s unable to run and gives following error:

Error response from daemon: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "./app artisan migrate": stat ./app artisan migrate: no such file or directory: unknown

When using following,

FROM alpine
RUN apk update && apk add --no-cache curl ca-certificates unzip coreutils bash
WORKDIR /
COPY build/current/database .
COPY build/current/dist .
COPY build/current/queries .
COPY build/current/resources .
COPY build/current/storage .
COPY build/current/.env .
COPY build/current/app .
ENTRYPOINT ./app artisan migrate
CMD ./app

I get error in docker logs

/bin/sh: line 0: ./app: not found
/bin/sh: line 0: ./app: not found
/bin/sh: line 0: ./app: not found
/bin/sh: line 0: ./app: not found
/bin/sh: line 0: ./app: not found
/bin/sh: line 0: ./app: not found
/bin/sh: line 0: ./app: not found

Please suggest there’s in mistake in my approach and suggest some way to fix the issue

enter image description here

2

Answers


  1. When you COPY build/current/database into your docker container, you are only copying the content of the directory /database.

    In you case, you are copying all the content of your directories into the root of the container. In the end, /database doesn’t exists in the container. Same for the others

    To solve your issue, you can try to COPY the content of build/current into the container.

    Here is a Dockerfile you can try :

    FROM alpine
    RUN apk update && apk add --no-cache curl ca-certificates unzip coreutils bash
    
    COPY build/current .
    WORKDIR /
    
    ENTRYPOINT ./app artisan migrate
    CMD ./app
    

    EDIT : I see you are trying to copy a build folder. A good practice is to build while you are creating the image. Not doing this can result in bugs due to a change of environment.
    You can try something like this and see if it works :

    FROM alpine as build #use base image of the langage you are using (like python/java o whatever)
    WORKDIR /temp
    COPY . .
    ### Create your build directory
    RUN build # or other command you use to build your project
    
    FROM alpine
    RUN apk update && apk add --no-cache curl ca-certificates unzip coreutils bash
    
    WORKDIR /app
    
    COPY --from=build /temp/build /app
    
    ENTRYPOINT ./app artisan migrate
    CMD ./app
    
    Login or Signup to reply.
  2. There are multiple issues with your Dockerfile:

    1. Using COPY <src> . with WORKDIR / will copy all files into the root directory of your image with clutters the root directory and may even conflict with existing files of the base image. You should really use WORKDIR /app or WORKDIR /code or something like that.

    2. The COPY build/current/database . directive etc. will only copy the directory contents, not the directory itself, i.e., the contained file get copied directly into / and not into a subdirectory /database/
      See the COPY documentation on this:

    If <src> is a directory, the entire contents of the directory are copied, including filesystem metadata.

    1. As others already pointed out in the comments: if ./app is a binary you need to make sure that all linked libraries are also available within the image.
      Instead of building the binary on the host I would recommend you to rather use multi-stage builds.

    2. ENTRYPOINT ["./app artisan migrate"] results in an error:

    Error response from daemon: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "./app artisan migrate": stat ./app artisan migrate: no such file or directory: unknown

    This because you are using the exec form of ENTRYPOINT where the first string in the array is the name of the executable and all following strings are the arguments. But since you only used a one string docker is trying to execute a single file named "./app artisan migrate", i.e., a file with two spaces in it – which does not exist.

    Instead what you should have used is

    ENTRYPOINT ["./app", "artisan", "migrate"]
    
    # but this works as well, see the _shell_ form of ENTRYPOINT
    ENTRYPOINT ./app artisan migrate
    
    1. Your use of CMD does not do what you seem to expect:
    ENTRYPOINT ./app artisan migrate
    CMD ./app
    

    This does not first execute the migrations and then start ./app!

    Instead the provided CMD is added as arguments to the ENTRYPOINT, i.e., what docker is executing to start your container is

    ./app artisan migrate ./app
    

    Read the documentation on ENTRYPOINT and CMD!

    If you want to ensure that ./app artisan migrate is always run before any provided command, you can use a custom entrypoint script. For example:

    #!/bin/sh
    
    # first execute the migrations
    ./app artisan migrate
    
    # then exec the command that was specified
    exec "$@"
    

    and use it with

    FROM alpine
    
    # ...
    
    COPY entrypoint.sh .
    ENTRYPOINT ["./entrypoint.sh"]
    CMD ./app
    

    With this by default docker will run ./app after the migrations and alternatively you can specify another command on the command line:

    docker run yourimage ./app othercommand
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search