How to use environment variables in Windows container with Powershell - powershell

In a Windows docker container I'm trying to set an environment variable based on a build argument.
I have tried this:
docker build --build-arg MYVAR="test" .
In the Dockerfile I have:
#escape=`
ARG MYVAR
ENV MYENVVAR {`"endpointCredentials`": [{`"password`": `"$Env:MYVAR`"}]}
But when I run Get-Item $Env:MYVAR I get:
{"endpointCredentials": [{"password":":MYVAR"}]}
What I want is
{"endpointCredentials": [{"password":"test"}]}

The --build-arg is to pass arguments to the Dockerfile, so you need to configure the MYVAR as an argument in the Dockerfile:
ARG MYVAR
ENV MYENVAR {`"endpointCredntials`": [{`"password`": "$MYVAR"}]}

Related

Command and args in pod

There are many way to set command in pod.
1.
command: ["/bin/sh","-c","sleep 1000"]
2.
command: ["/bin/sh"]
args: ["-c","sleep 1000"]
3.
args: ["/bin/sh","-c","sleep 1000"]
Are they the same or different?
These are all almost the same; except that if the underlying image has a Docker ENTRYPOINT then the args: are passed to it as arguments in the third case, without command:.
In core Docker, there are two ways to specify the main container command, the Dockerfile ENTRYPOINT and CMD directives. Either or both of these can be overridden when the container starts, but resetting the ENTRYPOINT value always clears CMD. If both are present then the CMD is passed as arguments to the ENTRYPOINT.
Kubernetes has the same concept and uses the same underlying image structure, but its names are different:
Kubernetes command: overrides Docker ENTRYPOINT (and resets CMD)
Kubernetes args: overrides Docker CMD
So in the first case setting command: overrides the container entrypoint and resets the container command to an empty list; in the second you directly set both parts. In both cases the two lists are combined together and you get an identical 3-word command.
In the last case args: only overrides the container command part. If the image has a Dockerfile ENTRYPOINT then this list of arguments is passed as arguments to the entrypoint process.
Which one is "right" depends on how the image is constructed:
Some images use CMD to specify the main command to run and don't have ENTRYPOINT; in this case any of the forms you show will work.
CMD the-main-program
# override with either Kubernetes command: or args:
Some images use ENTRYPOINT to specify the main command to run (and depending on how they're constructed may completely ignore CMD). In this case you must use one of the first two forms with command:.
ENTRYPOINT ["the-main-command"]
CMD ["--argument", "value"]
# override with Kubernetes command: and optionally args:
Some images use CMD to specify the main container command, and ENTRYPOINT as a wrapper script to do first-time setup. In this case you need the third form that sets only args: or else the entrypoint script will get skipped.
ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["the-main-command"]
# override with Kubernetes args: only

How to use environment variable in Dockerfile RUN

I've read several tutorials about this and it seems like this code should work. I want to use RUN in my Dockerfile to write environment variables to a config file. The Dockerfile is simply:
FROM php:7.4-apache
RUN echo "I am ${USERNAME}" > /home/dockerfile.log
I am starting it with this docker-compose.yml
version: "3"
services:
web:
build:
context: .
environment:
- USERNAME=larry
After starting with docker-compose up -d the log file just says I Am so the environment variable is empty. What's wrong here? If I run docker-compose config I can see the variable shown correctly there.
The Run command is executed at build time (I.e. when building the image ) while the environment variable in your docker-compose is added to the container at run time. So when the Run command is executed the environment variable has not been populated yet. If you want to pass a parameter at build time, I would suggest using a build argument instead. In your compose file use arg instead of environment

current directory variable substitution in docker-compose for windows 10

I am trying to create absolute paths in my docker-compose using variable substitution for the path up to the current directory. I pass these paths as environment variables in my container.
More precisely I try to pass it via the cd command and not via the PWD env var. It is supposed to be possible declaring the env var in the .env (https://github.com/docker/compose/issues/4081).
Unfortunately I cannot get the substitution to work.
I tried the different approaches:
with the declaration of the var as a environment variable in a .env OR directly in the environment list
Running with powershell and command prompt
using %cd% and $pwd with both shells
escape $ and %, doubling them down with extra $ or %
Relevant piece of the docker-compose:
version: "3.2"
services:
jupyter:
environment:
- WORKING_DIR=%cd% # OR $(pwd) OR $VAR
and when I use the .env:
VAR=%cd% or ${pwd}
When I 'set' my environment variables in my container, it just shows the command I used (%cd%, etc) as if they were strings.
I do not quite understand where is the docker-compose formated/executed: in the shell I use to call docker-compose, in the linux image where my docker daemon runs, etc ?
EDIT:
I tried on a MacOS version and I still have the problem ! (using adequate bash variable substitution)
Not a complete solution, but perhaps something you can script:
If you specify DOCUMENT_ROOT as a variable in the docker compose file
environment:
- WORKING_DIR=${DOCUMENT_ROOT}
And have an .env file in your working directory that contains
DOCUMENT_ROOT=./
, then you can use the local .env file by running
docker-compose -f /path/to/docker-compose.yml up
https://docs.docker.com/compose/compose-file/#env_file

In codeship, what is the syntax to use env variables in steps?

In codeship - I am trying to use the env variables. My setup looks like this:
codeship-services.yml
environment:
- ENV=my-var
codeship-steps.yml
type: parallel
steps:
- command: echo $ENV
I does not work, it just prints $ENV.
Environment variables are only available when you the command is invoked in the context of a shell. By default that's not the case (similar to how docker run operates as well).
To get access to environment variables, either extract the command to a shell script and call the script instead, or explicitly invoke a shell
- service: app
command: sh -c "echo $ENV"

docker compose environment variable for command

I am having troubles in passing environment variables to my custom image via the compose command option:
My compose file:
---
version: '2'
services:
myservice:
image: mycustomimage_lms
environment:
CONF_HOME: /opt/apps-java/
APP_ENV: dev
UUID: me1
command: -Dconfig.home=${CONF_HOME} -Dcomponent.name=LMS -Denv=${APP_ENV} -Duser.dir=/tmp/ -DLMS_UUID=${UUID} -jar /opt/apps-java/my.jar
ports:
- "9060"
volumes:
- ./:/opt/apps-java/
- ./:/var/logs/apps-logs/
- ./:/tmp/data
My image is just a custom jre image which has an entrypoint set to a shell script that accepts jvm arguments. My run.sh that is called from enrtypoint
#!/bin/sh
export JAVA_HOME="/usr/java/latest/"
exec $JAVA_HOME/bin/java $#
I need to pass values to command at runtime since I can then use my image for a lot of other jars and just changing parameters to my image.
This is what i get:
$> docker-compose up
WARNING: The CONF_HOME variable is not set. Defaulting to a blank string.
WARNING: The APP_ENV variable is not set. Defaulting to a blank string.
WARNING: The UUID variable is not set. Defaulting to a blank string.
I have also
gone through couple of answers such as :
Docker Compose - Command using Container Environment Variable
and
Docker-compose environment variables
but could not get it working. Any directions please?
The variables are being read by Compose when the file is parsed. But setting environment only provides values to the container, not to the file parsing.
If you're trying to pass those variables into the container, you need to escape them in the command using an extra $
-Dconfig.home=$${CONF_HOME} -Dcomponent.name=LMS -Denv=$${APP_ENV} -Duser.dir=/tmp/ -DLMS_UUID=$${UUID
If you're just trying to use variables in the Compose file, you need to put those variables into an .env file.
See https://docs.docker.com/compose/compose-file/compose-file-v3/#variable-substitution for the full documentation
Escaping the $ so the variable is not substituted immediately but later in the container environment is the trick, as the accepted answer does in the the docker-compose.yml.
I just wanted to add that in case you pass a command to the docker-compose call, you need to escape the character in the shell, i.e. with \
docker-compose run --rm myservice "-Dconfig.home=\${CONF_HOME} -Dcomponent.name=LMS -Denv=\${APP_ENV} -Duser.dir=/tmp/ -DLMS_UUID=\${UUID} -jar /opt/apps-java/my.jar"