Reputation: 1483
How to translate this bash command to Dockerfile?
cp $(stack exec which wai-crowd) /app
To
COPY $(stack exec which wai-crowd) /app
I want to copy the executable to a more convenient place to be the entry point. I might have a different base image for build and execute:
FROM fpco/stack-build:lts-14.20 as builder
COPY . /app
WORKDIR /app
RUN stack build
FROM fpco/stack-build:lts-14.20
WORKDIR /app
COPY --from=builder $(stack exec which wai-crowd) /app
EXPOSE 3000
ENTRYPOINT ["./wai-crowd"]
Upvotes: 0
Views: 1234
Reputation: 159403
For a couple of reasons, you need to run this step during the "build" stage. That will leave the file in a known place where the "runtime" stage can COPY --from=build
it.
FROM fpco/stack-build:lts-14.20 as builder
...
RUN stack build
RUN cp $(stack exec which wai-crowd) /app
FROM alpine
COPY --from=builder /app/wai-crowd /usr/bin
EXPOSE 3000
CMD ["wai-crowd"]
When you start the second stage, it doesn't have any of the content from the first stage, so even ignoring the syntactic problems, the stack exec which ...
command wouldn't be able to see the build tree. Typically using a compiled language (like Haskell) you will want to avoid having the actual build tools in your final image, since they can be quite large; in my example I've used the lightweight alpine
image as the base for the final image, and so you also won't have the stack
command there.
The solution here is to leave any artifacts that you do want to copy into the final image in fixed paths. In my example I've reused the same /app
directory that the source is in but you can use pretty much any directory you want.
Upvotes: 1
Reputation: 42511
I believe not all shell commands can be translated to dockerfile.
Dockerfile is a script that builds the docker image. COPY
command takes some file from the host machine (on which you build the image) and copies it into the docker image so that this file will later be available for docker process.
AFAIK, docker doesn't have any tools to run commands in the host machine during the docker build (see also this thread)
So you can run the command before the build, create a file in the buildroot and then run docker build
immediately after that.
Upvotes: 1