How to pass arguments with space by environment variable? - bash

On bash shell, I want to pass argument by environment variable.
like this...
$ export DOCKER_OPTIONS="-p 9200:9200 -e ES_JAVA_OPTS='-Xmx1g -Xms1g' -d "
$ docker run -d $DOCKER_OPTIONS elasticsearch
I expect that "ES_JAVA_OPTS='-Xmx1g -Xms1g'" is passed as an option value of "-e". But I couldn't find a way.
$ set -x
$ docker run -d $DOCKER_OPTIONS elasticsearch
+ docker run -d -p 9200:9200 -e 'ES_JAVA_OPTS='\''-Xmx1g' '-Xms1g'\''' elasticsearch
unknown shorthand flag: 'X' in -Xms1g'
This separated -Xms1g as an another option.
$ docker run -d "$DOCKER_OPTIONS" elasticsearch
+ docker run -d '-p 9200:9200 -e ES_JAVA_OPTS='\''-Xmx1g -Xms1g'\''' elasticsearch
docker: Invalid containerPort: 9200 -e ES_JAVA_OPTS='-Xmx1g -Xms1g'.
This bundled the parameters together.
What should I do?

Use an array to circumvent these awkward parsing problems. Arrays are great because you don't need to do any special quote when defining them. The only place you have to be careful with quotes is when expanding them: always put quotes around "${array[#]}".
dockerOptions=(-p 9200:9200 -e ES_JAVA_OPTS='-Xmx1g -Xms1g' -d)
docker run -d "${dockerOptions[#]}" elasticsearch
Note that export isn't needed since you're passing the options to docker via its command-line rather than as an environment variable.
Also, all uppercase names are reserved for the shell. It's best to avoid them when defining your own variables.

Related

Escape $ (dollar) into bash command in docker compose is not interpreted

I would need some help with docker compose and the $ caracter.
Here an example of container in docker compose :
services:
setup:
image: image:tag
container_name: setup
user: "0"
command: >
/bin/sh -c '
sed -i 's/before with a $$/after with a $$/' /foo/bar/something.txt;
'
When I try with this, the sed command goes without any $, even if I escaped it twice. What am I missing?
Best regards,
On the docker compose side, I see there is no other way to escape the $ caracter : I have to double-it with another $ : $$
However, in the bash command, no $ is interpreted, so the sed command doesn't work

How to pass ALL environment variables to container with docker exec

It's possible to set one or more environment variables in the container while doing docker exec, for example:
docker exec -ti -e VAR=1 -e HOME container_name command
But I would like to pass all the shell's environment variables without explicitly specifying them individually. Essentially the equivalent of sudo -E, although it's a different thing.
According to the documentation, there is no such option. But one hack would be something like:
env > env_vars && docker exec -ti --env-file ./env_vars container_name command
Which works, but I'm looking for a simple one step solution that doesn't involve creating a temporary file. Perhaps a bash trick I don't know or haven't thought of yet. Thanks.
Please note: Passing all environment variables is not recommended and defeats the purpose of container process isolation. This question is for knowledge, not about what should be done. Also, the question is specifically about running a temporary command in an existing container with docker exec, not about docker run.
With Bash it seems using process substitution work:
docker run --rm -ti --env-file <(env) alpine sh
Note, this creates a temporary fifo file behind the scenes anyway.
Note, this will not work properly with variables containing newlines, they are cutoff on newlines. You should do something along, I tried to make it short:
readarray -d '' -t args < <(env -0 | sed -z 's/^/--env\x00/')
docker run --rm -ti "${args[#]}" alpine sh

How can I script a Docker command into a 'single word' binary? Using bash script?

When I install something like nmap(even from APT), I cant get it to execute correctly, so I like to go the container route. Instead of typing:
docker run --rm -it instrumentisto/nmap -A -T4 scanme.nmap.org
I figured maybe I could script it out, but nothing i've learned or found on google, youtube, etc, has helped so far... Can somebody lend a hand? I need to know how to get Bash to execute a command with args:
execute like:
./nmap.sh -A -T4 -Pn x.x.x.x
#!/bin/bash
echo docker run --rm -it instrumentisto/nmap $1 $2 $3 $4 $5
but how to get bash to run this instead of just echo I dont know. Thanks ahead!
Two solutions: create an alias, create a script.
With an alias
The command you write is replaced with the value of the alias, so
alias nmap="docker run --rm -it instrumentisto/nmap"
nmap -A -T4 -Pn x.x.x.x
# executes docker run --rm -it instrumentisto/nmap -A -T4 -Pn x.x.x.x
Aliases are not persistent so you will have to store it in some bash config (generally ~/.bashrc).
With a script
#!/bin/bash
set -Eeuo pipefail
docker run --rm -it instrumentisto/nmap "$#"
"$#" will forward all the arguments provided to the script directly to the command. The quotes are important, if you call your script with quoted values like ./nmap "something with spaces", that's one argument, it needs to be kept as one argument.
Bonus: With a function
Just like the script, you need to forward arguments when writing functions, just like aliases, they are not persistent so you have to store them in bash config:
nmap() {
docker run --rm -it instrumentisto/nmap "$#"
}

Save output of bash command from Dockerfile after Docker container was launched

I have a Dockerfile with ubuntu image as a base.
FROM ubuntu
ARG var_name
ENV env_var_name=$var_name
ENTRYPOINT ["/bin/bash", "-c", "echo $env_var_name"]
I expect from this
executing of a simple bash script, which will take an environment variable from user keyboard input and output this value after running docker container. It goes right.
(part where i have a problem) saving values of environment variables to file + after every running of docker run --rm -e env_var_name=%valueOfVar% IMAGE-NAME i can see a list of entered from keyboard values.
My idea about part 2 were like
docker run --rm -e env_var_name=%valueOfVar% IMAGE-NAME > /directory/tosave/values.txt. That works, but only one last value saves, not a list of values.
How can i change Dockerfile to save values to a file, which Docker will see and from which Docker after running will read and ouyput values? May be i shouldn`t use ENTRYPOINT?
Appreciate for any possible help. I`ve stuck.
Emphasizing that output and save of environment variables expected.
Like #lojza hinted at, > overwrites files whereas >> appends to them which is why your command is clobbering the file instead of adding to it. So you could fix it with this:
docker run --rm -e env_var_name=%valueOfVar% IMAGE-NAME >> /directory/tosave/values.txt
Or using tee(1):
docker run --rm -e env_var_name=%valueOfVar% IMAGE-NAME | tee -a /directory/tosave/values.txt
To clarify though, the docker container is not writing to values.txt, your shell is what is redirecting the output of the docker run command to the file. If you want the file to be written to by docker you should mount a file or directory into it the container using -v and redirect the output of the echo there. Here's an example:
FROM ubuntu
ARG var_name
ENV env_var_name=$var_name
ENTRYPOINT ["/bin/bash", "-c", "echo $env_var_name | tee -a /data/values.txt"]
And then run it like so:
$ docker run --rm -e env_var_name=test1 -v "$(pwd):/data:rw" IMAGE-NAME
test1
$ docker run --rm -e env_var_name=test2 -v "$(pwd):/data:rw" IMAGE-NAME
test2
$ ls -l values.txt
-rw-r--r-- 1 root root 12 May 3 15:11 values.txt
$ cat values.txt
test1
test2
One more thing worth mentioning. echo $env_var_name is printing the value of the environment variable whose name is literally env_var_name. For example if you run the container with -e env_var_name=PATH it would print the literal string PATH and not the value of your $PATH environment variable. This does seem to be the desired outcome, but I thought it was worth explicitly spelling this out.

How to call multiple multiline commands in a yml script?

I'm not quite sure how to add a multiline script with multiple commands in a yml file of my CI - which is in my case a .gitlab-ci.yml:
production:
stage: deploy
image: ${DOCKER_IMAGE}
script:
- while IFS='-' read -r dom app; do
docker stop "$dom-$app" || true &&
docker rm "$dom-$app" || true
docker run
--name "$dom-$app"
--detach
--restart=always
-e VIRTUAL_HOST=$dom
"$dom-$app":latest
done < $FILE
So what I'm doing here is to read a file with a list of apps. For each line I have to stop the existing docker image, remove it and run the new one with some parameters.
How do I have to connect the docker commands (stop, rm and run)? Maybe a &&?
Do I have to use " for $dom-$app?
There are several ways to create multiline strings in YAML.
The way you are using the multiline plain string, all lines will be folded together with spaces.
Also your last line of the string isn't indented enough.
Longer strings like that should be quoted, because chances are high that there is a : or # inside the string which is special in YAML.
I suggest using literal block style, because that means the text will be interpreted exactly as you see it:
script:
- |
while IFS='-' read -r dom app; do
docker stop "$dom-$app" || true
docker rm "$dom-$app" || true
docker run \
--name "$dom-$app" \
--detach \
--restart=always \
-e VIRTUAL_HOST=$dom \
"$dom-$app":latest
done < $FILE
(Note that sequences (items starting with -) don't have to be indented, that's why the - is directly below script.)
You can find more information about YAML quoting styles on my website:
https://www.yaml.info/learn/quote.html

Resources