Dockerfile multiple lined env variable - environment-variables

How can I define this variable? Please help
ENV NGINX_REPO \
cat > /etc/yum.repos.d/nginx.repo <<EOF
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/\$basearch/
gpgcheck=0
enabled=1
EOF

If you have access to docker-compose you can use an .env file.
Check out this link https://docs.docker.com/compose/env-file/#syntax-rules
But maybe you should pass this as a configuration file to your docker container. There are multiple ways of doing it:
If you are in a docker swarm then simply refer to https://docs.docker.com/engine/reference/commandline/config/
If you are using docker-compose then use the .env file as explained before
Use --volume option to pass the config file to your container
Hope this could help you

Related

Adding a tag to all images in a docker-compose file?

For all my images I have a latest tag for production, and test for testing. I also have 2 docker-compose files that only differ in that one uses latest on all packages (except nginx), while the other uses test. Is there a way to set this via a CLI variable, so that I don't have to keep those two files in sync manually all the time?
We can handle this with an environment variable.
docker-compose.yml:
services:
...
my-service-one:
image: my/service-one:$TAG
...
my-service-two:
image: my/service-two:$TAG
...
Then we can add an .env file (for production) with content
TAG=latest
and a test.env file (for testing) with content
TAG=test
When we run docker compose up, file .env will be used by default. If we want to start our test deployment, we can run docker compose --env-file test.env up.
You can simply run these commands for each compose file
TAG=latest docker-compose -p latestapp -f compose-for-latest.yaml up -d
and
TAG=test docker-compose -p testapp -f compose-for-test.yaml up -d
of course, don't forget to specify where you want to use your Tag variable in the compose files using ${TAG} or $TAG (BOTH ARE VALID)
If you are a LINUX user this should work fine.
if you are a WINDOWS user and the commands didn't work you can use GIT BASH instead.

How to use environment variables with docker-compose from other directory

I'm setting up a new docker-based server and I'm writing a script to run
$ docker-compose
for all my docker-compose.yml files at once.
I know about existence of .env files and I know about env_file option in docker-compose.yml, however the .env files are ignored when I launch
$ docker-compose up
from other directory and not even full path in env_file helps.
my_script.sh:
#!/bin/bash
docker-compose up -f /server1/docker-compose.yml -f /server2/docker-compose.yml -f /server3/docker-compose.yml
Docker-compose.yml:
version: '3'
services:
service_name:
build: /server1/apache
env_file: /server1/.env
volumes:
- /web/${ROOT_DOMAIN}/web/:/var/www/:rw
.env
ROOT_DOMAIN=server1.domain.tld
I want to replace the ${ROOT_DOMAIN} variable in volumes for the variable defined in .env file. This works like a charm, when I run
$ docker-compose up
from the /server1/ folder. However when I run my script, which is in other directory, the enviroment variables are ignored and default to null.
How to fix this?
Sorry to say, but this cannot be fixed. The provided .env file must be in the same directory.
This is a requirement of docker compose and is described in the docs (first paragraph): https://docs.docker.com/compose/env-file/
You can roll your own:
function execute() {
(set -a; source .env; cat $1 | envsubst | kubectl apply -f -)
}
execute foo.yaml
And
$ cat .env
FOO=123
BAR=abc
$ cat foo.yaml
{
"myfoo": $FOO,
"mybar": $BAR
}
You can use the flag --env-file to set the path where the .env file is located, example:
docker-compose --env-file ./config/.env.dev up
Official Docker documentation:
https://docs.docker.com/compose/environment-variables/
Using flag didn't work for me, but since I'm using node I created a script which copies the .env file across before running it.
If you aren't using node you could run a bash script which essentially does the same thing.
"scripts":
{
"build_postgres":"cp .env node-app/pg/.env && cd node-app/pg && docker-compose up",
}
Warning
Just be sure to exclude the .env file from your .gitignore in case of sensitive information ending up in git repo.
.gitignore
node-app/pg/.env
note:
Be sure your .env file variables are uppercase and values
PG_USER=myuser

Configuring cassandra.yaml for password auth inside docker

Can someone tell me how to change cassandra.yaml inside a docker container?
I want to enable password authentication inside docker for cassandra access.
If you're using the official Cassandra Docker image, you'll already have the docker-entrypoint.sh. See: https://github.com/docker-library/cassandra/blob/master/docker-entrypoint.sh for some of the variables already defined, as examples.
To have these included when your container starts, you could:
fork and edit the docker-entrypoint.sh starting at (currently) line 51 to add your own variables like this:
for yaml in \
broadcast_address \
broadcast_rpc_address \
[your_selected_yaml_variable] \
...
include the values you want to override in docker-compose.yml like this:
environment:
- CASSANDRA_SEEDS=DC1C1,DC1C2,DC2C1,DC2C2
- CASSANDRA_CLUSTER_NAME=Dev_Cluster
- CASSANDRA_ENDPOINT_SNITCH=GossipingPropertyFileSnitch
- CASSANDRA_[YOUR_SELECTED_YAML_VARIABLE]
You could create a docker entry point (basically it'a script file that you instruct Docker to copy on the container and it's defined as entrypoint).
COPY docker-entrypoint.sh /docker-entrypoint.sh
ENTRYPOINT ["bin/sh", "/docker-entrypoint.sh"]
In that file you can do whatever changes you like on cassandra.yaml file using sed.
sed -ri '/^# data_file_directories:/{n;s/^#.*/'" - $CASSANDRA_DATA_DIRECTORY"'/}' "$CASSANDRA_CONFIG/cassandra.yaml"
Note that $CASSANDRA_DATA_DIRECTORY and $CASSANDRA_CONFIG are some variables defined in advance.

Set $PROJECT_NAME in docker-compose file

I am using the same docker-compose.yml file for multiple projects. I am really lazy, so I don't want to start them with docker-compose -p $PROJECT_NAME up.
As of Docker version 17.06.0, is it possible to set the variable directly in the docker-compose.yml file?
UPDATE: You can now use the top-level name property in your docker-compose YAML file. This is available from Docker Compose v2.3.3
This is the result of the #745 proposal. An issue which persevered for about 8 years.
Previously:
Right now, we can use the .env file to set the custom project name like this:
COMPOSE_PROJECT_NAME=SOMEPROJECTNAME
It's not flexible, but it's better than nothing. Currently, there is an open issue regarding this as a proposal.
I know this question was asked a long time ago, but I ran into the same problem. There's a suggestion to add the feature https://github.com/docker/compose/issues/745, but they don't want to.
However, I have a Makefile in the root of the directory and then you can add something like in the Makefile:
.PHONY: container-name
container-name:
docker-compose -p $PROJECT_NAME up -d container-name
and then run make container-name
I know it isn't what you asked for, but could maybe make your life a bit easier.
220806 UPDATE: you can now use the top-level name property in your docker-compose YAML file.
This is the result of the #745 proposal.
Update as on Docker Compose version 2.3.3, name can be given in the compose file, but please note the following as per documentation compose-spec at github.com., Compose official documentation
Whenever project name is defined by top-level name or by some custom mechanism, it MUST be exposed for interpolation and environment variable resolution as COMPOSE_PROJECT_NAME.
name: stitch
services:
foo:
image: busybox
environment:
- COMPOSE_PROJECT_NAME
command: echo "I'm running ${COMPOSE_PROJECT_NAME}"
Previously proposed solution :
You can add them as your environment variables which are available through the session in which you are bringing up your containers using the docker compose.
Ie, if you wanted to use $PROJECT_NAME somewhere inside your docker-compose.yaml then if this variable has a value in your session, then it would be picked up.
Inside the yaml you can assign it to anything as you want it. You want as a commandline arg to some script, even that is also possible. ie,
working_dir: /opt
command: /bin/bash -c './script.sh ${PROJECT_NAME}'
volumes:
- /var/run/:/host/var/run/
I'm using
docker version : Docker version 17.09.0-ce, build afdb6d4
docker-compose version : docker-compose version 1.14.0, build c7bdf9e

Can you add ENV VAR to docker compose up command

I need to define an env var in docker compose (v2).
Now I just have something like:
environment:
- SERVERNAME=192.168.xx.xx
But I don't really like this approach. People need to modifiy the compose file. Is there way that I can do this more dynamic. Something like:
docker-compose up --env SERVERNAME=192.168.xx.xx
What is the best approach to do this?
I think it's not possible but the most close solution can be pass it in a env file
From de docker docs:
You can pass multiple environment variables from an external file through to a service’s containers with the ‘env_file’ option.
So you can create a env file with the variable, for example server.env, and reference it in the docker-composer.yml
env_file:
- server.env
Or you can create a .env file in the folder
$ cat .env
SERVERNAME=192.168.xx.xx
And change your config with:
environment:
- SERVERNAME=${SERVERNAME}
You can do it with -e "SERVERNAME=192.168.1.1" -e "SOMETHING=bla" syntax.
Or use something like Hashicorp's Vault.

Resources