Docker Container is not running - docker

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

Related

Docker Container immediately exists even I execute it with -dit

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"]

Why aren't commands from the Dockerfile executed when the `/bin/bash` command is appended to `docker run -it IMAGE ID`?

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

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

Run copied file in container

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

docker container volumes from directory access in CMD instruction

docker container volumes from directory access in CMD instruction
$ sudo docker run -d --name ext -v /external busybox /bin/sh
and
run.sh
#!/bin/bash
if [[ -f "/external" ]]
then
echo 'success!'
else
echo 'Sorry, I can't find /external...'
fi
and
Dockerfile
FROM ubuntu:14.04
MAINTAINER newbie
ADD run.sh /run.sh
RUN chmod +x /run.sh
CMD ["bash", "/run.sh"]
and
$ sudo docker build -t app .
and
$ sudo docker run -d --volumes-from ext app
ac57afb95f923eeffd28e7d9d9cb76cb1b7699ebd
So
$ sudo docker logs ac57afb95f923eeffd28e7d9d9cb76cb1b7699ebd
Sorry, I can't find /external...
My question is,
How can I access /external directory in run.sh in CMD instruction
impossible?
Thank you~
modify your run.sh
-f is check for file exists. in this case use -d check for directory exists.
Check if a directory exists in a shell script
futhermore if you want make only volume container, need not add -d, /bin/sh
volume container run command change like this
$ sudo docker run --name ext -v /external busybox

Resources