Why is docker-compose ps different from docker ps? - docker

Why does docker compose create containers that are only accecible from docker-compose ps and that persist after killing running container ?

It doesn't.
docker ps only shows running containers, docker-compose ps shows all containers related to the current compose file, running and stopped. docker-compose kill just force stops the container and it can be restarted with docker-compose start, it will therefore be visible when running docker-compose ps but not docker ps.
To list all containers with docker use docker ps -a. To removed stopped containers related to a compose file run docker-compose rm, if you want to stop and remove all containers, have a look at docker-compose down.

docker-compose is the software wrapper around docker and there is not full support still. You can try to read a little bit about kubernets and mesos for the comparing different clusters built on top of docker or similar container systems.
article on blog about swarm and compose

Related

why does docker ps shows empty containers

I start with
docker run rasa/rasa
as tutorials from Youtube suggest.
but when I do
docker ps
it shows empty list of containers. I thought run command should start container processing.
Why is doesnt start? What am I doing wrong?
Also
docker ps -a
shows me a giant list of exited containers, but I didn't exit them.
With command docker ps -a command shows you all the containers running/stopped/exited. These are the containers that you tried to run but somehow due to some internal errors those container got stopped and are not running anymore and it shows the status exited(not running).
With command docker ps, it shows only running containers. As no containers are running on your machine thats why it is not showing anything. It requires -a argument to show you the all containers like docker ps -a (Show all containers).

Is there any difference between docker start and docker container start?

I was wondering if there is any difference between
docker start <container name>
and
docker container start <container name>
I personally always use docker container start though, because that is the method that was suggested to run a stopped container. What would be the difference if I use docker start instead?
There is no difference between docker container start and docker start.
Over time, the docker cli has become more organized so that, for example, there are separate docker container inspect and docker image inspect commands. Earlier, there was a single command that would do both depending on the arguments, which could be confusing. There are a number of commands that are there for historic reasons (like docker ps, docker inspect, etc) that duplicate functionality that is now also available via subcommands of docker container, docker image, and so on.

Docker ps disappeared after restart system

Run docker container on a Linux system. From docker ps can see all the processes.
After restart the system and run docker ps can't see some containers, but use docker ps -a can see them. Is the container still running?
If you don't set the option --restart=always when run the docker container, these containers will not be started automatically, after you restart the system.
Restart policies (–restart)
always - Always restart the container regardless of the exit status. When you specify always, the Docker daemon will try to restart the container indefinitely. The container will also always start on daemon startup, regardless of the current state of the container.
Refer: docker run - Restart policies (–restart)

Difference between docker restart & docker-compose restart

I'm using docker-compose.yml to setup docker containers. And I have started the services using docker-compose up -d.
Now every time I deploy the application to the server I need to restart one of the services.
Previously I used to run the container without docker-compose using just the docker run command like this: docker run --name test-mvn -v "$(pwd)":/usr/src/app test/mvn-spring-boot -d.
And to restart the container I used to do docker restart test-mvn.
But now there are two options out there docker-compose restart and docker restart. I'm not sure which one I should prefer.
I want to know what is the difference between these two options and which one I should use in my case.
With docker-compose you manage a services, typically constituting multiple containers, while docker manages individual containers. Thus docker-compose restart will restart all the containers of a service and docker restart only the given containers.
Assuming "one of the services" in your question refers to an individual container I would suggest docker restart.

How to stop a docker container which started with `--restart=always`

Is there any way to stop a docker container which started with --restart=always like following
sudo docker run -it --restart=always <image_id>
Here's the mighty eagle that docker has recently included. :D
You can update docker container.
use sudo docker update --restart=no <container_id> to update --restart flag of the container.
Now you can stop the container.
You should be able to just use docker stop and then docker rm to make sure the container doesn't restart when the daemon restarts.
Your question is an issue on the docker github and someone has made some comments about to how to solve here
I'm not sure if it's intended behavior to restart a stopped container on daemon restart... but for sure docker rm would be all that is needed, no need to remove the image.
If you use docker stop or docker kill, you're manually stopping the container so it will not restart. You can do some tests about restart policies: restarting the docker daemon, rebooting your server, using a CMD inside a container and running an exit...
See this answer for more details:
https://serverfault.com/a/884823/381420
TL;DR
Also check docker swarm if there are any stacks that spin up containers there. Simply run docker stack ls followed by docker rm <stack_name>.
Longer version
This is not exactly an answer to your question, but I had a very similar issue where containers kept spinning up even if I ran docker update --restart=no <container_id>, docker stop <container_id> and docker rm <container_id>. These were some old containers, so I had no clue how I generated them.
After some Googling, I realized that it was a docker swarm stack that kept spinning up containers. By running docker stack ls followed by docker rm <stack_name>, I was able to stop the auto spin-up of the containers and thus remove them entirely.

Resources