Run command from .bashrc in detached docker container - bash

I'm running a docker container in detached mode, and I'm having difficulty running a command that's in the container's ~/.bashrc.
Inside the docker container's ~/.bashrc:
labserve () {
jupyter lab --ip=0.0.0.0 --allow-root --no-browser --NotebookApp.token=''
}
If I start the container with
$ docker run -d image -p 8888:8888 --name jupyterbox
I'd like to be able to tell the image to tell the detached container to open up jupyterlab with the following command:
$ docker exec jupyterbox bash -c labserve
however, when I do that, I get the error
bash: labinit: command not found
On the other hand, if I do
$ docker run -it jupyterbox bash
root#4e344655fc31:/home# labserve
The command runs correctly, and opens up a jupyterlab within my docker container running on port 8888.
How do I exec this command in a detached container?

Related

OCI runtime exec failed: exec failed: container_linux.go:349: starting container process caused "exec:

I have created image with our application, after running the image i can see the docker containers are also created, when I am trying to getinto the docker container i am getting the below error, can you please help me here.
""""OCI runtime exec failed: exec failed: container_linux.go:349: starting container process caused "exec: "bash": executable file not found in $PATH": unknown""""
I have tried to execute all the below commands
docker run -it exec 962f5d99458c
docker run -it 962f5d99458c
docker exec -it 962f5d99458c /bin
docker exec -it 75f6cb44f9e1
docker run --rm -ti 962f5d99458c sh
docker run --rm -ti 962f5d99458c /bin
docker exec --rm -ti 962f5d99458c /bin
docker exec -ti 962f5d99458c /bin
docker exec -ti 962f5d99458c \bin
docker exec -it 75f6cb44f9e1 bash
but no luck..... can you please help me here.
What the error says is that the startup command is invalid because the image has no (or it's not on $PATH) certain executable in it (bash in this case). The absence of bash is adequate for certain images (e.g. based on Alpine Linux or scratch) but if there is any shell at all, you can use sh:
# create a container from image and get straight into shell
docker run -it <image> sh
# or start a container in background, then get into shell
docker run -d <image>
docker exec -it <container> sh
If the image has no shell in it, then the only way to launch it, is by using the binary of the application the image supposed to run. Normally it works out of the box, unless you've overridden ENTRYPOINT and/or CMD in Dockerfile or by run arguments. The default startup arguments can be found with docker inspect:
docker image inspect nginx | jq '.[0].Config.Entrypoint'
docker image inspect nginx | jq '.[0].Config.Cmd'
In case you absolutely sure that the executable is there but still get the error, using an absolute path might help (e.g. /bin/bash instead of simply bash).

How to get an interactive bash shell in a Docker container

I'm trying to connect to a running container or start a new container in interactive mode with the bash shell -- not the sh shell. I want to run:
docker exec -it <container_name> /bin/bash
or
docker run -it <container_name> <image_name>
or
docker run -it <container_name> <image_name> /bin/bash
and get an interactive bash shell.
What I've tried so far:
Per this post I've tried
Adding this to my Dockerfile
SHELL ["/bin/bash", "-c"]
Adding this to my Dockerfile
RUN ["/bin/bash", "-c", "echo I am now using bash!"]
But every time I try to run a container in interactive mode (docker run -it or attach to a running container (docker exec -it), I land in the sh shell.
How can I get an interactive bash shell that is running inside a docker container?
Update: Minimal working Dockerfile
FROM ubuntu
SHELL ["/bin/bash", "-c"]
You are in fact running an interactive bash with commands like:
docker container run -it ubuntu /bin/bash
When you run bash in a docker container, that shell is in a container. So it won't have the command history from outside of the container, that history is maintained on the host filesystem. It also won't have your prompt, the PS1 variable is not automatically exported into the container's environment. And it won't have your .bashrc configuration from your host, since that isn't inside the container. Instead you get a bash shell that is out of the box from a minimal ubuntu host.

Execute script when docker container start

I want container with "centos:latest" image to be started and should execute my script. The scripts are copied with docker cp commands.
docker create --name centos1 centos:latest
docker cp . 5db38b908880:/opt ---> scripts are in current directory, hence .
docker commit centos1 new_centos1 --> now new_centos1 image has scripts
Now I want to start new container with the scripts to be executed: I tried below commands:
docker run -ti --rm --entrypoint "cd /opt && deploy_mediainfo_lambda.sh" new_centos1:latest
docker run -ti --rm new_centos1:latest "cd /opt && deploy_mediainfo_lambda.sh"
Both of above commands failed with:
docker: Error response from daemon: OCI runtime create failed: container_linux.go:296: starting container process caused "exec: \"cd /opt && deploy_mediainfo_lambda.sh\": stat cd /opt && deploy_mediainfo_lambda.sh: no such file or directory": unknown.
ERRO[0000] error waiting for container: context canceled
if used bash command while starting container, I can run my script using 'execuateble path'/'execuatble name' inside container, but I can not do this while starting container on commandline
docker run -ti --rm new_centos1:latest bash
[root#c34207f3f1c4 /]# ./opt/deploy_mediainfo_lambda.sh
If used below command, which calls executable directly, it gives path error.
docker run -ti --rm new_centos1:latest "deploy_mediainfo_lambda.sh"
docker: Error response from daemon: OCI runtime create failed: container_linux.go:296: starting container process caused "exec: \"deploy_mediainfo_lambda.sh\": executable file not found in $PATH": unknown.
ERRO[0000] error waiting for container: context canceled
Also not sure about setting $PATH from commandline while starting the container.
I know, using Dockerfile this is achievable, like:
can set path using ENV,
can copy executables with ADD or COPY
run executables using CMD or ENTRYPOINT
How to achieves it using docker commandline?
Thanks melpomene.
Here is my bash script to automate script execution inside container, after copying them, all using docker commands.
# Start docker container
docker create --name mediainfo_docker centos:latest
# copy script files
docker cp . mediainfo_docker:/opt
# save container with the new image, which contains all scripts.
docker commit mediainfo_docker mediainfo_docker_with_scripts
# Now run scripts inside docker container
docker run -ti --rm mediainfo_docker_with_scripts:latest /opt/deploy_mediainfo_lambda.sh
Since deploy_mediainfo_lambda.sh is a script, first line of it is:
#!/bin/bash

Running shell script using Docker image

Input:
- There is Windows machine with Docker Toolbox installed.
- There is a shell script file baz.sh which calls py2dsc-deb.
Problem: py2dsc-deb is not available on Windows.
As I understand correctly, I can pull some Linux distro image from Docker repository, create a container and then execute shell-script file and it will run py2dsc-deb and do its job.
I have pulled:
debian - stretch-slim - 3ad21 - 3 weeks ago - 55.3MB
Now
How do I run my script using debian, something like: docker exec mycontainer /path/to/test.sh?
Running docker --rm debian:stretch-slim does nothing. Doesn't it suppose to run Debian distro at docker-machine ip?
I have tried to keep the container up using docker run -it debian:stretch-slim /bin/bash, then run the script using docker exec 1ef5b ./build.sh, but getting
$ docker exec 745 ./build.sh
rpc error: code = 2 desc = oci runtime error: exec failed: container_linux.go:247: starting container process caused "exec: \"./build.sh\": stat ./build.sh: no such file or directory"
Does it mean I can't run external script and has to always pass it inside the Docker?
You can execute bash command inside your container by typing
docker exec -ti -u `username` `container_name` bash -c "cd /path/to/ && ./test.sh"
lets say your container name is test_buildbox, you are root and your script stays inside /bin/test.sh You can call this script by typing
docker exec -ti -u root test_buildbox bash -c "cd /bin/ && ./test.sh
Please check if you have correct line endings in your .sh scripts (<LF>) when you built Docker image on Windows.

How can I run docker container without entering into container

I have Dockefile
FROM centos:7
So I have no entrypoint in dockerfile.
Then I build it to image
sudo docker build -t my_container .
Then I start it.
sudo docker run -t my_container
And I get open tty to container
root#my_container_id/
If I start it without -t it stopped immidiately after start.
How can I run docker container without start tty and without entrypoint?
You can start your container in a detached mode:
docker run -it -d my_container
The -d option here means your container will run in "detached" mode, in the background.
If you want to attach the container and drop to a shell, you can use:
docker exec -it my_container /bin/bash
Note, if your container is based on an alpine image, you need to use sh, i.e.:
docker exec -it my_container /bin/sh
You can't do that. Your container lives if its main process is running, so you have to have a main process which is the process with PID 1 inside your container, and your container will be up if that process is running.

Resources