Docker-Compose Passing command args by .env - docker

I often need to start Odoo server with different arguments.
So in docker-compose.yml, which is versioned, I specified the following:
version: '3'
services:
web:
image: odoo:12.0
command: odoo ${ODOO_ARGS}
and created a .env file with:
ODOO_ARGS="--update=all"
This works well with a single argument, but it doesn't handle multiple arguments. For example if I try the following:
ODOO_ARGS="--database=myDb --update=all --stop-after-init"
the command will be evaluated as: odoo --database="myDb --update=all --stop-after-init"
I pretty sure it is a syntax issue, so I'd like to know how to pass multiple arguments to the command option through .env file.

It actually evaluates to odoo "--database=myDb --update=all --stop-after-init" just because you put quotes in the env file. Here is an example with several arguments in one string:
docker-compose.yml
version: "3.7"
services:
test:
image: debian:buster
command: find ${ARGS}
.env
ARGS=/ -name bash
Running this you'll get:
test_1 | /bin/bash
test_1 | /usr/share/lintian/overrides/bash
test_1 | /usr/share/menu/bash
test_1 | /usr/share/doc/bash

Related

Setting JVM_ARGS for Payara server in docker-compose

How can I set a Payara servers jvm-option in docker compose?
For the application I use the payara/server-full:5.194 container.
I know that I have to use the JVM_ARGS ENV Variable, but I don't know how exactly.
This is a piece of my docker-compose.yml file:
version: "3.5"
services:
myApplication:
build:
context: .
dockerfile: Dockerfile
image: myApplication:latest
environment:
POSTBOOT_COMMANDS: /tmp/init_asadmin_commands.sh
JVM_ARGS: -DmyApplication.home.dir=/msc/srv/myApplication
ports:
- "8080:8080"
- "4848:4848"
Does anybody has an example for me? (I haven't found a good usage)
Thanks!
Edit(1):
This is the error what i get on docker-compse up:
sed: -e expression #1, char 54: unknown option to `s'
And this is what I found, how the container uses it:
COMMAND=`echo "$OUTPUT"\
| sed -n -e '2,/^$/p'\
| sed 's/glassfish.jar/glassfish.jar '"$JVM_ARGS"'/' `

docker-compose need to get properties by service name , espesely ports

I try to get the ports from the services I defined in the compose_file.yml
as i need to to extract the ports docker generate me.
no option to inject them to docker .
version: '3.3'
services:
oracleTest:
image: ora_image
container_name: "test1"
ports:
- 15211:1521
mysqlTest:
image: mysql:8.0
container_name: "test2"
restart: always
ports:
- 33061:3306
what I found is that I can get the ports by container name , this is not what I need
I can get the services with :
docker-compose ps --services
also can cat the mapped port with docker command again not what I need
docker port mysql8.0 | cut -d':' -f 2
what I need is something like this pseudo code:
docker-compose -f compose_file.yml port oracleTest
or any think like this ... any idea?
If you want ports to be access explicitly anyway, then you might want to inject the port into docker-compose.yml file vie environment variables.
Say you have .env file (default for docker-compose to look up) as follow
oraclePort=15211
mysqlPort=33061
you can then edit your docker-compose.yml to accept variable as
version: '3.3'
services:
oracleTest:
image: ora_image
container_name: "test1"
ports:
-$oraclePort:1521 #<-- notice the variable
mysqlTest:
image: mysql:8.0
container_name: "test2"
restart: always
ports:
-$mysqlPort:3306 #<-- notice the variable
hope this is what you need.
Since docker-compose.yml is a regular YAML file, you can use yq as YAML shell parser.
kislyuk/yq: Command-line YAML and XML processor - jq wrapper for YAML/XML documents
Command-line YAML and XML processor - jq wrapper for YAML/XML documents https://kislyuk.github.io/yq/
install yq and jq
pip install yq
pip install jq
Parse ports from docker-compose.yml
cat docker-compose.yml | yq '.services|.[].ports'
[
"15211:1521"
]
[
"33061:3306"
]
Get ports of oracleTest
what i need is something like this pseudo code:
docker-compose -f compose_file.yml port oracleTest
To get ports of specific service, use:
cat docker-compose.yml | yq '.services.oracleTest.ports'
[
"15211:1521"
]

docker-compose: .env file is not loaded

My docker-compose.yml
version: '3'
services:
test:
build: .
My Dockerfile
FROM alpine:latest
ENTRYPOINT echo $ARG1 $ARG2
My .env file
ARG1=argument100
ARG2=argument200
If I run docker-compose run test the empty line got printed. However, it I add env_file section to my docker-compose.yml I am getting the expected output.
New docker-compose.yml
version: '3'
services:
test:
build: .
env_file:
- .env
Running docker-compose again
$ docker-compose run test
argument100 argument200
Am I using an illegal way to pass arguments to the ENTRYPOINT command inside the Docker? Why is .env not loaded automatically upon docker-compose run?
The .env file is used to set variables in the docker-compose command itself that can be used to expand variables in the docker-compose.yml file itself. The env_file specification will define variables inside the containers. They operate at two different levels.
There's also a third type of variable, build args, that can be injected into the building of an image.
Each of these has a distinct purpose, and are not multiple ways to do the same thing.

Environment variable not set in container when using `environment` key

Following these instructions, I tried setting an environment variable with -e TC=3 and in the compose file like so:
services:
balancer:
environment:
- TC=3
But the variable is not set when the container is run.
Does anyone see what I'm doing wrong?
I'm using:
docker-compose 1.23.1, build b02f1306
Docker 18.06.1-ce, build e68fc7a
The way you are setting the environment is correct. With this compose file
version: '2'
services:
test:
environment:
- HELLO=WORLD
image: alpine
command: env
I got this output
$ docker-compose -f test-compose.yml up
Creating network "sandbox_default" with the default driver
Creating sandbox_test_1 ... done
Attaching to sandbox_test_1
test_1 | PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
test_1 | HOSTNAME=e2eb1a0da23e
test_1 | HELLO=WORLD
test_1 | HOME=/root
sandbox_test_1 exited with code 0
If you want to be able to override a variable written in compose file, then you need to use ${var_name} syntax, e.g.
environment:
- HELLO=${hello_value}

env_file is ignored in docker-compose and cause "The ... variable is not set" warning

I simply want to use a environment variable loaded from file in my docker-compose file. But after running the container, I only got
WARNING: The TESTVAR variable is not set. Defaulting to a blank string.
Only found this topic, but I'm using a later version of docker like there (docker-compose: 1.14.0, docker: 17.05.0-ce). And I changed the encoding to ISO 8859-1, since I found a github issue where strange behavior with encodings was detected. Both doesn't work.
My docker-compose file
version: '2'
services:
mysql:
container_name: test_${TESTVAR}
build: mysql
mem_limit: 1G
env_file:
- credentials.env
credentials.env contains only TESTVAR=test123. To start, I run docker-compose up mysql and I also tried to specify the environment variables directly in the compose file like this:
environment:
- TESTVAR=1234
Not working, too.
If you want to use variables in the docker-compose.yml you can do it with .env file, docker docs
$ cat .env
TAG=v1.5
TESTVAR=123
$ cat docker-compose.yml
version: '3'
services:
web:
image: "webapp:${TAG}"
environment: ["TESTVAR=${TESTVAR}"]

Resources