Bash file for docker invalid reference $PWD - bash

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

Related

Executing docker from terminal directly works fine but not when executed from inside a .sh script?

I am on ubuntu 20.04 I installed docker using sudo snap install docker now when I run directly from the terminal (terminal installed with ubuntu) docker command it works fine but when I execute a .sh script from the terminal using either bash ./script.sh or ./script.sh I am getting an error docker: command not found.
This is the script:
#!/bin/bash
source $(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/env.sh
docker run -e "NODE_ENV=dev" -it --rm --name my-npm-2 -v $PROJECT_HOME/code:/var/www/html/code -w /var/www/html/code node:14 npm install
docker run -e "NODE_ENV=dev" -it --rm --name my-npm -v $PROJECT_HOME/code/web:/var/www/html/code/web -w /var/www/html/code/web node:14 npm install
$SCRIPT_HOME/buildjs_dev.sh
docker exec project_php sudo php -d memory_limit=-1 /usr/local/bin/composer install --working-dir=/var/www/html/code
docker exec project_php chown -R www-data:www-data /var/www/html/code/var/cache
docker exec project_php chown -R www-data:www-data /var/www/html/code/var/log
I am new to linux in general and I don't know if the problem is with the script itself or why isn't it recognizing docker?
You are defining a source file at the start of your script which might be changing the PATH variable. Try by either commenting the source line or calling the docker command with full path.

Redirecting aws cli v2 to file adds extra control characters

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.

Docker run to execute script in mount without exiting container automatically?

I have a simple bash script 'test.sh' in the root of mounted folder :
#!/bin/bash
Rscript -e "source('/home/rstudio/mount-folder/src/controller.R')";
However, when i try to mount folder and start the container with docker run as follows:
docker run -d -p 8000:8787 -e ROOT=true -e DISABLE_AUTH=true --name container -v mount-folder/:/home/rstudio/ image_name /home/rstudio/test.sh
above run command starts the container but exits automatically.
I am looking for a docker run command that starts the container , mounts the folder and then executes the bash script which is in the mount-folder without exiting the container.
(** dont want to go with docker exec command as it is not suitable for my use case for other reasons)
Dockerfile:
FROM rocker/rstudio:4.0.2
//some RUN commands to install necessary r packages
EXPOSE 8787
CMD tail -f /dev/null
Other details :
Image that i am using is rstudio server from rocker and container runs on AWS ubuntu machine.
Edit :
have also tried adding CMD tail -f /dev/null at the end of dockerfile as suggested in http://bigdatums.net/2017/11/07/how-to-keep-docker-containers-running/ even then the container exits.
Docker containers shutdown automatically when run in detached mode. I think this article proposes a nice solution:
http://bigdatums.net/2017/11/07/how-to-keep-docker-containers-running/
You could add tail -f /dev/null as the last command in your bash script instead so that the script will never halt unless it is told to do so.
When you do docker run [options] image_name [cmd] the command you specify becomes the command for the container and replaces any the command specified in the dockerfile (that's why adding CMD tail -f /dev/null doesn't do anything). If you ran your container without the /home/rstudio/test.sh at the end, it should stay running.
The solution would be to update your script to add the tail command at the end.
#!/bin/bash
Rscript -e "source('/home/rstudio/mount-folder/src/controller.R')";
exec tail -f /dev/null
If you can't update that script, you could instead add it to the command being passed to the container, with something like:
docker run [options] image_name bash -c '/home/rstudio/test.sh && exec tail -f /dev/null'

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.

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