See if a Docker Container Exists With a Specific Name - docker

I am trying to find if a Docker Container with a specific name exists, but I seem to be unsuccessful. I have found multiple answers from other posts, the most notable one being this one, but I cannot seem to reproduce their success. Their answer returns nothing when it is tried with the container in multiple states (sometimes stopped, sometimes running), but I can't seem to get an output. It is not supposed to do anything if the container is running for right now. The block of code that I've been using is:
if [ ! "$(docker ps -q -f name=mysql80)" ]; then
if [ "$(docker ps -aq -f status=exited -f name=mysql80)" ]; then
# cleanup
docker rm mysql80
fi
# run your container
docker run -d -p 3306:3306 -p 33060:33060 --volume "$MYSQLFOLDERLOCATION":/var/lib/mysql --name mysql80 frostedflakez/php-mysql-webserver:0.9-beta.3-mysql-latest-8.0
fi
I expect this block of code to find the container, and remove it if it is exited. I know this can sometimes be hazardous. I am running macOS 10.15.6 Catalina (latest update), with Docker Desktop 2.3.0.4 (46911).
% docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1300b4021189 frostedflakez/php-mysql-webserver:0.9-beta.3-mysql-latest-8.0 "docker-entrypoint.s…" 6 seconds ago Up 5 seconds 0.0.0.0:3306->3306/tcp, 0.0.0.0:33060->33060/tcp mysql80
4e83c877642b frostedflakez/php-mysql-webserver:0.9-beta.3-php-latest-7.4 "docker-php-entrypoi…" 8 seconds ago Up 6 seconds 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp php74
^^^ This is my docker ps output

Your script works for me. First run:
> t
Unable to find image 'frostedflakez/php-mysql-webserver:0.9-beta.3-mysql-latest-8.0' locally
0.9-beta.3-mysql-latest-8.0: Pulling from frostedflakez/php-mysql-webserver
...
5e500ef7181b: Pull complete
af7528e958b6: Pull complete
Digest: sha256:ef35b9e2b11b0b42a40205eba89a2b8320ace58a3c073b43e1770a52a044484b
Status: Downloaded newer image for frostedflakez/php-mysql-webserver:0.9-beta.3-mysql-latest-8.0
8b01dd743c554a8fb79d73bccf683097e531e8eeea3af1062e0414b35401a000
>
Second run:
> t
>
and if I stop the container, and then run the script:
> t
deleting mysql80
mysql80
35ca48875efdc60032b00325dd0563da64a834df0074580051c25e0601573180
>
(I added a echo "deleting mysql80" just to help to know what was going on)
If I stop and delete the container, and run the script, I get:
> t
971309690a97d0b2ffb730feb9f19ab03db10df730d19c91c3482a952a9ebb18
>
I'm running Docker version 19.03.12, build 48a66213fe on a Mac.

Related

Docker container ps or ls doesn't show running Containers

Here is an example on my CLI:
$ docker pull hello-world
$ docker run hello-world
It shows empty when ls/ps
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
$ docker container ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
It shows up only when I use -a but it'd suggest the containers are actually not actively running.
$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
96c3e42ae83a hello-world "/hello" 11 seconds ago Exited (0) 8 seconds ago jovial_rosalind
dcaed0ba308f registry "/entrypoint.sh /etc…" 42 minutes ago Created 0.0.0.0:5000->5000/tcp registry
Have I missed something?
Looks like your container exited right away. Is it meant to be interactive? (like running bash, or needing any user interaction?) if it is, you should run it like this to attach a terminal to it:
docker run -ti hello-world
If not, what does your hello program do? If it is not something that will keep running, then the container will stop whenever it exits.
Also keep in mind that, unless you pass docker run the -d/--detach flag, it will only return after the container has stopped - so if it returns right away, that means your container has already stopped.
You may want to use one of these to get a bash shell in the container to debug your problem:
docker run -ti hello-world bash
docker run --entrypoint bash -ti hello-world
To understand the difference between them, you can read the documentation on ENTRYPOINT and COMMAND.

Docker run does not display any output

I installed docker on a raspberry-pi (Connected via ssh)
Installation is successful.
But running docker run hello-world produce no output.
Note on very first time I got additional messages regard installing image
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
ad0f38092cf2: Pull complete
Digest: sha256:e366bc07db5e8a50dbabadd94c2a95d212bc103e3557e47df8a2eebd8bb46309
Status: Downloaded newer image for hello-world:latest
But there is no actual output from hello world script
Note I installed docker using command curl -sSL https://get.docker.com | sh
I tried following command too
sudo usermod -aG docker pi
sudo systemctl start docker
sudo docker run hello-world
Tried following commands docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
734dd8f733d7 hello-world "/hello" About a minute ago Exited (139) 59 seconds ago thirsty_bhaskara
I ran into the same issue on a Raspberry Pi 1B+ (armv6l). Inspired by #JanDrábek's answer, the first observation is that the hello-world image would indeed be one supporting ARM, yet only after using hypriot/armhf-hello-world instead did I get the expected output:
$ uname -a
Linux 4.1.19+ #858 Tue Mar 15 15:52:03 GMT 2016 armv6l GNU/Linux
$ docker run hello-world # No output
$ docker image inspect hello-world | grep Architecture # Arch looks right though
"Architecture": "arm",
$ docker run hypriot/armhf-hello-world # This does the job
Hello from Docker.
This message shows that your installation appears to be working correctly.
run:
docker ps -a
and check if you can see the exited container.
take the container ID from the output and type
docker logs <ID>
this will allow you to see the logs.
if you want to see the output in the first place when you run it add -it flags to the run command
edit:
I tried in on my machine:
docker run -it hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
d1725b59e92d: Pull complete
Digest: sha256:e366bc07db5e8a50dbabadd94c2a95d212bc103e3557e47df8a2eebd8bb46309
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
maybe your output is redirected to some other stream.
try using :
docker run -it hello-world > ./test.txt 2>&1
after that check if the file has any content
I was having similar issue, my solution was definitely very naive but I basically removed all container and images and then tried again. It worked.
# Delete all containers
docker rm $(docker ps -a -q)
# Delete all images
docker rmi $(docker images -q)
I recently had same problem on my freshly installed Fedora 28 (up-to-date)... the containers all exited with exit code 139, the docker events said that it died, and docker logs said nothing.
My solution was to update the docker (or switch to CE edition) as the installed docker version was 1.13 which is quite old. (The tutorial for fedora https://docs.docker.com/install/linux/docker-ce/fedora/)
Also I have came across one potential thing to check... is your container compatible with your architecture (raspberry is ARM isn't it?) Use docker image inspect <image> search for Architecture.

Create new image based on standard one

I have installed Docker and have running some Ubuntu image with command:
sudo docker run ubuntu
I would like to create some text file on it and find it next time the same image will run. How to achieve that?
UPD.
Got problems with attaching to docker.
I have running docker
docker ps -a
aef01293fdc9 ubuntu "/bin/bash" 6 hours ago Up 6 hours priceless_ramanujan
Since it is Up mode, I suppose I don't need to execute command:
docker start priceless_ramanujan
So, I run command attach
docker attach priceless_ramanujan
And got nothing in output while command not returns.
Why I can't get to container's bash?
Simple example:
$ docker run -it ubuntu
root#4d5643e8c1a8:/# echo "test" > test.txt
root#4d5643e8c1a8:/# cat test.txt
test
root#4d5643e8c1a8:/# exit
exit
$ docker run -it ubuntu
root#cdb44750bffc:/# cat test.txt
cat: test.txt: No such file or directory
root#cdb44750bffc:/#
docker run image_name
This command creates and starts a new container based on the provided image_name. If a name is not set for the container, a random one is generated and assigned by docker. In the above example 2 containers were created based on ubuntu.
with docker ps -a we can see that modest_jennings and optimistic_leakey are the random names created:
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cdb44750bffc ubuntu "/bin/bash" About a minute ago Exited (1) 4 seconds ago optimistic_leakey
4d5643e8c1a8 ubuntu "/bin/bash" 2 minutes ago Exited (0) 2 minutes ago modest_jennings
cat test.txt failed the 2nd time because the file didn't exist. The container started from a "clean" ubuntu image.
Actually, we created test.txt inside modest_jennings only.
docker start container_name
This command starts a stopped container. So, in our case, the file is still there:
$ docker start modest_jennings
modest_jennings
$ docker attach modest_jennings
root#4d5643e8c1a8:/# cat test.txt
test
root#4d5643e8c1a8:/#
docker commit container_name image_name
This command is to create a new image, so that you can use it later and run containers based on that image. Continuing our example...
$ docker commit modest_jennings my_ubuntu
sha256:a4357f37153ac0b94e37315595f1a3b540538283adc3721df4d4e3b39bf8334f
$ docker run -it my_ubuntu
root#2e38616d532a:/# cat test.txt
test
root#2e38616d532a:/#
If you want a custom image, you can create a Dockerfile
`FROM ubuntu:16.04
ADD ./test.txt /tmp/`
after you can build it docker build -t ubuntu:custom .
and finally run your custom image docker run --name myubuntu ubuntu:custom sleep 3000
You can check your file with docker exec -it myubuntu /bin/bash and more /tmp/test.txt

Docker look at the log of an exited container

Is there any way I can see the log of a container that has exited?
I can get the container id of the exited container using docker ps -a but I want to know what happened when it was running.
Use docker logs. It also works for stopped containers and captures the entire STDOUT and STDERR streams of the container's main process:
$ docker run -d --name test debian echo "Hello World"
02a279c37d5533ecde76976d7f9d1ca986b5e3ec03fac31a38e3dbed5ea65def
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
49daa9d41a24 debian "echo test" 2 minutes ago Exited (0) 2 minutes ago test
$ docker logs -t test
2016-04-16T15:47:58.988748693Z Hello World
docker logs --tail=50 <container id> for the last fifty lines - useful when your container has been running for a long time.
You can use below command to copy logs even from an exited container :
docker cp container_name:path_of_file_in_container destination_path_locally
Eg:
docker cp sample_container:/tmp/report /root/mylog
To directly view the logfile of an exited container in less, scrolled to the end of the file, I use:
docker inspect $1 | grep 'LogPath' | sed -n "s/^.*\(\/var.*\)\",$/\1/p" | xargs sudo less +G
run as ./viewLogs.sh CONTAINERNAME
This method has the benefit over docker logs based approaches, that the file is directly opened, instead of streamed.
sudo is necessary, as the LogPath/File usually is under root-owned
#icyerasor comment above actually helped me solve the issue. In my particular situation the container that has stopped running had no container name only container id.
Steps that found the logs also listed in this post
Find the stopped container via docker ps -a
grab the container id of the failed container
Substitute it in this command cat /var/lib/docker/containers/<container id>/<container id>-json.log

Docker container not starting

I'm trying to run this docker example. Yet it appears that I can't start my docker container.
sudo docker stop aff28c5dab3f
sudo docker start aff28c5dab3f
sudo docker ps
ID IMAGE COMMAND CREATED STATUS PORTS
sudo docker ps -a
ID IMAGE COMMAND CREATED STATUS PORTS
aff28c5dab3f shykes/pybuilder:latest /usr/local/bin/build 26 minutes ago Exit 0
52200b5c58a6 shykes/pybuilder:latest /usr/local/bin/build 10 hours ago Exit 0
b59e84340a7c ubuntu:12.04 echo hello 11 hours ago Exit 0
5c1bd5bc53d6 ubuntu:12.04 echo hello 12 hours ago Exit 0
and when I try to run
sudo docker attach aff28c5dab36, I see Impossible to attach to a stopped container, start it first
I forgot to attach to the container, and hence was getting the error. I ran sudo docker attach aff28c5dab36 and now everything works fine.
It's possible that when you run docker attch command, the buildapp command inside the container is already finished and the container is stopped automatically. If you run docker attach immediately after run the buildapp command, you'll see its output.

Resources