Docker build fails when executed within Makefile

So, I have a Docker build command that I have tested which works great

docker build \
  -t app \
  --no-cache --network host \
  --build argssh_private_key="$(cat ~/.ssh/id_rsa)"\
  --build-arg python_version="3.6.8" -f Dockerfile .

To ease the pain of the team learning Docker I encapsulated a few of the commands - build, start, stop - within a Makefile. However, within the Makefile I need to change the command slightly by modifying

$(cat ~/.ssh/id_rsa)

to

$(shell cat ~/.ssh/id_rsa)

When I execute the following:

make build

I receive the following message:

Step 13/20 : RUN git clone --depth 1 "${git_user}@${git_host}:${git_repo}" app
---> Running in d2eb41a71315
Cloning into 'app'...
Warning: Permanently added the ECDSA host key for IP address [ip_address] to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights and the repository exists.

However, I do not have the same issue when executing from the command-line. I I think it has something to do with the way the call the "cat" command but, I do not know a way to resolve.

Any ideas ?

Makefile:

APP_NAME=ccs_data_pipeline
DATA?="${HOME}/data"
DOCKER_FILE=Dockerfile
PYTHON_VERSION?=3.6.8
SRC?=$(shell dirname `pwd`)
PRIVATE_KEY?=$(shell echo $(shell cat ~/.ssh/id_rsa))

build: ## Build container for ccs data pipeline
  docker build \
    -t $(APP_NAME) \
    --no-cache --network host \
    --build-arg ssh_private_key="$(PRIVATE_KEY)" \
    --build-arg python_version="$(PYTHON_VERSION)" \
    -f $(DOCKER_FILE) .

start: ## Start the docker container
  docker run \
    -it -v $(DATA):/data \
    --network host \
    --rm \
    --name="$(APP_NAME)" $(APP_NAME)
        
stop: ## Stop the docker container
  docker stop $(APP_NAME); \
  docker rm $(APP_NAME)

Solution 1:

Please show your actual makefile, or at least the entire rule that is having the error. The single command you provided, with no context, is not enough to understand what you're doing or what might be wrong.

Note that it is often not correct to replace a shell operation like $(...) with a make shell command $(shell ...). However, sometimes it will work "by accident", where the real differences between those commands don't happen to matter.

In general you should never use $(shell ...) inside a recipe (I have no idea if this command appears in a recipe). Instead, you should escape all the dollar signs that you want to be passed verbatim to the shell when it runs your recipe:

$$(cat ~/.ssh/id_rsa)