Execute script when docker container start - bash

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

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).

Run command from .bashrc in detached docker container

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?

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.

Run bash command before running container

I want to run a pre-existing Docker image like so:
docker run -d --name cdt-selenium selenium/standalone-firefox:3.4.0-chromium
So there is no Dockerfile that I control for this image. However, I would like to copy some files into this container.
If I did control the Dockerfile, I would like to run these commands:
RUN mkdir -p /root/cdt-tests/csv-data
COPY ./csv-data/* /root/cdt-tests/csv-data
Is there a way to run those commands in the same line as the Docker run command above?
I tried this:
docker run -d --name cdt-selenium selenium/standalone-firefox:3.4.0-chromium
docker exec cdt-selenium mkdir -p /root/cdt-tests/csv-data
docker cp cdt-selenium:/root/cdt-tests/csv-data ./csv-data
but I get a permissions error on the docker exec line
All images have a FROM line, and that can be any other image. So you can make a Dockerfile with:
FROM selenium/standalone-firefox:3.4.0-chromium
USER root
RUN mkdir -p /root/cdt-tests/csv-data
COPY ./csv-data/* /root/cdt-tests/csv-data
USER seluser
that will build your own image with your commands run.
You'd build it and create your own tag:
docker build -t alexander/selenium:3.4.0-chromium .
And then run it:
docker run -d --name cdt-selenium alexander/selenium:3.4.0-chromium
Edit: the exec command you ran failed because docker runs this container as a different user. You can see that in their Dockerfile. To solve that, run the exec with the root user option (-u root):
docker exec -u root cdt-selenium mkdir -p /root/cdt-tests/csv-data

Can't change directories in a docker container?

I am attempting to run a few commands inside of a docker container from it's host machine. Typically when I want to execute a shell command in a docker container from the host I will do something like this:
docker exec -ti myContainer ls -l
which works fine. But when I try to change directories like this:
docker exec -ti myContainer cd myDirectory
it throws the error:
rpc error: code = 2 desc = "oci runtime error: exec failed: exec:
\"cd\": executable file not found in $PATH"
Does anyone have any idea why this would be happening and how I can resolve it?
cd isn't an OS executable, is a shell command. To run it, you'll need to run a shell to parse it. docker exec -ti myContainer /bin/sh -c "cd myDirectory"

Resources