bash: C:/Program: No such file or directory - bash

I am new to Docker, Debezium, Bash, and Kafka. I am attempting to run the Debezium tutorial/example for MSSQL Server on Windows 10 here:
https://github.com/debezium/debezium-examples/blob/master/tutorial/README.md#using-sql-server
I am able to start the topology, per step one. However, when I go to step two and execute the following command:
cat debezium-sqlserver-init/inventory.sql | docker exec -i tutorial_sqlserver_1 bash -c '/opt/mssql-tools/bin/sqlcmd -U sa -P $SA_PASSWORD'
I get the following error:
bash: C:/Program: No such file or directory
I do not have the foggiest idea why it would even drag C:/Program in to this. I do not see it in the command nor do I see it in the *.sql file. Does anyone know why this is happening and what the fix is?
Note 1: I am already in the current directory where this command should be runnable and there are no spaces in the folder/file path
Note 2: I am running the commands in Git Bash
When using set -x to log how the command is run, there's still no C:/Program anywhere in it, as can be seen by the following log:
$ cat debezium-sqlserver-init/inventory.sql | docker exec -i tutorial_sqlserver_1 bash -c '/opt/mssql-tools/bin/sqlcmd -U sa -P $SA_PASSWORD'
+ cat debezium-sqlserver-init/inventory.sql
+ docker exec -i tutorial_sqlserver_1 bash -c '/opt/mssql-tools/bin/sqlcmd -U sa -P $SA_PASSWORD'
bash: C:/Program: No such file or directory

I had a similar problem yesterday, the solution was adding a backslash before the absolute path, like :
cat debezium-sqlserver-init/inventory.sql | docker exec -i tutorial_sqlserver_1 bash -c '\/opt/mssql-tools/bin/sqlcmd -U sa -P $SA_PASSWORD'
\/opt/mssql-tools/bin/sqlcmd prevents conversion to Windows path.

Related

executing shell command in docker composer gives me error

I'm trying to execute shell command in docker-compose.yml. Code is:
command: bash -c mkdir /opt/wa/usr/install_templates
When I do:
sudo docker-compose up serviceA
it gives me :
serviceA | mkdir: missing operand
When you use bash -c, it runs the first string after the -c flag. See this SO answer for more information. Docker is reading your command bash -c mkdir /path/ and your bash command is just running mkdir in a bash subshell, causing that error.
You don't need to put bash -c before your commands in a docker-compose file. The Docker engine will handle running it in a shell for you and you can simply write the command you want to be run. I'd suggest replacing your command with this:
command: mkdir /opt/wa/usr/install_templates
Alternatively, you could try putting the entire command into a string if you want to force the command to be run in bash:
command: bash -c "mkdir /opt/wa/usr/install_templates"

Bash script for running postgres docker image failing on linux

The command which on line 4 on the script below seems to have an issue, intellij says
which is non-standard. Use builtin 'command -v' instead
Since which psql seems like it is not working it automatically affects line 12 and 13.
While investigating i removed line 4 then the script executed line 6 to 10 which succefully created a docker file(pg-docker) however i also need the schema.sql (line 12) and data.sql (13) to be executed. Is there an alternative command for which command(line 4)
Below is my bash Script:
#!/usr/bin/env bash
set -euo pipefail
which psql > /dev/null || (echo "Please ensure that postgres client is in your PATH" && exit 1)
mkdir -p $HOME/docker/volumes/postgres
rm -rf $HOME/docker/volumes/postgres/data
docker run --rm --name pg-docker -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=dev -d -p 432:5432 -v $HOME/docker/volumes/postgres:/var/lib/postgresql postgres
sleep 3
export PGPASSWORD=postgres
psql -U postgres -d dev -h localhost -f schema.sql
psql -U postgres -d dev -h localhost -f data.sql
I get the below on the problems on Intellij
line 4 complains about which command
line 6,7 and 9 complains about $HOME
line 11 complains about PGPASSWORD
which is used to find and show the full path of a command (in your script it is only used to make sure the command psql is there).
IntelliJ or probably better the defined linter for (bash) scripts suggest not to rely on an separate whichcommand but just use the builtin bash-function command -v so the line 4 would read
command -v psql > /dev/null || (echo "Please ensure that postgres client is in your PATH" && exit 1)
That said - it's most likely not your real problem. You need the PostgreSQL Client psql installed and in your PATH variable to run the commands in line 12 and 13. Exactly that's what you're checking in line 4 - regardless of using which or command -v.
How to install the psql command depends on your OS.

How to set flags for executable in a Dockerfile RUN command?

I am trying to create a Docker image from a dockerfile, wherein I have an executable file. I want to set some flags for the executable, as example: An executable file with a silent flag and a flag for a ressource file. How can I do this in a RUN command in a Dockerfile?
I have so far tried to do:
RUN /bin/bash -c 'PATH/TO/EXECUTABLE -i silent -f ressourceFile.properties',
this just returns an exit code of non-zero 1.
If I try to start the intermediate container from the layer before, and run the command with a docker exec -it container_id /bin/bash -c 'PATH/TO/EXECUTABLE -i silent -f file.properties' .
It gives me
-i: -c: line 0: unexpected EOF while looking for matching `''
-i: -c: line 1: syntax error: unexpected end of file
I have tried to run it as
RUN 'PATH/TO/EXECUTABLE -i silent -f ressourceFile.properties'
with the same result
And I have also tried to convert the RUN command to exec form per Dockers documentation
RUN ["/bin/bash", "-c", "PATH/TO/EXECUTABLE", "-i", "silent", "-f", "file.properties"]
but then it is unable to catch the flags.
Currently my dockerfile looks like this, with redacted executable:
FROM perl
COPY EXECUTABLE.bin ressourceFile.properties /tmp/
RUN chmod +x /tmp/EXECUTABLE.bin
RUN /bin/bash -c '/tmp/EXECUTABLE.bin -i silent -f /tmp/ressourceFile.properties'
# For debug purposes
CMD ["tail", "-f", "/dev/null"]
Expected result should be that the executable runs with the correct flags set and the path to the file.properties file correctly set. Hope you guys can help.

Running `docker run` from bash script fails. Command does not fail on Command Line

If I run the following from the command line.
docker run -t repo:tag ls -l
the command succeeds just fine. However, if I invoke the same from within a bash script I get the following ERROR:
docker: Error response from daemon: OCI runtime create failed:
container_linux.go:348: starting container process caused "exec: \"ls
-l\": executable file not found in $PATH": unknown.
What about the bash script causes this error?
"exec: \"ls -l\": executable file not found in $PATH"
From the error I can tell that when you invoke docker, you somehow invoke with ls -l including space as one argument. Something like,
docker run -t repo:tag "ls -l" # wrong
or perhaps
cmd="ls -l"
docker run -t repo:tag "$cmd" # wrong
The shell to parse the docker command must see ls and -l as separate parameters so that the argument -l is distinguished from the ls executable name.
cmd="ls -l"
docker run -t repo:tag $cmd #works

Exec sed command to a docker container

I'm trying to change a config file that is inside a docker container.
docker exec container_name sed -ire '/URL_BASE = /c\api.myapiurl' tmp/config.ini
Executing this sed command locally works just fine, but when I try to execute this in the container I receive the following error message.
sed: cannot rename tmp/config.ini: Operation not permitted
What I need to do is replace the 'URL_BASE =' from the 'config.ini' before deploy the container to my server.
I don't know why the sed command is trying to rename the file when its not suppose to.
Any ideas?
What I've tried
I tried to execute with the --privileged flag, but didn't worked. I tried to change the file permissions with chmod but I couldn't for the same reason of permission.
docker exec --privileged container_name sed -ire '/URL_BASE = /c\api.myapiurl' tmp/config.ini
Result: sed: cannot rename tmp/config.ini: Operation not permitted
Chmod
docker exec --privileged container_name chmod 755 tmp/config.ini
Result: chmod: changing permissions of 'tmp/config.ini': Operation not permitted
I also have tried execute with sudo before docker but didn't work either.
Nehal is absolutely right, sed works creating a local file so you just need a different approach, which is commonly used on Linux: heredocs.
Taking just the first lines from the documentation, a here document is a special-purpose code block. It uses a form of I/O redirection to feed a command list to an interactive program.
It can help us with docker exec as follows:
docker exec -i container_name bash <<EOF
sed -ire '/URL_BASE = /c\api.myapiurl' /tmp/config.ini
grep URL_BASE /tmp/config.ini
# any other command you like
EOF
Be aware of the -t, which is commonly used running bash, because it allocates a pseudo-TTY, and we don't really need that.
Also, to be safe always use absolute paths like /tmp/config.ini.
docker exec -i <container name> sed -i 's/xxx/${yyy}/g' path/filename.yaml
This is working for me.

Resources