Run docker command in bash file and run using crontab - bash

#!/bin/bash
docker exec -ti erpnext sh -c "cd /home/frappe/frappe-bench/ &&
/usr/local/bin/bench backup"
echo 'Hello, world.' >foo.txt
The above code is my bash file.
Here have two command
create txt file
execute docker container
If I run this command
cd /home/arifur/workspace_python/erpdatabasebackup && bash backup_database.sh
in terminal then it is working
But when I run in crontab
* * * * * cd /home/arifur/workspace_python/erpdatabasebackup && bash backup_database.sh
then only txt file creation is working but docker container is not working.

The -ti requests to use a pseudo-tty and run in interactive mode but cron does not attach to any TTY. Try removing -ti as in
docker exec erpnext sh -c "cd /home/frappe/frappe-bench/ && /usr/local/bin/bench backup"

Related

How to run a bash terminal in a Docker container along with additional commands?

To run a bash terminal in a Docker container I can run the following:
$ docker exec -it <container> /bin/bash
However, I want to execute a command in the container automatically. For example, if I want to open a bash terminal in the container and create a file I would expect to run something like:
docker exec -it <container> /bin/bash -c "touch foo.txt"
However, this doesn't work... Is there a simple way to achieve this? Of course, I could type the command after opening the container, but I want to open a bash terminal and run a command at the same time.
You can run your touch command and then spawn another shell :
docker exec -it <container> /bin/bash -c "touch foo.txt; exec bash"
Works perfectly fine for me
~# docker run -tid --rm --name test ubuntu:20.04
~# docker exec -it test /bin/bash -c "touch /foo.txt"
~# docker exec -it test /bin/bash
root#b6b0efbb13be:/# ls -ltr foo.txt
-rw-r--r-- 1 root root 0 Mar 7 05:35 foo.txt
Easy solution:
docker exec -it <container> touch foo.txt
You can verify
docker exec -it <container> ls
This was tested with alpine image.
Remember that in docker images there is a entrypoint and a command. Now we are editing the command of the default entrypoint for alpine, via docker exec
It depends of the entrypoint if env variablers are load or not, $PATH ..., so other images maybe you need to write /bin/touch or /usr/bin/ls
Good luck!
When you run docker exec -it <container> /bin/bash -c "touch foo.txt", container sends 0 exit code so that it means the task is done and you'll be returned to your host.
When you run docker exec -it <container /bin/bash, bash shell is not terminated until you explicitly type exit or use CTRL+D in bash environment. bash is continuously running.
This is why when you run the second command, it goes to bash, runs your command (touches) and then exits.

Execute a "docker exec -it" command within in a ubuntu cron job

For a daily database backup, I created the following cron job :
File : crontab -e
SHELL=/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin
* * * * * /bin/bash /var/path/deploy/database/scripts/backup.sh
File : /var/path/deploy/database/scripts/backup.sh
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
cp -r /var/path/deploy/database/scripts /var/bricoolpathpostgresql/data
chmod 755 -R /var/path/postgresql/data
docker exec -it database /var/lib/postgresql/data/scripts/pg_backup_rotated.sh
When I execute the script directly it works well and the backup is created successfully. But the script is being excuted from the cron job the command docker exec -it database /var/lib/postgresql/data/scripts/pg_backup_rotated.sh does not seem to work.
I have no error output in /var/log/syslog
as Danny Ebbers mentioned in comment, the problem ws the -i argument in Docker command.

Bash shell script not executing command docker entrypoint

I have a shell script, it is not executing the "crontab" command when it is run from docker CMD. But works when I execute the file inside the docker container. What am I missing?
File entry.sh:
#!/bin/sh
echo "Starting cron"
/usr/sbin/cron -f -L 15
crontab /etc/cron.d/cron-python
Docker file:
FROM python:3
RUN apt-get update && apt-get -y install cron
COPY ./cron/entry.sh /entry.sh
COPY ./cron/crontab.txt /etc/cron.d/cron-python
RUN chmod +x /entry.sh
RUN chmod 0644 /etc/cron.d/cron-python
CMD ["./entry.sh"]
cron-python:
# crontab -e
*/1 * * * * python /app/myscript.py >> /var/log/script.log 2>&1
Following should work :
#!/bin/sh
# start cron
echo "Starting cron"
crontab /etc/cron.d/cron-python
/usr/sbin/cron -f -L 15
In your version, /etc/cron.d/cron-python does not get installed as you asked /usr/sbin/cron to run on foreground.
Also you need to copy /app/myscript.py.

Source script on interactive shell inside Docker container

I want to open a interactive shell which sources a script to use the bitbake environment on a repository that I bind mount:
docker run --rm -it \
--mount type=bind,source=$(MY_PATH),destination=/mnt/bb_repoistory \
my_image /bin/bash -c "cd /mnt/bb_repoistory/oe-core && source build/conf/set_bb_env.sh"
The problem is that the -it argument does not seem to have any effect, since the shell exits right after executing cd /mnt/bb_repoistory/oe-core && source build/conf/set_bb_env.sh
I also tried this:
docker run --rm -it \
--mount type=bind,source=$(MY_PATH),destination=/mnt/bb_repoistory \
my_image /bin/bash -c "cd /mnt/bb_repoistory/oe-core && source build/conf/set_bb_env.sh && bash"
Which spawns an interactive shell, but none of the macros defined in set_bb_env.sh
Would there be a way to provide a tty with the script properly sourcered ?
The -it flag is conflicting with the command to run in that you're telling docker to create the pseudo-terminal (ptty), and then running a command in that terminal (bash -c ...). When that command finishes, then the run is done.
What some people have done to work around this is to only have export variables in their sourced environment, and the last command would be exec bash. But if you need aliases or other items that aren't inherited like that, then your options are a bit more limited.
Instead of running the source in a parent shell, you could run it in the target shell. If you modified your .bash_profile to include the following line:
[ -n "$DOCKER_LOAD_EXTRA" -a -r "$DOCKER_LOAD_EXTRA" ] && source "$DOCKER_LOAD_EXTRA”
and then had your command be:
... /bin/bash -c "cd /mnt/bb_repository/oe-core && DOCKER_LOAD_EXTRA=build/conf/set_bb_env.sh exec bash"
that may work. This tells your .bash_profile to load this file when the env variable is already set, but not otherwise. (There can also be the -e flag on the docker command line, but I think that sets it globally for the entire container, which is probably not what you want.)

How to execute commands in docker container as part of bash shell script

I would like to write a bash script that automates the following:
Get inside running container
docker exec -it CONTAINER_NAME /bin/bash
Execute some commands:
cat /dev/null > /usr/local/tomcat/logs/app.log
exit
The problematic part is when docker exec is executed. The new shell is created, but the other commands are not executed.
Is there a way to solve it?
You can use heredoc with docker exec command:
docker exec -i CONTAINER_NAME bash <<'EOF'
cat /dev/null > /usr/local/tomcat/logs/app.log
exit
EOF
To use variables:
logname='/usr/local/tomcat/logs/app.log'
then use as:
docker exec -i CONTAINER_NAME bash <<EOF
cat /dev/null > "$logname"
exit
EOF
You can simply launch
docker exec -it container_id cat /dev/null > /usr/local/tomcat/logs/app.log

Resources