Change selected directory at startup of docker container - docker

Is it possible to save the selected directory in docker before exiting the container?
As a default, docker does not remember the selected directory before it exits.
In the example below, I changed the directory inside docker to home.
Example:
> docker exec -it loving_mccarthy /bin/bash
root#6bd70522dd17:/# cd /home
root#6bd70522dd17:/home# exit
exit
> docker exec -it loving_mccarthy /bin/bash
/#

You can achieve this behavior by setting up a bash function, which you can configure to run on terminal exit, that writes the current working directory to the .bashrc file, which you can then use the second time you run a bash terminal in the container with cd -.
Here's a full example:
Start the container:
$ docker run -it --name=example -d ubuntu /bin/bash
Run docker exec the first time, and configure the function for trap:
$ docker exec -it example /bin/bash
root#ecfc612fe6b8:/# set_workdir_on_exit() { echo "export OLDPWD=$PWD" >> $HOME/.bashrc ;}
root#ecfc612fe6b8:/# trap 'set_workdir_on_exit' EXIT
root#ecfc612fe6b8:/# cd /home
root#ecfc612fe6b8:/home# exit
Run docker exec the second time:
$ docker exec -it example /bin/bash
root#6a491ad1aee5:/# cd -
/home
root#6a491ad1aee5:/home#

Related

Trying to copy a script into a detached Docker container, and execute it with docker exec

Right now I am setting my Docker instance running with:
sudo docker run --name docker_verify --rm \
-t -d daoplays/rust_v1.63
so that it runs in detached mode in the background. I then copy a script to that instance:
sudo docker cp verify_run_script.sh docker_verify:/.
and I want to be able to execute that script with what I expected to be:
sudo docker exec -d docker_verify bash \
-c "./verify_run_script.sh"
However, this doesn't seem to do anything. If from another terminal I run
sudo docker container logs -f docker_verify
nothing is shown. If I attach myself to the Docker instance then I can run the script myself but that sort of defeats the point of running in detached mode.
I assume I am just not passing the right arguments here, but I am really not clear what I should be doing!
When you run a command in a container you need to also allocate a pseudo-TTY if you want to see the results.
Your command should be:
sudo docker exec -t docker_verify bash \
-c "./verify_run_script.sh"
(note the -t flag)
Steps to reproduce it:
# create a dummy script
cat > script.sh <<EOF
echo This is running!
EOF
# run a container to work with
docker run --rm --name docker_verify -d alpine:latest sleep 3000
# copy the script
docker cp script.sh docker_verify:/
# run the script
docker exec -t docker_verify sh -c "chmod a+x /script.sh && /script.sh"
# clean up
docker container rm -f docker_verify
You should see This is running! in the output.

How to run a .out file and store the output in a .txt file inside a docker Container?

I have built a docker Container named ubuntu-comp and run it with a volume by this command:
sudo docker run -v /home/vivek/PersonalSet/OJBh/Judge/folderrun/:/home/folderrun/ -d ubuntu-comp
This command runs successfully. Now I want to execute a command to run a file called c.out and store the output (STDOUT) in a text file called c.txt. Both files are stored in the volume directory.
Normally I'd use the command /home/folderrun/c.out > /home/folderrun/c.txt. This works fine if I run the Command in interactive mode, i.e. with the -it flag.
but when i run the following command:
sudo docker exec -it focused_wilbur /home/folderrun/c.out > /home/folderrun/c.txt
I get the following error:
bash: /home/folderrun/c.txt: No such file or directory
You can do it by passing your command to /bin/sh like this
sudo docker exec -it focused_wilbur /bin/sh -c "/home/folderrun/c.out > /home/folderrun/c.txt"

Bash on alpine linux

I cannot get a bash shell into an alpine container.
I'm starting with this Alpine container:
gitlab/gitlab-runner:alpine
I'm adding a bash shell and other configs in this dockerfile:
from gitlab/gitlab-runner:alpine
ENV http_proxy=<corporate_proxy>
ENV https_proxy=<corporate_proxy>
RUN apk add vim wget curl nmap lsof bash bash-completion which
CMD ["/bin/bash"]
RUN ls -l /bin # THIS WORKS, I CAN SEE 'BASH' SHOW UP WITH 755 OWNED BY ROOT
RUN which bash # THIS ALSO WORKS
RUN /bin/bash -c "echo hi" # YES, THIS WORKS TOO
However when spawning the container to use a bash shell via:
docker run -idt <image_name> /bin/bash, the container fails to start with FATAL: Command /bin/bash not found.
Note that these other options also fail for me when spawning a container: ash, sh, /bin/ash, /bin/sh, etc
running the container with --user root also does not work.
The entrypoint is a GitLab Runner script. Change it to bash to get shell access:
$ docker run -it --entrypoint /bin/bash <image_name>
It turns out something funky was being set in the container's entrypoint. I need to remember to override the entrypoint when spawning a new container via docker run.
Adding this line in the Dockerfile fixed the problem:
ENTRYPOINT: []
1- verify if you container in fully loaded by :
docker ps
so after to enter in bash shell like:
docker exec -it <<container_name>> bash
Alpine doesn't have bash, use sh instead:
docker exec -it 64103333b32 /bin/sh

How to take Oracle-xe-11g backup from running Docker container

I am running oracle-xe-11g on rancher os. I want to take the data backup of my DB. When I tried with the command
docker exec -it $Container_Name /bin/bash
then I entered:
exp userid=username/password file=test.dmp
It is working fine, and it created the test.dump file.
But I want to run the command with the docker exec command itself. When I tried this command:
docker exec $Container_Name sh -C exp userid=username/password file=test.dmp
I am getting this error message: sh: 0: Can't open exp.
The problem is:
When running bash with -c switch it is not running as interactive or a login shell so bash won't read the same startup scripts. Anything set in /etc/profile, ~/.bash_profile, ~/.bash_login, or ~/.profile would be skipped.
Workaround:
run your container with following command:
sudo docker run -d --name Oracle-DB -p 49160:22 -p 1521:1521 -e ORACLE_ALLOW_REMOTE=true -e ORACLE_HOME=/u01/app/oracle/product/11.2.0/xe -e PATH=/u01/app/oracle/product/11.2.0/xe/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin -e ORACLE_SID=XE -e SHLVL=1 wnameless/oracle-xe-11g
What I'm doing is specifying the environment variables set in the container using docker.
Now for generating the backup file:
sudo docker exec -it e0e6a0d3e6a9 /bin/bash -c "exp userid=system/oracle file=/test.dmp"
Please note the file will be created inside the container, so you need to copy it to docker host via docker cp command
This is how I did it. Mount a volume to the container e.g. /share/backups/ then execute:
docker exec -it oracle /bin/bash -c "ORACLE_HOME=/u01/app/oracle/product/11.2.0/xe ORACLE_SID=XE /u01/app/oracle/product/11.2.0/xe/bin/exp userid=<username>/<password> owner=<owner> file=/share/backups/$(date +"%Y%m%d")_backup.dmp"

Running a script in docker container and not killing the script when leaving terminal

I have got some docker container for instance my_container
I Want to run a long living script in my container, but not killing it while leaving the shell
I would like to do something like that
docker exec -ti my_container /bin/bash
And then
screen -S myScreen
Then
Executing my script in screen and exit the terminal
Unfortunately, I cannot execute screen in docker terminal
this maybe help you.
docker exec -i -t c2ab7ae71ab8 sh -c "exec >/dev/tty 2>/dev/tty </dev/tty && /usr/bin/screen -r nmsrv -s /bin/bash"
and this is the reference link
Only way I can think of is to run your container with your script at the start;
docker run -d --name my_container nginx /etc/init.d/myscript
If you have to run the script directly in an already running container, you can do that with exec:
docker exec my_container /path/to/some_script.sh
or if you wanna run it through Php:
docker exec my_container php /path/to/some_script.php
That said, you typically don't want to run scripts in already running containers, but to just use the same image as some already running container. You can do that with a standard docker run:
docker run -a stdout --rm some_repo/some_image:some_tag php /path/to/some_script.php

Resources