Using Dockerfile ARG within an ADD statement - docker

I am trying to create a dockerfile that adds files from build directory to the working directory.
The build directory can vary and thus needs to be specified by an argument:
ARG BUILD_SOURCE
FROM node:8.11.4
WORKDIR /usr/local/app
ADD "$BUILD_SOURCE" .
I ran this with docker build BUILD_SOURCE=bin/bundle ..
Somehow the ARG is not substituted so that the whole current directory is added to the image.
When I hardcode the build source it works fine.
I have tried using ENV instead, copying the arg into the the env like this:
ENV BUILD_SOURCE $BUILD_SOURCE
ADD "${BUILD_SOURCE}" .
That didn't work either.
In the official docker documentation I cannot find this behaviour being mentioned.
Does anybody why this is happening and what possible workaround would be?

Ok, I figured it out.
The ARG must be placed below the FROM statement unless it is used within the FROM statement:
FROM node:8.11.4
ARG BUILD_SOURCE
WORKDIR /usr/local/app
ADD "$BUILD_SOURCE" .

Related

DOCKERFILE, Attirbutes in it

I dont really understand what is WORKDIR
I have seen many sources by no one says exactyly where is that WORKDIR
I am using Windows
WORKDIR /usr/src/my_app_directory
Here is the sample from one website, it says that my workdir is /usr/src/my_app_directory.
Can I write anything as my workdir if yes where is that workdir?
Put simply it used to change the default working directly of the container e.g. The directory you are logged into when you first connect or start the container.

Can someone explain what does 5th line mean . i.e ADD . $HOME_DIR mean in docker file?

I have a problem with 5th line of my Dockerfile. I couldn't figure out what that means.
FROM python:3.7-alpine
LABEL author= APPLE
LABEL company= PINEAPPLE
ARG HOME_DIR='/schooldata'
ADD . $HOME_DIRECT ##[ this line ]
EXPOSE 5000
WORKDIR $HOME_DIRECT
RUN pip install -r requirements.txt
ENTRYPOINT ["python", "app.py"]
Here :
ADD . $HOME_DIRECT
ADD performs a resource copy from a source to the current built image with the following specificity : if that is recognized as an archive (tar,zip...), it copies its content, otherwise it copies the contained files/directories such as.
The two next arguments are the source resource and the target resource of ADD.
. means that the source is the build context. Build context is the last argument of the docker build command : often we specify . such as docker build FooTag . to represent the current directory where the docker build command is executed but that may be different.
$HOME_DIRECT is the target directory of the copy inside the built image.
As a side note, ADD has a quite complex behavior (it also may accept URL as source, so it should be favored over COPY only for special cases (URL and copy archive's content).
In most of cases that is indeed better :
COPY . $HOME_DIRECT
It will add the content of your working directory (where the build command has been executed) into you image in the location defined in environment variable HOME_DIRECT
More details: here

When using COPY with more than one source file, the destination must be a directory and end with a /

I decided to use the multiple source form of COPY to save an intermediate command but when I run it the following error pops up:
Step 17/22 : COPY --chown=$APP_USER:$APP_USER Gemfile Gemfile.lock $APP_PATH
When using COPY with more than one source file, the destination must be a directory and end with a /
In the Dockerfile I have this:
ARG APP_PATH='/usr/share/app/'
ONBUILD COPY --chown=$APP_USER:$APP_USER Gemfile Gemfile.lock $APP_PATH
Edit
Just to be clear, this happens with ONBUILD present and without, it just so happened I pasted in the ONBUILD example
I've tried with and without the single quotes. The arg has a trailing slash and is a directory so why is the build not honouring it?
I'd like to make this Dockerfile into a template using ONBUILD so it'd be good if I can make sure the APP_PATH arg is populated with a default that will work.
Any help or insight will be much appreciated.
In my case adding the (forward) slash at the end (following docker error message) was enough, like this:
COPY package*.json . # (fails!)
COPY package*.json ./ # (works:)
The answer, as of Docker version 18.09.0, build 4d60db4, is don't do it this way because it won't work.
I ended up hard-coding the destination directory (and the chown args too):
ONBUILD COPY --chown=app:app Gemfile Gemfile.lock /usr/share/app/
Since COPY is in ONBUILD, ARG needs to be also in ONBUILD
You can think that Docker sort of copies (injects) your ONBUILD command right after the next FROM, at that stage it does not know your ARG if your ARG is not added with ONBUILD.
In my case, earlier I set buildKit to false, so Settings → Docker Engine
"features": {
"buildkit": true
}

Docker and trying to build an image using Azure Pipelines

Hopefully someone can help me see the wood for the trees as they say!
I am no Linux expert and therefore I am probably missing something very obvious.
I have a dockerfile which contains the following:
FROM node:9.8.0-alpine as node-webapi
EXPOSE 3000
LABEL authors="David Sheardown"
COPY ["package.json", "npm-shrinkwrap.json*", "./"]
RUN npm install --production --silent && mv node_modules ../
COPY . /home/vsts/work/1/s/
CMD ["node", "index.js"]
I then have an Azure pipeline setup as the following image shows:
My issue seems to be the build process cannot find the dockerfile itself:
##[error]Unhandled: No Dockerfile matching /home/vsts/work/1/s/**/Dockerfile was found.
Again, apologies in advance for my lack of Linux knowledge.. there is something silly I have done or not done ;)
P.S: I forgot to mention in Azure Pipelines I am using "Hosted Linux Preview"
-- UPDATE --
This is the get sources stage:
I would recommend adding the exact path to where the docker file resides on your repository .
Dockerfile: subpath/Dockerfile`
You're misusing this absolute path, both within the dockerfile and in the docker build task:
/home/vsts/work/1/s/
That is a path that exists on the build agent (not within the dockerfile) - but it may or may not exist on any given pipeline run. If the agent happens to use work directory 2, or 3, or any other number, then your path will be invalid. If you want to run this pipeline on a different type of agent, then your path will be invalid.
If you want to use a dockerfile in your checked out code, then you should do so by using a relative path (based on the root of your code repository), for example:
buildinfo/docker/Dockerfile
Note: that was just an example, to show the kind of path you should use; here you should be using the actual relative path in your actual code repo.

docker add "requires at least one argument" error

I have a folder which contains all the necessary components for an app, which I want to make a container of. I have everything set up so far, with the directory /home/user/Documents/App in the Dockerfile under the ADD heading. Then when Idocker build . in the App directory I get this
ADD /home/user/Documents/App
ADD requires at least one argument
I realize that this is probably a simple fix but I am new to this so any help would be greatly appreciated. Thank you
FROM alpine
ADD </home/user/Documents/App> </home/user/Documents/DockerApp>
WORKDIR /code
RUN pip install -r requirements.txt
EXPPOSE 8080
CMD ["python", "app.py"]
You need a source and destination for the ADD command. The source here is the app folder path. The destination should be where you want the dockerfile is run.
Try this I think it might work
ADD defined in Dockerfile has the following structure
ADD sourceJarName destinationJarName
e.g.
ADD target/spring-boot-rest-docker-example-0.0.1-SNAPSHOT.jar app.jar
change your ADD likewise and try it will work

Resources