This question already has answers here:
How to run shell script on host from docker container?
(15 answers)
Closed 12 days ago.
I have a script run_test.sh. I need to run a script not in a container. But how can i run it if the script file is in the container? Also when i call this script it starts this container.
I have windows and WSL ubuntu 22.04
If I understand you correctly, you have a Docker image that contains a script that you want to run directly on the host.
To do that, let's create a Dockerfile that creates an image with a script called script.sh like this
FROM debian
WORKDIR /app
RUN echo '#!/bin/bash' > script.sh && \
echo 'echo ''Hello''' >> script.sh
You can the run the image, cat the script to stdout and pipe it into bash, like this
docker build -t test .
docker run --rm test cat script.sh | bash -
That will run the script on the host and print 'Hello'.
Related
I have a shell script like create.sh inside a docker container.
I connected to container like below.
docker exec -it --user=root <container_id> /bin/sh
and when i try to run the script inside the container i am getting an error .
sh-4.2# sh create.sh
create.sh: line 1: !/bin/bash: No such file or directory
Any ideas?
You must have put #!/bin/bash at the beginning of your .sh script. So it searches for /bin/bash to run this script, but cannot find it because bash is not installed.
I have a Dockerfile in which I have specified an ENTRYPOINT "my_script.sh".In my_script.sh,I am exe.cuting a CURL command.When the docker image with this Dockerfile is built.How should I run it so that output of my_script.sh will be printed on my host.
Dockerfile -
FROM my-company-repo-java-base-image
ADD my_script.sh /root
ENTRYPOINT bash "/root/my_script.sh
my_script.sh
echo "Hello My Script"
curl -x POST "some_api_which_returns_json"
I have built the image using command
docker build
I want to run this image and see output of my_script.sh on my dockerhost.
Given a Docker image whose tag is $DOCKER_IMAGE:
docker container run -it --rm $DOCKER_IMAGE
-i keeps STDIN open
-t allocates a pseudo-TTY
--rm automatically removes the container when it exits
See docker container run for all the options.
Of course you can see the output of shell script. Make sure you delete the old image before building new one when you change the script. Else, your container will keep using the old script over and over. Here's an example
Dockerfile
FROM alpine:3.7
ENTRYPOINT ["/usr/bin/myscript.sh"]
COPY myscript.sh /usr/bin/myscript.sh
myscript.sh
#!/usr/bin/env sh
echo "Hello there"
commands to run:
docker image rm testdocker
docker build --tag testdocker .
docker run testdocker
You should see the line Hello there appears on the terminal
I want to run an interactive script inside the docker container. I have created the image which contains the interactive script. I want to run that script when I am launching the image inside the docker container. But its not running.
Can Someone please help me or share a link for that. My need is when I launch the container that script should hit and from the container it should communicate and ask for the input.
Here is a working solution:
First the script which prompts the user: input.sh
#!/bin/sh
echo who are you?
read someone
echo hello $someone
The Dockerfile:
FROM bash:4
COPY ./input.sh input.sh
RUN chmod +x input.sh
CMD ./input.sh
Just build it
docker build -t testinput .
And run it:
docker run -i testinput:latest
The output:
$ docker run -i testinput:latest
who are you?
bob
hello bob
The important part is the -i option that runs the container interactively
My script is as follows:
# start a ubuntu container in the background
docker run -it --name ub -d ubuntu /bin/bash
sleep 1
# run a command in the container
docker exec -it ub bash
echo 234
# exit the container
exit
sleep 1
# do something else
echo 123
But the script would just stop right after exit and hang there. Does anyone know why is that?
p.s: My Docker version is: 17.03.0-ce, build 60ccb22
You have given -it during the run command. which opens up the /bin/bash of your container and waits there. The next command wont get executed until the first command execution is completed.
It's better to create a script file and move it inside the container while making the docker. and run the script on starting the docker. You may specify that using a CMD in the docker file.
You won't be needing an additional exec command.
The corresponding Dockerfile would be
FROM ubuntu:latest
COPY <path-to-script> <dest>
CMD [" <path-to-script> "]
You have to create the script file along with the Dockerfile. Build the docker using the command
docker build -t <image-name> <location of Dockerfile>
The execution command would be
docker run -d --name <name> -d ubuntu <path-to-script>
I have created my simple own image from .
FROM python:2.7.11
RUN mkdir /extra/later/ \
&& mkdir /yyy
Now I'm able to perform the following steps:
docker run -d -v xxx:/yyy myimage:latest
So now my volume is mounted inside the container. I'm going to access and I'm able to perform commands on that mounted volume inside my container:
docker exec -it container_id bash
bash# tar -cvpzf /mybackup.tar -C /yyy/ .
Is there a way to automate this steps in your Dockerfile or describing the commands in your docker run command?
The commands executed in the Dockerfile build the image, and the volume is attached to a running container, so you will not be able to run your commands inside of the Dockerfile itself and affect the volume.
Instead, you should create a startup script that is the command run by your container (via CMD or ENTRYPOINT in your Dockerfile). Place the logic inside of your startup script to detect that it needs to initialize the volume, and it will run when the container is launched. If you run the script with CMD you will be able to override running that script with any command you pass to docker run which may or may not be a good thing depending on your situation.
Try using the CMD option in the Dockerfile to run the tar command
CMD tar -cvpzf /mybackup.tar -C /yyy/ .
or
CMD ["tar", "-cvpzf", "/mybackup.tar", "-C", "/yyy/", "."]