Dcokerfile pass ENV to ENTRYPOINT or CMD - docker

I'm trying to start an app based on arg passed at build time
cmd:
docker build --build-arg profile=live . -t app
Dockerfile:
FROM openjdk:11.0.7-jre-slim-buster
WORKDIR /app
ARG JAR_FILE=target/*.jar
ARG profile
ENV profile ${profile:-dev}
EXPOSE 8080
COPY ${JAR_FILE} /app/app.jar
# ENTRYPOINT ["java", "-jar", "app.jar", "--spring.profiles.active=${profile}"] --- not working
RUN echo $profile <--- here I got the value
#CMD java -jar app.jar --spring.profiles.active=${profile} --- not working
#CMD java -jar app.jar --spring.profiles.active=$profile --- not working
CMD ["sh", "-c", "node server.js ${profile}"] --- not working
when I inspect the docker image I got
"Cmd": [
"sh",
"-c",
"node server.js ${profile}"
],
What i'm missing?
Thanks
update:
works with CMD java -jar app.jar --spring.profiles.active=$profile and the $profile will have the desired value at runtime

Environment replacement doesn't happen in CMD. Instead it happens in the shell running inside the container (in your case sh, though it's not clear why you've used the json/exec syntax to call a sh command).
Documentation on environment replacement is available from: https://docs.docker.com/engine/reference/builder/#environment-replacement

Try this Dockerfile: docker build --build-arg PROFILE=uat -t app .
FROM alpine
ARG PROFILE
ENV PROFILE ${PROFILE:-dev}
CMD ["ash", "-c", "while :; do echo $PROFILE; sleep 1; done"]
Run it and it prints uat every second: docker run -it --rm app
You didn't mention what exactly is the outcome when you said "not working". Presumed you got empty string or other unexpected value, the environment variable could be in used by the base image. Try another name for your environment variable, or use non-slim version of openjdk image.

Related

ENTRYPOINT and CMD with build arguments

This doesn't work:
FROM alpine:3.7
# build argument with default value
ARG PING_HOST=localhost
# environment variable with same value
ENV PING_HOST=${PING_HOST}
# act as executable
ENTRYPOINT ["/bin/ping"]
# default command
CMD ["${PING_HOST}"]
It should be possible to build an image with build-arg and to start a container with an environment variable to override cmd as well.
docker build -t ping-image .
docker run -it --rm ping-image
Error: ping: bad address '${PING_HOST}'
UPDATE:
FROM alpine:3.7
# build argument with default value
ARG PING_HOST=localhost
# environment variable with same value
ENV PING_HOST ${PING_HOST}
COPY ./entrypoint.sh /usr/local/bin/
# act as executable
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
# default command
CMD $PING_HOST
entrypoint.sh
#!/bin/sh
/bin/ping $PING_HOST
This works because the entrypoint.sh enables variable substitution as expected.
For CMD to expand variables, you need to arrange for a shell because shell is responsible for expanding environment variables, not Docker. You can do that like this:
ENTRYPOINT ["/bin/sh"]
CMD ["-c" , "ping ${PING_HOST}"]
OR
CMD ["sh", "-c", "ping ${PING_HOST}"]

User-provided environment variable within docker CMD

I have successfully pushed my docker image to the swisscom app cloud (similar to this example: https://ict.swisscom.ch/2016/05/docker-and-cloudfoundry/).
Now I would like to use a user-provided environment variable within my docker CMD. Something like this:
ADD target/app.jar app.jar
CMD java -jar app.jar -S $USER_PROVIDED_ENV_VARIABLE
I also tried system-provided environment variables:
ADD target/app.jar app.jar
CMD java -jar app.jar -S $VCAP_APPLICATION
What am I doing wrong here?
If your Dockerfile is built like that, you'll simply need to pass the -e flag when running the image.
Example Dockerfile:
FROM ubuntu:16.10
ENV MY_VAR "default value" # Optional - set a default value.
CMD echo $MY_VAR
Build the image:
docker build -t my_image .
Run a container from the image:
docker run -e MY_VAR="my value here" my_image

Dockerfile placeholders not replaced during build

Using Docker for Windows, Community version 17.06.0-ce-win19 (12801), Windows 10
Dockerfile
FROM frolvlad/alpine-oraclejdk8:slim
ARG APP_NAME=client-default
RUN mkdir -p /client/
ADD build/libs/$APP_NAME.jar /client/$APP_NAME.jar
ENTRYPOINT ["java", "-jar", "/client/$APP_NAME.jar"]
Running
docker build --build-arg APP_NAME=client-1 -t test/client-1 .
Placeholders not replaced after build (tried ${APP_NAME}, %APP_NAME%)
ARG and ENV are not replaced by Docker in an ENTRYPOINT or a CMD when you use the EXEC form inside []. If the ENTRYPOINT or CMD use the shell form (string, not an array) the shell will be able to do the variable substitution for you.
ARG and ENV variables will be available in RUN commands in the container as environment variables.
Docker will also replace $VARIABLES in the Dockerfile in the following instructions:
ADD
COPY
ENV
EXPOSE
FROM
LABEL
STOPSIGNAL
USER
VOLUME
WORKDIR
Only ENV variables will become available in a CMD or ENTRYPOINT, only in the environment of the running container:
Using ARG
FROM frolvlad/alpine-oraclejdk8:slim
ARG APP_NAME=client-default
ENV APP_NAME=$APP_NAME
RUN mkdir -p /client/
ADD build/libs/$APP_NAME.jar /client/$APP_NAME.jar
ENTRYPOINT ["sh", "-c", "java -jar /client/$APP_NAME.jar"]
or with only ENV
FROM frolvlad/alpine-oraclejdk8:slim
ENV APP_NAME=client-default
RUN mkdir -p /client/
ADD build/libs/$APP_NAME.jar /client/$APP_NAME.jar
ENTRYPOINT ["sh", "-c", "java -jar /client/$APP_NAME.jar"]
Environment variables in Dockerfile are declared with the ENV statement.
In your case:
FROM frolvlad/alpine-oraclejdk8:slim
ENV APP_NAME client-default
RUN mkdir -p /client/
ADD build/libs/${APP_NAME}.jar /client/${APP_NAME}.jar
ENTRYPOINT ["java", "-jar", "/client/${APP_NAME}.jar"]
ARG is only available during the build of a Docker image (RUN etc), not after the image is created and containers are started from it (ENTRYPOINT OR CMD)
ARG variable doesn't get substituted in Dockerfile. ARG IS really an environment variable. During the build, docker "runs" the container using ARG variables as environment variables. Easy to prove (you can test it): Try RUN printenv on your Dockerfile and you'll see the ARG as environment variable
So, you've got two problems:
First:
The Steps in console DOESN'T substitute the ARG variable. So, your jar is really in the container, your line:
ADD build/libs/$APP_NAME.jar /client/$APP_NAME.jar
it's working. Try it.
Second:
Entrypoint is for the running container, not for the image, so you need to put your ARG into an ENV variable, and not to use the exec form. If your use the exec form of ENTRYPOINT does not invoke a command shell. This means that normal shell processing does not happen. If you want shell processing then either use the shell form or execute a shell directly, for example:
ENTRYPOINT cat ${APP_NAME_RUN}
So, try this working example:
FROM ubuntu
ARG APP_NAME=client
RUN mkdir -p /client/
ADD ./$APP_NAME.txt /client/$APP_NAME.txt
ENV APP_NAME_RUN="/client/${APP_NAME}.txt"
RUN echo $APP_NAME_RUN
ENTRYPOINT cat ${APP_NAME_RUN}

Docker Running the Java -JAR file to create Cassandra KeySpaces. But its not creating

I tried to run the CMD "java -jar /tmp/migration.jar update_schema atlas " to create the Keyspace in the cassandra. But its not creating any keyspaces in cassandra. But if i run same command in the command line its creating any idea whats the issue?
My dockerfile is as follows
'FROM tomcat:8-jre8
ENV LANG en_US.UTF-8
ENV COMMAND="update"
ENV ARGS="--logLevel=debug"
WORKDIR /usr/local/tomcat/
ADD /migration.jar /tmp
ADD atlas_migration.sh /usr/local/bin/atlas_migration.sh
CMD ["/bin/sh", "/usr/local/bin/atlas_migration.sh"]
CMD ENTRYPOINT ["java","-jar","/tmp/migration.jar","update_schema", "atlas"]
CMD java -jar /tmp/migration.jar update_schema atlas
ENV CATALINA_OPTS "-Xmx256m -Xms192m"
EXPOSE 8085
CMD ./bin/catalina.sh start && tail -f ./logs/catalina.out'
CMD is for specifying the command the container should run when it starts. If you want to run a command during the build, so the state after execution is persisted in the image, you need to use RUN.
COPY is also preferable to ADD, so the relevant instructions should be:
COPY /migration.jar /tmp
COPY atlas_migration.sh /usr/local/bin/atlas_migration.sh
RUN /usr/local/bin/atlas_migration.sh
RUN ["java","-jar","/tmp/migration.jar","update_schema", "atlas"]

Docker CMD weirdness when ENTRYPOINT is a shell script

Here's a simple Dockerfile
FROM centos:6.6
ENTRYPOINT ["/bin/bash", "-l", "-c"]
CMD ["echo", "foo"]
Unfortunately it doesn't work. Nothing is echo'd when you run the resulting container that's built.
If you comment out the ENTRYPOINT then it works. However, if you set the ENTRYPOINT to /bin/sh -c, then it fails again
FROM centos:6.6
ENTRYPOINT ["/bin/sh", "-c"]
CMD ["echo", "foo"]
I thought that was the default ENTRYPOINT for an container that didn't have one defined, why didn't that work?
Finally, this also works
FROM centos:6.6
ENTRYPOINT ["/bin/bash", "-l", "-c"]
CMD ["echo foo"]
Before I submit an issue, I wanted to see if I'm doing something obviously wrong?
I'm using rvm inside my container which sort of needs a login shell to work right.
Note that the default entry point/cmd for an official centos 6 image is:
no entrypoint
only CMD ["/bin/bash"]
If you are using the -c command, you need to pass one argument (which is the full command): "echo foo".
Not a series of arguments (CMD ["echo", "foo"]).
As stated in dockerfile CMD section:
If you use the shell form of the CMD, then the <command> will execute in /bin/sh -c:
FROM ubuntu
CMD echo "This is a test." | wc -
If you want to run your <command> without a shell then you must express the command as a JSON array and give the full path to the executable
Since echo is a built-in command in the bash and C shells, the shell form here is preferable.

Resources