Variable Substitution with Bamboo in Dockerfile - bamboo

My Dockerfile looks like the following:
from httpd:${bamboo.test.tag}
COPY index.html /usr/local/apache2/htdocs/
In Bamboo I have a task with the following script:
docker build --no-cache -t myproj/my .
When running the job, I get the following error:
build 26-Sep-2022 10:42:26 Step 1/2 : from httpd:${bamboo.test.tag}
error 26-Sep-2022 10:42:26 failed to process "httpd:${bamboo.test.tag}": missing ':' in substitution
How can I substitute the tag?

This is actually a problem with how you are using the dockerfile.
Docker will not expand environment variables inside your Dockerfile. You need to pass the environment value as a build argument in the docker build command then use the ARG keyword inside the Dockerfile.
Your Dockerfile would look like this:
ARG IMAGE_TAG
from httpd:${IMAGE_TAG}
COPY index.html /usr/local/apache2/htdocs/
And you would need to change you docker build command to:
docker build --no-cache --build-arg IMAGE_TAG=${bamboo.test.tag} -t myproj/my .
Check a more detailed explanation here

Related

Variable substitution is not happening inside Containerfile

Building a docker container
Using alpine as build image
The Containerfile receives VERSION as argument
It is set as image version
The same version is passed to dotnet publish command.
The passed version (which is a variable) in current context never gets substituted. Instead of substituted variable ${VARIABLE} is passed to dotnet publish command.
ARG VERSION
FROM mcr.microsoft.com/dotnet/sdk:6.0-alpine as build
ARG VERSION
RUN echo ${VERSION}
RUN echo ${VERSION} > image_version
WORKDIR /app
COPY . .
RUN dotnet restore
# TODO Pass the version as build command below.
RUN dotnet publish --version-suffix ${VERSION} --output /app/published-app
I tried different ways to do the variable substitution like below:
"$VERSION"
"$(VERSION)"
$VERSION ..... and many other ways I found in the internet. None of them worked. The argument passed is exactly what it is before the variable is substituted.
Thoughts on what may be causing this?

Docker ADD and COPY not honored in Dockerfile

When I try to build the following Dockerfile, the ADD and COPY steps do nothing:
# Use an official Apache runtime as a parent image
FROM amd64/httpd
# Set the working directory
WORKDIR /usr/local/apache2
# Copy the following contents into the container
ADD ./httpd.conf {$workdir}/conf/httpd.conf
COPY ./Projects/RavensHomeSupport/build/* {$workdir}/htdocs/Test/
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME RavensHomeWeb
I run the following build command:
docker build -t ravenshome --rm --no-cache .
and when I check the contents of the Test directory in the running container, none of the data that I expected has been copied across to the container. The output of the build command is here.
Sending build context to Docker daemon 1.444MB
Step 1/6 : FROM amd64/httpd
---> 19459a872194
Step 2/6 : WORKDIR /usr/local/apache2
---> Running in 192cb44f767e
Removing intermediate container 192cb44f767e
---> d9816ea17258
Step 3/6 : ADD ./httpd.conf {$workdir}/conf/
---> 19f48db970bb
Step 4/6 : COPY ./Projects/RavensHomeSupport/build/ {$workdir}/htdocs/Test/
---> d93939218c2b
Step 5/6 : EXPOSE 80
---> Running in 43b9e9297f60
Removing intermediate container 43b9e9297f60
---> 3b994be07747
Step 6/6 : ENV NAME RavensHomeWeb
---> Running in a64bccaf81c8
Removing intermediate container a64bccaf81c8
---> 9217c242868c
Successfully built 9217c242868c
Successfully tagged ravenshome:latest
I start the container with the following command:
docker run -dit -p 8080:80 --name ravenshome ravenshome
When I examine the problem directory in the container with the following command:
docker exec ravenshome ls -a /usr/local/apache2/htdocs
I get the following result:
.
..
index.html
As you can see, all that was there is the contents of the default image, not the additional content that I expected.
Similarly, my customized version of httpd.conf is not copied to the new container.
I have read several posts that suggest that the problem is due to using volumes, but I am not doing so, nor do I have a .dockerignore file.
Can anyone see what I am doing wrong?
$workdir isn't a defined environment variable, so it expands to an empty string. $variable inside curly braces isn't special syntax at all; it expands to the variable expansion, inside curly braces. The net result of this is that these two lines:
WORKDIR /usr/local/apache2
ADD ./httpd.conf {$workdir}/conf/httpd.conf
copy content into a directory /usr/local/apache2/{}/conf/http.conf -- nothing is inside the curly braces, and the curly braces themselves are interpreted as a directory relative to the current working directory.
You don't need an environment variable here at all; you can just COPY to the current WORKDIR
WORKDIR /usr/local/apache2
ADD ./httpd.conf ./conf/httpd.conf
COPY ./Projects/RavensHomeSupport/build/* ./htdocs/Test/
See also Variable substitution in the docker-compose.yml documentation for the allowed forms; you're probably thinking of ${variable} syntax (dollars outside the curly braces).

How to make a Docker environment variable value to get a random id

I am looking to pass an environment variable which should get a random id.
Something like below.
ENV SERVICE_TAG= $uuid
In short, every time I run the container, I should get a random id for this environment variable inside the container.
Can anyone please suggest the way forward?
Thanks and regards,
Prasanth.
Add uuidgen package to the image. In case you are using alpine add
RUN apk add --no-cache util-linux
to the Dockerfile
Then in the entrypoint of your Dockerfile add
Below is a sample Dockerfile
FROM alpine:latest
RUN apk add --no-cache util-linux
ENTRYPOINT export UUID=`uuidgen` && echo $UUIDFROM alpine:latest
You should pass the environment variable from a docker run -env parameter generated uuid or guid from your shell, eg:
bash:
docker run --env SERVICE_TAG=$(uuidgen) yourimage
See more details at the official docker docs:
https://docs.docker.com/engine/reference/commandline/run/#set-environment-variables--e---env---env-file

Add a link to docker run

I am making a test file. I need to have a docker image and run it like this:
docker run www.google.com
Everytime that url changes, I need to pass it into a file inside the docker. Is that possible?
Sure. You need a custom docker image but this is definitely possible.
Let's say you want to execute the command "ping -c 3" and pass it the parameter you send in the command line.
You can build a custom image with the following Dockerfile:
FROM alpine:latest
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT /entrypoint.sh
The entrypoint.sh file contains the following:
#!/bin/sh
ping -c 3 "$WEBSITE"
Then, you have to build you image by running:
docker build -t pinger .
Now, you can run your image with this command:
docker run --rm -e WEBSITE=www.google.com pinger
By changing the value of the WEBSITE env variable in the latest step you can get what you requested.
I just solved it by adding this:
--env="url=test"
to the docker run, but I guess your way of doing it, is better.
Thank you

Environment variables for build not visible in Dockerfile

I've set an environment variable (NPM_TOKEN) for my repo in Docker Cloud to use when building my Dockerfile. However, the variable is always empty...
Tried both of these in Dockerfile:
RUN echo ${NPM_TOKEN}
and:
ARG NPM_TOKEN
RUN echo ${NPM_TOKEN}
Am I wrong in assuming that Docker Clouds environment variables for build does the same thing as --build-arg?
It took me along time, but you can use build hooks to set variables for automated builds!
https://docs.docker.com/docker-cloud/builds/advanced/#build-hook-examples