I would like copy file from host to container then execute it. Like this:
docker run --name ubuntu_trusty ubuntu:14.04
docker cp ./script.sh ubuntu_trusty:/script.sh
# run ubuntu_trusty container then execute /bin/bash /script.sh
I can see the ubuntu_trusty container
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
20e54389340e ubuntu:14.04 "/bin/bash" 9 minutes ago Exited (0) 1 seconds ago ubuntu_trusty
But i don't know how to "re-run" my "/bin/bash /script.sh" in it. How to do it ?
You can use docker exec like this:
interactive
$ docker exec -it ubuntu_trusty bash
>>> /script.sh
>>> exit
non-interactive
$ docker exec ubuntu_trusty bash /script.sh
To complete kev answer as fully working answer, container must be running when use docker exec, so we must run container and keep it alive with -d:
docker run -i -d --name ubuntu_trusty ubuntu:14.04 /bin/bash
Then we can cp and exec
docker cp script.sh ubuntu_trusty:/script.sh
docker exec ubuntu_trusty /bin/bash /script.sh
Hello world
Related
Step 1:
docker ps -a
container Id: dd5cf6b519b4
I need to inspect inside the stopped docker container which is cannot start.
I tried with docker exec -it container-id bin/bash But this is for running container.
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS NAMES
0dfd54557799 ubuntu "/bin/bash" 25 seconds ago Exited (1) 4 seconds ago peaceful_feynman
Commit the stopped image
$ docker commit 0dfd54557799 debug/ubuntu
now we have a new image
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
debug/ubuntu <none> cc9db32dcc2d 2 seconds ago 64.3MB
create a new container from the "broken" image
$ docker run -it --rm --entrypoint sh debug/ubuntu
inside of the container we can inspect - for example, the file system
$ ls /app
App.dll
App.pdb
App.deps.json
You can start container with specific entrypoint
docker run --entrypoint sleep YOUR_IMAGE 3600
It will block current terminal for 3600 seconds. You can open new terminal tab(do not close current one) and you can verify if your container is working with the
docker ps
If you do not want to block current terminal, you can add -d flag to docker run:
docker run -d --entrypoint sleep YOUR_IMAGE 3600
Above command will start docker which will be doing nothing, then you can ssh into the container when it is working with
docker exec -ti CONTAINER HASH sh
My docker container immediately exists after I run it with command -dit .. I do not understand why it happens. I am running my container with sh command in the Jenkins file which is executed in the Jenkins. You can find out the actual code below. I see that unable to find the container after I execute with the command "docker exec" as you will see below. Even I list the containers with docker ps -a command I can see the docker container exited in 1 second. Do you think I made a mistake while executing docker run -d -I -t command ?
def containerName = myContainer
def imageName= buildedImage
sh "docker run -d -i -t --name $containerName $imageName"
sh "docker ps -a"
sh "docker exec -it $containerName /bin/bash \"/target/scripts/dockertest.sh\""
My Dockerfile is :
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
ADD ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Xmx128m","-Xss256k","-XX:+UseG1GC","- Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
I am just starting out with Docker. I have this Dockerfile:
FROM jonathonf/manjaro
CMD ["pacman", "-S", "--noconfirm", "git"]
When I build the image with
sudo docker build -t uname/description:tag .
and then run it with
sudo docker run IMAGE_ID
, where IMAGE_ID is the ID I get from sudo docker images command, the command in the Dockerfile CMD ["pacman", "-S", "--noconfirm", "git"] runs, git is installed, a container is created (that I can commit).
If I run the image with
sudo docker run IMAGE_ID /bin/bash
the CMD from the Dockerfile is not executed.
I expected it to run the commands from the Dockerfile, make git available in the container and let me work further in the shell.
Couple of things here:
If you want git installed always, why run it as a CMD and then manually commit as a new image, rather than just running in a Dockerfile RUN instruction?
Anything you put after docker run... is going to run as the CMD and override it. If you don't want to override it, you should put it as an ENTRYPOINT instead. But really you should do 1.
When you use CMD ["pacman", "-S", "--noconfirm", "git"] in your dockerfile, you are setting pacman -S --noconfirm git as pid-1 process of your container.
Now when you run container sudo docker run IMAGE_ID the first process will be the one specified in the CMD. You can verify this by running docker exec -it container-id ps -ef
When you run sudo docker run IMAGE_ID /bin/bash the pid-1 process of your docker container is replaced by /bin/bash.
[user#jumphost ~]$ docker run -itd -p 3666:3306 alpine sh
dcef6d1cc121bfd195552fa7639038ac513a74eaa035a855bb7917dd620be642
[user#jumphost ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dcef6d1cc121 alpine "sh" 2 seconds ago Up 2 seconds 0.0.0.0:3666->3306/tcp fervent_euclid
[user#jumphost ~]$ docker exec -it dcef6d1cc121 ps -ef
PID USER TIME COMMAND
1 root 0:00 sh
7 root 0:00 ps -ef
[user#jumphost ~]$
Also to know more about CMD check this out.
Also check docker entrypoint and its difference with CMD.
Hope this helps.
Thats how Docker works. You override the CMD from your Dockerfile. If you want to achive both, a git Install and your bash command, move your CMD command to RUN command. Please consider Dockerfile documentation https://docs.docker.com/engine/reference/builder/#run
Please help. When I want to go into a container is says
Error response from daemon: Container 90599013c666d332ff6560ccde5053d9127e72042ecc3887550aef90fa1d1eac is not running
My DockerFile:
FROM ubuntu:16.04
MAINTAINER Anton Lapitski <a.lapitski#godeltech.com>
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
ADD ./ /usr/src/app
EXPOSE 80
ENTRYPOINT ["/bin/sh", "-c", "/usr/src/app/entry.sh"]
Starting script - start.sh:
sudo docker build -t starter .
sudo docker run -t -v mounted-directory:/usr/src/app/mounted-directory -p 80:80 starter
entry.sh script:
echo "Hello World"
ls -l
pwd
if mountpoint -q /mounted-directory
then
echo "mounted"
else
echo "not mounted"
fi
sudo docker ps -a gives:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
90599013c666 starter "/bin/sh -c /usr/src…" 18 minutes ago Exited (0) 18 minutes ago thirsty_wiles
And mosе important:
sudo docker exec -it 90599013c666 bash
Error response from daemon: Container 90599013c666d332ff6560ccde5053d9127e72042ecc3887550aef90fa1d1eac is not running
Please could you tell what I am doing wrong?
P.S adding -d flag when running not helped.
Once the ENTRYPOINT completes (in any form), the container exits.
Once the container exits, you can't docker exec into it.
If you want to get a shell on the image you just built to poke around in it, you can
sudo docker run --rm -it --entrypoint /bin/sh starter
To make this slightly easier to run, you might change ENTRYPOINT to CMD in your Dockerfile. (Docker will run the ENTRYPOINT passing the CMD as command-line arguments; or if there is no entrypoint just run the CMD.)
...
RUN chmod +x ./app.sh
CMD ["./app.sh"]
Having done that, you can more easily override the command
sudo docker run --rm -it starter /bin/sh
You can try
docker start container_id and then docker exec -ti container_id bash for a stopped container.
You cannot execute the container, because your ENTRYPOINT script has been finished, and the container stopped. Try this:
Remove the ENTRYPOINT from your Dockerfile
Rebuild the image
run it with sudo docker run -it -v mounted-directory:/usr/src/app/mounted-directory -p 80:80 starter sh
The key is the i flag and the sh at the end of the command.
I tried these two commands and it works:
sudo docker start <container_id>
docker exec -it <containerName> /bin/bash
I'm trying to run my docker image and overwrite the default command node ./bin/name-of-program with:
docker run name/hub:4.5.0 /bin/bash
But isn't working. The default command is being executed and finishing the container:
2e9d10532e4c name/hub:4.5.0 "node ./bin/name-of-program /bin/ba" About a minute ago Exited (1) About a minute ago insane_brown
Is it possible to access a container? I have a code inside that i losted.
If the container defines an ENTRYPOINT command, that runs before the CMD. Try:
docker run -it --entrypoint /bin/bash name/hub:4.5.0
The -it (aka -i -t) is necessary to get an interactive terminal; without that bash won't run.