docker: invalid reference format in shell script - bash

I'm trying to create a shell script to run a docker container and am struggling. My script is like this:
#!/bin/bash
if [ "$1" == "" ]; then
echo "Usage > run.sh IMAGE NAME"
echo
echo "i.e. ./build.sh cd2:0.0.49"
exit
fi
echo $1
docker run -it --rm \
-e NODE_PATH='./src'\
-e NODE_HOST='0.0.0.0'\
-e NODE_ENV='production'\
-e DOCKER=true\
-e PORT='8080'\
-e STAGING=true\
-e SENDGRID_API_KEY='<redacted>'\
-p 8080:8080 $1
When I run: bash run.sh cd2:0.0.50
I get: docker: invalid reference format: repository name must be lowercase.
Even if I do bash run.sh cd:0.0.50 it still fails (echo $1 results in cd2:0.0.50).
If I run docker run -it --rm -p 8080:8080 cd2:0.0.50 from the command line it works...
Can anyone help?

docker run \
-e NODE_PATH='./src' \
-e NODE_HOST='0.0.0.0' \
-e NODE_ENV='production' \
-e DOCKER=true \
-e PORT='8080' \
-e STAGING=true \
-e SENDGRID_API_KEY='<redacted>' \
-p 8080:8080 --rm -it $1
The image name should be immediately after the -it parameter and so re arrange your run command.

Related

multi-line command line in bash fails while equal one line command line succeeds

I try to figure out why the following command fails
docker run \
--rm \
--name somedb \
-v "$(pwd)/database:/var/lib/mysql/" \
-p "3306:3306" \
-e MARIADB_USER=dbuser \
-e MARIADB_PASSWORD=dbuserpwd \
-e MARIADB_ROOT_PASSWORD=rootpwd \
-d \
mariadb:10.10-jammy
with the message:
docker: invalid reference format.
See 'docker run --help'.
-e : commande introuvable ## command could not be found in french
while the equivalent one line command line
docker run --rm --name somedb -v "$(pwd)/database:/var/lib/mysql/" -p "3306:3306" -e MARIADB_USER=dbuser -e MARIADB_PASSWORD=dbuserpwd -e MARIADB_ROOT_PASSWORD=rootpwd -d mariadb:10.10-jammy
succeeds.
There's a space after your -e MARIADB_USER=dbuser \
This worked
docker run \
--rm \
--name somedb \
-v "$(pwd)/database:/var/lib/mysql/" \
-p "3306:3306" \
-e MARIADB_USER=dbuser \
-e MARIADB_PASSWORD=dbuserpwd \
-e MARIADB_ROOT_PASSWORD=rootpwd \
-d \
mariadb:10.10-jammy

How can echo the .env.list file in docker run command in jenkins

I would like to echo out the .env.list values used in the docker run command in Jenkins, I have tried as follows echo "Print the env.list file: ${.env.list}"
Could someone please advise how can I print the .env.list file in docker run command
sh """
docker run --env-file bookpro-co/.env.list ${baseUrlConfig} \
-v \"${ARTEFACT_DIR}:/artefacts\" \
-v \"${env.WORKSPACE}/bookpro-co:/bookpro-test\" \
cypress:latest \
/node_modules/.bin/cypress-tags ${cypressArgs}
echo "Print the env.list file: ${.env.list}"
"""

Why does my bash script move beyond "docker run" even tho docker is still running?

I am trying with a bash script to run a docker container then print a message. However the finished message is executed whilst the container is still running - I can exec into it and see PID 1 and multiple other processes.
How can I force the docker run command to complete first?
docker run --name registr \
-v ~/v1:/v1 \
-v ~/logging.yaml:/root/logging.yaml \
-v ~/.aws:/root/.aws \
-v ~/luigi.cfg:/root/luigi.cfg \
-v ~/params:/root/params \
-p 8082:8082 \
simonm3/registr
echo "docker finished"
The docker image has CMD ["python", "/root/worker/start.py"]

How to break up command in CircleCI yml to multiple lines? [duplicate]

This question already has answers here:
How do I break a string in YAML over multiple lines?
(9 answers)
Closed 5 years ago.
I have a CircleCI configuration file that looks like so:
# Customize test commands
test:
override:
- docker run -e VAR1=$VAR! -e VAR2=$VAR2 -e $VAR3-$VAR3 --entrypoint python my_image:latest -m unittest discover -v -s test
How can I break up the docker run command into multiple lines like:
docker run \
-e VAR1=$VAR! \
-e VAR2=$VAR2 \
-e $VAR3-$VAR3 \
--entrypoint python my_image:latest \
-m unittest discover -v -s test
I've tried using the | operator for yaml, but CircleCI was unable to parse because it expects override to be a list.
# Customize test commands
test:
override: |
docker run \
-e VAR1=$VAR! \
-e VAR2=$VAR2 \
-e $VAR3-$VAR3 \
--entrypoint python my_image:latest \
-m unittest discover -v -s test
Using this answer which details the various ways to break up a string over multiple lines in yaml, I was able to deduce a solution which works nicely.
Note the use of the >- operator in the override section.
test:
override:
- >-
docker run
-e VAR1=$VAR!
-e VAR2=$VAR2
-e $VAR3-$VAR3
--entrypoint python my_image:latest
-m unittest discover -v -s test
This generates a nice single-line command of:
docker run -e VAR1=$VAR! -e VAR2=$VAR2 -e $VAR3-$VAR3 --entrypoint python my_image:latest -m unittest discover -v -s test

How to detect fully interactive shell in bash from docker?

I'm wanting to detect in "docker run" whether -ti has been passed to the entrypoint script.
docker run --help for -t -i
-i, --interactive=false Keep STDIN open even if not attached
-t, --tty=false Allocate a pseudo-TTY
I have tried the following but even when tested locally (not inside docker) it didn't work and printed out "Not interactive" always.
#!/bin/bash
[[ $- == *i* ]] && echo 'Interactive' || echo 'Not interactive'
entrypoint.sh:
#!/bin/bash
set -e
if [ -t 0 ] ; then
echo "(interactive shell)"
else
echo "(not interactive shell)"
fi
/bin/bash -c "$#"
Dockerfile:
FROM debian:7.8
COPY entrypoint.sh /usr/bin/entrypoint.sh
RUN chmod 755 /usr/bin/entrypoint.sh
ENTRYPOINT ["/usr/bin/entrypoint.sh"]
CMD ["/bin/bash"]
build the image:
$ docker build -t is_interactive .
run the image interactively:
$ docker run -ti --rm is_interactive "/bin/bash"
(interactive shell)
root#dd7dd9bf3f4e:/$ echo something
something
root#dd7dd9bf3f4e:/$ echo $HOME
/root
root#dd7dd9bf3f4e:/$ exit
exit
run the image not interactively:
$ docker run --rm is_interactive "echo \$HOME"
(not interactive shell)
/root
$
This stackoverflow answer helped me find [ -t 0 ].

Resources