Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker: In Dockerfile, copy files temporarily, but not for final image

Tags:

docker

I have a Java service I'd like to package, and the only thing the final docker image needs is the JAR file and a config file. However, I need to run my gradle command first to build the JAR, but I don't want all the things that gradle uses to be in the result docker image.

Here's my current DockerFile:

RUN apt-get update && apt-get install -y openjdk-7-jdk
COPY . /
RUN ./gradlew shadowJar
CMD ["java", "-jar", "service/build/libs/service.jar", "server", "service/service.yml"]

You can see I have to COPY everything first so that I can run ./gradlew (otherwise it says the command cannot be found). But in the end, all I need is the service.jar and service.yml files.

I'm probably missing something, but how I can make everything available during the ./gradlew build step, but only have the result image include the service.jar and service.yml.

like image 774
Sean Adkinson Avatar asked Oct 10 '14 18:10

Sean Adkinson


People also ask

What are the differences between COPY and add directives in Dockerfile?

COPY is a docker file command that copies files from a local source location to a destination in the Docker container. ADD command is used to copy files/directories into a Docker image. It only has only one assigned function. It can also copy files from a URL.

How does Workdir work in Dockerfile?

The WORKDIR command is used to define the working directory of a Docker container at any given time. The command is specified in the Dockerfile. Any RUN , CMD , ADD , COPY , or ENTRYPOINT command will be executed in the specified working directory.

Does Dockerfile need Workdir?

According to the documentation: The WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile. If the WORKDIR doesn't exist, it will be created even if it's not used in any subsequent Dockerfile instruction.

What is difference between CMD and entrypoint?

Differences between CMD & ENTRYPOINT CMD commands are ignored by Daemon when there are parameters stated within the docker run command while ENTRYPOINT instructions are not ignored but instead are appended as command line parameters by treating those as arguments of the command.


2 Answers

Docker introduced so called "multi stage builds". With them, you can avoid having unnecessary files in your image. Your Dockerfile would look like this:

FROM ubuntu AS build
COPY . /src
# run your build

FROM ubuntu
RUN mkdir -p /dist/
COPY --from=build /src/my.object /dist/

The principle is simple. In the first FROM you name your build. In the second FROM you can use the COPY --from=-parameter to copy files from your first build to your second. The second build is the one which results in an usable image later.

If you want to test the results of your build instead the resulting image, you can use docker build --target build myimage:build .. The resulting image only includes the first FROM in your Dockerfile.

For more information: https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds

like image 160
Trendfischer Avatar answered Oct 17 '22 02:10

Trendfischer


There is an experimental build flag --squash to summarize everything into one layer.

For example, docker build --squash -t <tag> .

The documentation https://docs.docker.com/engine/reference/commandline/image_build/#options.

The Github discussion https://github.com/moby/moby/issues/332.

How to enable experimental features in the daemon? https://github.com/docker/docker-ce/blob/master/components/cli/experimental/README.md

like image 25
Phizaz Avatar answered Oct 17 '22 01:10

Phizaz