Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker build-arg and copy

Tags:

docker

Trying to copy a folders content, it works when i hard code the path like:

COPY ./my-folder /path/to/location

But need to be able to change this path so i tried using a build argument like this:

COPY ${folderVariable} /path/to/location

and then build with

--build-arg folderVariable=./my-folder

But it copies everything in the same folder as "my-folder", when i only want the contents of "my-folder"

like image 981
A.Jac Avatar asked Apr 18 '17 13:04

A.Jac


2 Answers

You need to define it with ARG in Dockerfile before using:

FROM alpine:3.3

ARG folderVariable=./my-folder # Optional default value to be `./my-folder`

COPY ${folderVariable} /opt/my-folder

And build it like:

docker build --build-arg folderVariable=./folder-copy -t test .

More details please refer to: https://docs.docker.com/engine/reference/builder/#arg

like image 74
shizhz Avatar answered Nov 09 '22 11:11

shizhz


Expansion still does not work for the COPY --from=$var ... case. But you can create intermediate image as an alias, like this:

ARG docsBranch=4.5
ARG docsFullPath=registry.myCompany.pro/group/project-docs/docs:$docsBranch

# Lifehack
FROM $docsFullPath as docs

FROM node:10.21.0-buster-slim
WORKDIR /app

# Now we can use docs instead of $docsFullPath
COPY --from=docs /app/html/en ./documentation/en
like image 43
Dzenly Avatar answered Nov 09 '22 09:11

Dzenly