Redirecting aws cli v2 to file adds extra control characters - bash

If I run the following command:
docker run -v "$PWD/aws":/root/.aws --rm -it amazon/aws-cli wafv2 list-ip-sets --profile dev --scope=CLOUDFRONT --region=us-east-1 --color off
I get the following output:
{
"IPSets": []
}
If I run the following command:
docker run -v "$PWD/aws":/root/.aws --rm -it amazon/aws-cli wafv2 list-ip-sets --profile dev --scope=CLOUDFRONT --region=us-east-1 --color off > test.txt
I get the following in test.txt:
[?1h=
{[m
"IPSets": [][m
}[m
[K[?1l>
I think these are xterm control codes or something - in any case, how do I get the contents of test.txt to match what is output to the terminal? I am on a Mac but my solution needs to work on Mac and Linux.

Apparently that -it parameter being passed to Docker was the problem. Passing just just -i made it work.

Related

Bash file for docker invalid reference $PWD

I wrote a bash file to automatically build a docker image and run the docker. The build goes fine however i get an invalid reference error for using $PWD. The command is as follows:
#!/bin/bash
app="docker.test2"
docker build -t ${app} .
docker run --rm -it -p 5000:5000 -v $PWD:/usr/src/Deployment -w /usr/src/Deployment/microblog2 ${app} flask run --host 0.0.0.0
When I replace the $PWD with the actual file path it works fine. I also tried replacing it with $pwd, $(pwd) and ${pwd} but to no avail. The error is as follows:
Docker/test:latest
should work fine

Can't open dashboard

I'm using Windows and installed Zalenium with the .\prepare.bat
Then, when i try o start Zalenium with:
docker run --rm -ti --name zalenium -p 4444:4444
-v /var/run/docker.sock:/var/run/docker.sock
-v /tmp/videos:/home/seluser/videos
--privileged dosel/zalenium start
I get an error on the console:
Copying files for Dashboard...
cp: cannot create regular file '/home/seluser/videos/dashboard.html': No such file or directory
Everything works except the Dashboard.
What am i doing wrong?
I'm using the latest version.
Thank you
Error clearly say that it is trying to look for the file in LINUX like structure "/home/seluser/videos/" which will not be available on windows.
When you start zalenium, It looks for dashboard.html in the mount drive. Without this file dashboard will not be visible.
You should use below command in case of windows.
docker run --rm -ti --name zalenium -p 4444:4444 ^
-v /var/run/docker.sock:/var/run/docker.sock ^
-v /c/Users/your_user_name/temp/videos:/home/seluser/videos ^
--privileged dosel/zalenium start
Zalenium documentation
I'm new to Zalenium but I found out that the run command on the Zalenium Github page does not work on all systems.
Try this command i use and let me know if it works for
*docker run --rm -ti --name zalenium -p 4444:4444 -v /var/run/docker.sock:/var/run/docker.sock --privileged dosel/zalenium start*
docker run -d -ti --name zalenium -p 4445:4444 -v /var/run/docker.sock:/var/run/docker.sock -v /tmp/videos:/home/seluser/videos --restart=always --privileged dosel/zalenium start
worked for me.. dashboard opens on 4445 port

How to pass environment variable spring.main.web-application-type=SERVLET to run docker image

When I use docker run command, the variable "SPRING_PROFILES_ACTIVE" is right, but "SPRING_MAIN_WEB-APPLICATION-TYPE" does not work, how to pass "SPRING_MAIN_WEB-APPLICATION-TYPE" to dokcer image?
sudo docker run -d -e SPRING_PROFILES_ACTIVE=product -e SPRING_MAIN_WEB-APPLICATION-TYPE=SERVLET -e SERVER_PORT=6789 --network mongo_network
As described in the documentation, the expected format for that environment variable is SPRING_MAIN_WEBAPPLICATIONTYPE.
I use docker ENV to get it worked, here is the Dockerfile:
FROM openjdk:8-jre
ENV TYPE NONE
COPY data.jar data.jar
CMD ["java","-jar","data.jar", "--spring.main.web-application-type=${TYPE}"]
then run the docker image:
sudo docker run -d -e SPRING_PROFILES_ACTIVE=product -e TYPE=SERVLET -e SERVER_PORT=6789
hope this helps.

How does docker run interpret dynamically generated --env arguments

I am trying to provide a dynamically generated list of --env VAR1 --env VAR2 --env-file env.list environment variables to docker run.
Unfortunately it is not working.
for --env mapped variables, the variables are not visible in the container.
for --env-file provided file, docker complains that it cannot find the file: docker: open "env.list": no such file or directory.
Details
Running:
# env_params contains either --env or --env-file arguments
MY_VAR=123
env_params='--env "MY_VAR"'
echo ${env_params}
docker run -it --rm \
${env_params} \
my_docker_image env | grep MY_VAR
will not output anything. MY_VAR is not visible inside the container. But:
MY_VAR=123
docker run -it --rm \
--env "MY_VAR" \
my_docker_image env | grep MY_VAR
will work and 123 will be printed.
In a similar way --env-file will not work when provided through env_params but will work when provided directly to the docker run command.
What am I doing wrong?
There are two issues here.
First, When you run, in your shell:
MY_VAR=123
You have not set an environment variable. You have set a local shell variable. When you use --env MY_VAR, you are telling Docker that you want to make the environment variable MY_VAR available inside the container, and since it doesn't exist you get nothing:
$ MY_VAR=123
$ docker run -it --rm -e MYVAR alpine env | grep MY_VAR
<crickets>
If you first export that to the environment:
$ export MY_VAR=123
$ docker run -it --rm -e MYVAR alpine env | grep MY_VAR
MY_VAR=123
Then it will work as you expect. Alternately, you can use the VARNAME=VARVALUE form of the --env option:
docker run -e "MY_VAR=${MY_VAR}" ...
The second issue has to do with how shell variable interpolation works. If you have:
env_params='--env "MY_VAR"'
docker run -it --rm \
${env_params} \
alpine env
Then the resulting command line is:
docker run -it --rm --env '"MY_VAR"' alpine env
That is, the argument you're passing to docker run includes literal double quotes. You can fix that through the use of the eval statement (keeping in mind that you'll need to modify your script to export MY_VAR):
eval docker run -it --rm \
${env_params} \
alpine env | grep MY_VAR
Alternately (and I would argue preferably) you can use your env_params variable as an array, as long as you're using bash:
env_params=(--env MY_VAR)
env_params+=(--env SOME_OTHER_VAR)
docker run -it --rm \
"${env_params[#]}" \
alpine env | grep MY_VAR
Which would result in the correct command line:
docker run -it --rm --env MY_VAR --env SOME_OTHER_VAR alpine env
I guess the summary here is that your issues ultimately have nothing to do with "how docker run interprets dynamically generated arguments", but have everything to do with "how shell variables and interpolation work".

Passing parameter in docker build command with fish

Using Docker For Mac, fish shell, macOS 10.11
I am trying to run the following command: docker run -d -it --name=my-app-container -v $(pwd):/app -p 3000:3000 myapp
I get the following error:
$(...) is not supported. In fish, please use '(pwd)'.
fish: docker run -d -it --name=my-app-container -v $(pwd):/app -p 3000:3000 myapp
Been reading through repos and SO answers but cant get this to work. Any ideas? Thank you.
The equivalent of bash $(command) in fish is just (command)
So all you need to do is remove the dollar sign.
docker run -d -it --name=my-app-cont -v (pwd):/app -p 3000:3000 myapp
Following #user2915097 's suggestion, seems like this doesn't throw an error....docker run -d -it --name=my-app-cont -v $PWD:/app -p 3000:3000 myapp. So switching $(pwd) to $PWD gets past this error.

Resources