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

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

Related

How to add Infrastructure Agent to Heroku applications

I am attempting to have the New Relic Infrastructure Agent monitor my heroku applications.
The documentation says to run the following:
docker run \
-d \
--name newrelic-infra \
--network=host \
--cap-add=SYS_PTRACE \
--privileged \
--pid=host \
-v "/:/host:ro" \
-v "/var/run/docker.sock:/var/run/docker.sock" \
-e NRIA_LICENSE_KEY=[Key]\
newrelic/infrastructure:latest
But where do I actually run or put this so it runs it on my Heroku apps?

Docker behaving odd with bash environment vars

I know technically host networking isn't supported MacOS (see https://docs.docker.com/network/host/)
The host networking driver only works on Linux hosts, and is not
supported on Docker Desktop for Mac, Docker Desktop for Windows, or
Docker EE for Windows Server.
However it does actually seem to work. E.g. this works just fine:
docker run \
--name local-mysql \
-e MYSQL_ROOT_PASSWORD=foo \
-e MYSQL_DATABASE=baz \
--network="host" \
-d mysql:latest
However when I try to conditionally specify the host networking with a bash variable, it doesn't work, and I can't make sense of it. Consider the following test.sh:
#!/bin/bash
echo "Test 1"
docker rm -f local-mysql
docker run \
--name local-mysql \
-e MYSQL_ROOT_PASSWORD=foo \
-e MYSQL_USER=master \
-e MYSQL_PASSWORD=bar \
-e MYSQL_DATABASE=baz \
--network="host" \
-d mysql:latest
docker ps
sleep 5
echo "Test 2"
export NETWORKING='--network="host"'
docker rm -f local-mysql
docker run \
--name local-mysql \
-e MYSQL_ROOT_PASSWORD=foo \
-e MYSQL_USER=master \
-e MYSQL_PASSWORD=bar \
-e MYSQL_DATABASE=baz \
${NETWORKING} \
-d mysql:latest
docker ps
This yields:
% ./test.sh
Test 1
local-mysql
6bbd68f0564943b8fb66ed37f1e639b54719bdb3b88b4e13aeef0a11cae4090b
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6bbd68f05649 mysql:latest "docker-entrypoint.s…" Less than a second ago Up Less than a second local-mysql
Test 2
local-mysql
e286028ef9a1a27f4226beb60e766cc163c289239ba506f63a71a35adbc73ef3
docker: Error response from daemon: network "host" not found.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
I.e. when I hard code --network=host into the docker command, the container starts fine. But the exact same parameter in an environment variable fails to start with network "host" not found.
I'm honestly not sure if this is a failure of bash or docker, but I can't actually figure out what's going wrong.
-- EDIT --
Changing
export NETWORKING='--network="host"'
to
export NETWORKING='--network=host'
works. And for my purposes right now that's enough. But just to be thorough... Why? The working example has quotes in the value (--network="host"), so why does the shell expansion break the non-working example? What if I wanted something like --network="my host"?

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"]

docker: invalid reference format in shell script

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.

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

Resources