'docker ps' and 'docker-compose ps' commands give different result - docker

docker ps is giving me a different output compared to docker-compose ps.
For example
docker ps
is not showing the same containers as
docker-compose ps
and vice-versa.
What is the reason for this?
I was thinking docker-compose is working on top of docker.

docker ps lists all running containers in docker engine. docker-compose ps lists containers related to images declared in docker-compose file.
The result of docker-compose ps is a subset of the result of docker ps.

docker ps - lists all running containers in Docker engine.
docker-compose ps - lists containers for the given docker compose configuration. The result will depend on configuration and parameters passed to docker-compose command.
Example
Start the containers with the following command:
docker-compose -p prod up -d
(-p in the command above defines the project name)
Running docker-compose ps won't list containers since the project name parameters is not passed:
docker-compose ps
Name Command State Ports
------------------------------
Running docker-compose -p prod ps will list all containers:
Name Command State Ports
--------------------------------------------------------------------------------------------------------
dev_app_1 sh -c exec java ${JAVA_OPT ... Up 0.0.0.0:5005->5005/tcp, 0.0.0.0:9001->9000/tcp
dev_database_1 docker-entrypoint.sh postgres Up 0.0.0.0:5432->5432/tcp
dev_nginx_1 /docker-entrypoint.sh ngin ... Up 0.0.0.0:8443->443/tcp, 0.0.0.0:8080->80/tcp
dev_pgadmin_1 /entrypoint.sh Up 443/tcp, 0.0.0.0:9100->80/tcp
The same goes if you define for example docker compose files with -f parameter.

Related

getting information for the existing docker containers

I am running a docker image using the following command:
sudo docker run -it -v "${pwd}:/qc/output" qc
It works perfectly then I used the following command to get the list of existing docker containers
sudo docker ps --filter ancestor=ubuntu
but it returns only headers which is the following line:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
do you know why it is empty and how I can get the information for the existing docker containers?
you will get all running containers details by command
docker ps
use docker ps -a to see all active and non-active container details

docker container not removing from docker

I am trying to remove container but when i run docker-compose rm ,runs fine but when i run docker ps then again it shows container:
root#datafinance:/tmp# docker-compose rm
Going to remove tmp_zookeeper_1_31dd890a1cbf
Are you sure? [yN] y
Removing tmp_zookeeper_1_31dd890a1cbf ... done
root#datafinance:/tmp# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
03b08e4ef0b3 confluentinc/cp-zookeeper:latest "/etc/confluent/dockā€¦" 14 hours ago Up 14 hours docker_c_zookeeper_1_7c953dce7d69
Use docker-compose ps, it will show the container which only launched by docker-compose up. If it shows there is no container, then this means this container was not launched by this docker-compose.yaml.
And Error starting userland proxy: listen tcp 0.0.0.0:32181: bind: address already in use' means the port 32181 is occupied either by other docker container or other process. You could use docker rm -f $(docker ps -qa) to delete all containers or more you can use netstat -oanltp | grep 32181 to find which process really occupy 32181.
Finally, if for any reason you did not able to delete container as you said, you can just use service docker restart or systemctl restart docker to make all container down. Then repeat above docker rm xxx.
With above steps, you can use docker compose up -d to use your service now.
try this :
docker rm -f 03b08e4ef0b3
DANGER
you may also try this, but be aware that will delete everything (Containers, Images, Networks, ....)
docker system prune -a -f
when all not helped your last resort is to restart Docker daemon
service docker restart
and then repeat the steps...
I think what you are looking for is :
docker-compose down
which removes the containers after stopping them according to this.
According to this, docker-compose rm removes the "stopped" containers. If your container(s) are running, I think it won't remove to prevent accidents.

last used docker-compose command

Let's say I have started my containers with:
docker-compose -f docker-compose.yml -f docker-compose.override.localhost.yml up --build
Is there a way to lookup, for a given running container, what the command was that started it? Something (pseudocode) like docker origin-command <container id> or similar?
sure you can.
use the inspect docker command:
docker container inspect <container_id>
this command will output a huge json object, what you need is under Config.CMD which appears to be an array containing the container entrypoint command and its flags.

Docker for Mac. docker run -d -p 80:80 --name webserver nginx shows another container with this name. but docker ps shows empty list

I am learning "Docker for Mac"
$ docker run -d -p 80:80 --name webserver nginx
docker: Error response from daemon: Conflict. The name "/webserver" is already in use by container 728da4a0a2852869c2fbfec3e3df3e575e8b4cd06cc751498d751fbaa75e8f1b. You have to remove (or rename) that container to be able to reuse that name..
But when I run
$ docker ps
It shows no containers listed.
But due to the previous error message tells me that there is this container 728da....
I removed that container
$ dockder rm 728da4a0a2852869c2fbfec3e3df3e575e8b4cd06cc751498d751fbaa75e8f1b
Now I run this statement again
$ docker run -d -p 80:80 --name webserver nginx
It is working fine this time.
And then I run $ docker ps, I can see this new container is listed
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3ecc0412fd31 nginx "nginx -g 'daemon off" 19 seconds ago Up 17 seconds 0.0.0.0:80->80/tcp, 443/tcp webserver
Note:
I am using "Docker for Mac".
But I had "Docker Box" installed on the Mac before. I don't know if that is the invisible "webserver" container comes from.
As activatedgeek says in the comments, the container must have been stopped. docker ps -a shows stopped containers. Stopped containers still hold the name, along with the contents of their RW layer that shows any changes made to the RO image being used. You can reference containers by name or container id which can make typing and scripting easier. docker start webserver would have restarted the old container. docker rm webserver would remove a stopped container with that name.
You can also abbreviate the container id's to the shortest unique name to save typing or a long copy/paste. So in your example, docker rm 728d would also have removed the container.
The Docker Getting Started document asks the learners trying two statements first.
docker run hello-world
and
docker run -d -p 80:80 --name webserver nginx
I was wondering why I can run
docker run hello-world
many times but if I run
docker run -d -p 80:80 --name webserver nginx
the second time, I got the name conflicts error. Many beginners would be wondering too.
With your help and I did more search, now I understand
docker run hello-world,
we did not use --name, in this case, a random name was given so there will be no name conflicts error.
Thanks!

Share and update docker data containers across containers

I have the following containers:
Data container which is build directly in quay.io from a github repo, basically is a website.
FPM container
NGINX container
The three of them are linked together and working just fine. BUT the problem is that every time I change something in the website (Data container) it is rebuilt (of course) and I have to remove that container and also the FPM and NGINX and recreate them all to be able to read the new content.
I started with a "backup approach" for what I'm copying the data from the container to a host directory and mounting that into the FPM and NGINX containers, this way I can update the data without restarting/removing any service.
But the idea of moving the data from the data container into the host, really doesn't like me. So wondering if there a "docker way" or a better way of doing it.
Thanks!
UPDATE: Adding more context
Dockerfile d`ata container definition
FROM debian
ADD data/* /home/mustela/
VOLUME /home/mustela/
Where data only has 2 files: hello.1 and hello.2
Compiling the image:
docker build -t="mustela/data" .
Running the data container:
docker run --name mustela-data mustela/data
Creating another container to link to the previous one:
docker run -d -it --name nginx --volumes-from mustela-data ubuntu bash
Listing the mounted files:
docker exec -it nginx ls /mustela/home
Result:
hello.1 hello.2
Now, lets rebuild the data container image, but first adding some new files, so now inside data we have hello.1 hello.2 hello.3 hello.4
docker rm mustela-data
docker build -t="mustela/data" .
docker run --name mustela-data mustela/data
If I ls /home/mustela from the running container, the files aren't being updated:
docker exec -it nginx ls /mustela/home
Result:
hello.1 hello.2
But if I run a new container I can see the files
docker run -it --name nginx2 --volumes-from mustela-data ubuntu ls /home/mustela
Result: hello.1 hello.2 hello.3 hello.4

Resources