I have a docker file where i need to open a file and get the data .If the data is dev, i am passing a argument(dev application) and if it is not passing another argument(prod application)to the sh file. It should be something like
f=open config.txt
data=f.read()
if data=dev
app=dev application
else
app=prod application
ENTRYPOINT ["./entry.sh app"]
here i am trying to pass the argument to the .sh file (entry.sh).so i can use the incoming argument in the entry.sh file. Please help me how to do it in a correct way.
I am new to this Dockerfile.
You can pass arguments while building the docker image by using the ARG parameters like this. while building you can pass values like docker build --build-arg ENV_ARG=dev
DOCKER FILE CODE SAMPLE
ARG ENV_ARG=prod
ENV ENVIRONMENT=$ENV_ARG
CMD ["sh", "-c", "entry.sh ${ENVIRONMENT}"]
Related
I have a deadly doubt, I would like to replace an environment variable that is declared in a .sh file inside a docker image.
Any way to do this without having to mount a volume to change it?
NB. I already tried to do this through compose, and I have already guaranteed that my set variable is there, but it is overwritten by the original declaration inside the sh file.
I don't understand reason for such thing. ENV is gets exact value on a stage of image run. If you are trying to run multiple application with different profile pass different env file during docker run.
In Dockerfile you could pass argument to you shell script:
CMD my_app.sh ${APP_ADDR} ${APP_PORT}
in my_app.sh
get variables like
APP_ADDR=$1
APP_PORT=$2
and when you running you docker image store all you variables in env file and pass it like this:
docker run --env-file=app_local.env my_app:0.1
in env file you could define you variables:
APP_ADDR=192.168.200.200
APP_PORT=5678
....
You can do as suggested here
ENTRYPOINT ./base_image_entrypoint_script.sh && export URI=http://localhost
This way you override the base image ENTRYPOINT with the same script but will add the env car you wanted that will override the script variable.
I have a Dockerfile such as:
FROM tomcat:8.5
COPY webapp.war /usr/local/tomcat/webapps/
COPY conf /usr/local/tomcat/conf/
CMD ["catalina.sh", "run"]
conf contains many types of files (.json, .xml, .properties) with some placeholders in them with the following format: ${some.place.holder}
I want to build the image with the placeholders, and give to my users the possibility to replace them.
Ideally, when running the image, they should be able to give a new file as a parameter such as:
some.place.holder=hello
What would be the correct way to achive that?
If you want to use a bash file to set all the variables, you can write set_env_var.sh:
#!/bin/sh
export PLACEHOLDER1=value
export PLACEHOLDER2=value
And run:
source set_env_var.sh
It will set all the variables.
Or you can run any command by setting multiple variables like this. Eg. I want to run a docker-compose command with variables in it by:
PLACEHOLDER1=value PLACEHOLDER2=value docker-compose up -d
I have this .env file:
admin=admin
password=adminsPassword
stackName=integration-demo
the values of which are used in the docker-compose.yml file, like this:
myService:
build:
context: .
dockerfile: myService.Dockerfile
args:
- instance=${stackName}.local
- admin=${admin}
- password=${password}
volumes:
- ./config:/config
I want to add them to the Docker compose file, like this:
FROM openjdk:8-jdk-alpine
ARG docker_properties_file=Username=$admin\nPassword=$password\nHost=$instance
RUN $docker_proprties_file >> config/gradle-docker.properties
so that I have a gradle-docker.properties file that looks like:
username=admin
password=adminsPassword
host=integration.demo.local
in the /config directory.
However, no gradle-docker.properties file is getting written.
How can I use the variable in a docker-compose.yml file to add data to a volume?
Plain Docker and Docker Compose don’t have this capability. You can create the file outside of Docker on the host and mount it into the container as you show, but neither Docker nor Compose has the templating capability you would need to be able to do this.
The overall approach you’re describing in the question builds a custom image for each set of configuration options. That’s not really a best practice: imagine needing to recompile ls because you attached a USB drive you needed to look at.
One thing you can do in plain Docker is teach the image how to create its own configuration file at startup time. You can do that with a script like, for example:
#!/bin/sh
# I am docker-entrypoint.sh
# Create the config file
cat >config/gradle-docker.properties <<EOF
username=$USERNAME
et=$CETERA
EOF
# Run the main container process
exec "$#"
In your Dockerfile, COPY this file into the image and set it as the ENTRYPOINT; leave your CMD unchanged. You must use the JSON-array form of the ENTRYPOINT directive.
...
COPY docker-entrypoint.sh .
RUN chmod +x docker-entrypoint.sh
ENTRYPOINT ["./docker-entrypoint.sh"]
CMD ["java", "-jar", "application.jar"]
(In Kubernetes, the Helm package manager does have a templating system that can create content for a ConfigMap object that can be injected into a pod; but that’s a significant amount of extra machinery.)
I have 2 Dockerfiles that have common arguments (ARG) that are passed to the actual commands (RUN) to build the images.
Is it possible to provide an external file with the arguments so that when I need to update one of them I don't need to touch both Dockerfiles?
An ARG is designed to be modified from the build command line, so you'd run:
docker build --build-arg VAR=value -t your_image .
That can be placed inside of a shell script to automate it and pass the same arg to each build.
You can also use a compose file, and the compose file may use environment variables or a .env file, to set variables used inside the compose file, e.g.
build:
context: ./your_app_dir
dockerfile: Dockerfile
args:
VAR: ${VALUE}
And the .env would contain:
VALUE=your_value
For more details on compose files, see the build syntax and also the environment file syntax.
How does one pass arguments into a dockerfile?
Lets say I have the dockerfile:
FROM ubuntu:14.04
MAINTAINER Karl Morrison
sudo do-something-here myVarHere
I would want to build the file as so for example:
docker build basickarl/my-image-example /directory/of/my/dockerfile "my string to be passed to myVarHere here!"
Docker has ARG that you can use here
FROM ubuntu:14.04
MAINTAINER Karl Morrison
ARG myVarHere
RUN do-something-here myVarHere
And then build using --build-arg:
docker build --build-arg myVarHere=value
We've had a similar requirement and came up with a relatively simple script that does exactly that:
We create a file called dockerfile_template in which we use variables just like you describe. The script takes that file, performs string substitutions and copies it to dockerfile (no _template) before calling docker build dockerfile.
Works pretty good. Also very extensible for future requirements.
Update:
Scrap that. Use build-arg (here)