One of things we do often is to package all source code in Dockerfile
when we build a Docker image.
ADD . /app
How can we avoid including the .git
directory in simple way ?
I tried the Unix way of handling this using ADD [^.]* /app/
Complete sample:
docker@boot2docker:/mnt/sda1/tmp/abc$ find . . ./c ./.git ./Dockerfile ./good ./good/a1 docker@boot2docker:/mnt/sda1/tmp/abc$ cat Dockerfile FROM ubuntu ADD [^.]* /app/ docker@boot2docker:/mnt/sda1/tmp/abc$ docker build -t abc . Sending build context to Docker daemon 4.096 kB Sending build context to Docker daemon Step 0 : FROM ubuntu ---> 04c5d3b7b065 Step 1 : ADD [^.]* /app/ d ---> 5d67603f108b Removing intermediate container 60159dee6ac8 Successfully built 5d67603f108b docker@boot2docker:/mnt/sda1/tmp/abc$ docker run -it abc root@1b1705dd66a2:/# ls -l app total 4 -rw-r--r-- 1 1000 staff 30 Jan 22 01:18 Dockerfile -rw-r--r-- 1 root root 0 Jan 22 01:03 a1 -rw-r--r-- 1 root root 0 Jan 22 00:10 c
And secondly, it will lose the directory structure, since good\a1
gets changed to a1
.
Related source code in Docker is https://github.com/docker/docker/blob/eaecf741f0e00a09782d5bcf16159cc8ea258b67/builder/internals.go#L115
Easy ! Just use the WORKDIR command to change the directory you want to. Any other commands you use beyond this command will be executed in the directory you have set. It is also a better practice to make use of WORKDIR in docker.
WORKDIR instruction is used to set the working directory for all the subsequent Dockerfile instructions. Some frequently used instructions in a Dockerfile are RUN, ADD, CMD, ENTRYPOINT, and COPY. If the WORKDIR is not manually created, it gets created automatically during the processing of the instructions.
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.
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.
You may exclude unwanted files with the help of the .dockerignore file
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With