How to run docker image as singleton

2019-08-04 10:26发布

I'm new to docker.

I have an image that I want to run, but I want docker to see if that image is already running from another terminal...if it is running I don't want it to load another one...

is this something that can be done with docker?

if it helps, I'm running the docker with a privileged mode.

I've tried to search for singleton docker or something like that, but no luck.

updates- 1.working from ubuntu. My scenario- from terminal X I run docker run Image_a from terminal Y I run docker run Image_a when trying to run from terminal Y, I want docker to check if there is already a docker running with Image_a, and the answer is true - I want docker not to run in terminal Y

标签: docker
1条回答
再贱就再见
2楼-- · 2019-08-04 11:00

You can use the following docker command to get all containers that running from specific image:

docker ps  --filter ancestor="imagename:tag"

Example:

docker ps  --filter ancestor="drone/drone:0.5"

Example Output:

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
3fb00087d4c1        drone/drone:0.5     "/drone agent"      6 days ago          Up 26 minutes       8000/tcp            drone_drone-agent_1

This approach uses docker api and docker daemon, so it doesnt matter if the run command executed in background or other terminal.

Aother approach:

If you have a single container form a single image:

Try naming your containers, You cant have 2 containers with the same name:

docker run --name uniquecontainer Image_a

Next time you run the above command you will get an error. Btw consider using -d so you dont have to switch terminals.

docker run -d --name uniquecontainer Image_a
查看更多
登录 后发表回答