prevent docker from running multiple containers with same image - docker

I have a docker image which I create a container from, I want to make sure that after creating the first container no one will create another container with the same image.
Is there a docker command or flag to do that?

No, there is no such flag. It is in fact quite common to create many containers from the same image.

Related

Docker volume from existent container

I'm kind of stuck,
I have a docker container that is running, and that container runs some elasticsearch inside.
But I forgot to use volume on the first deploy. So my container has lots of data inside, in a single folder in /app/data.
I would like to use the same container but use volume on that folder, without losing data inside...
So it will be possible to rebuild other containers to use the same volume.
Have you some tips to share?
The important thing is not to remove your container, or you'll lose all that data. I think docker cp is your friend here (docs here). Copy the data to the host, then start another container with a volume.
Once you've secured your data, you can stop and remove the first container.

docker run/start: Is there a significant impact on disk space by using the "run" command over and over?

I'm wondering what is the best practice for launching many containers (on the order of thousands per day) in terms of using docker container run or docker container start. I realize that start is used on a stopped container and run would be used to create a new container, but does it matter which one is used if the same underlying image is used across all the containers?
My guess is that since all the containers use the same image there would be very little overhead for creating many thousands of containers. In other words, just use docker container run over and over again.
Should I instead try to search for an existing container before starting a new one?
The easiest solution is to pass --rm to docker run. This will cause the container to be deleted as soon as it's done running, so repeated calls to it won't keep using more and more space.

Why do docker CLI commands default to controlling containers?

I'm new to Docker, and one of the things I'm interested in WRT to it is what are the majority use-cases. For example, These commands seem to do the same thing:
docker container rm
and
docker rm
i.e. the CLI provides a shorthand means of controlling containers rather than images (the command docker image ls is also valid).
Why does docker choose to provide a short-hand means of working with containers rather than with images?
From my experience I work more with containers than with images. You create the image once but may create a container from this image multiple times.
I think this is similar to classes and objects. A image is just a blueprint for a container, same as a class is a blueprint for an object. You create multiple objects from a class but you write the class just once so in the end you will also execute more commands for containers than for images.
I think this is the reason why the commands are focused on the containers.

When do I need Docker Volumes?

Trying to make sure I understand the proper usage of docker volumes. If I have a container running MongoDB that I plan to start and stop do I need a volume configured with I "docker run" the first time? My understanding is that if use Docker run once, then docker stop/start my data is saved inside the container. The volume is more useful if multiple containers want access to the data. Is that accurate or am I misunderstanding something?
Starting and stopping a container will not delete the container specific data. However, you upgrade containers by replacing them with new containers. Any changes to the container specific read/write layer will be lost when that happens, and the new container will go back to it's initial state. If there are files inside your container that you want to preserve when the container is replaced, then you need to store those files in a volume, and then mount that same volume in the new container.

Launch different containers from a Dockerfile

Is there any possibility to launch containers of different images simultaneously from a single "Dockerfile" ?
There is a misconception here. A Dockerfile is not responsible for launching a container. It's responsible for building an image (which you can then use docker run ... to create a container from). More info can be found on the official Docker documentation.
If you need to run many docker containers simultaneously I'd suggest you had a look at Docker Compose which you can use to run containers based on images either from the docker registry or custom-built via Dockerfiles
Also somewhat new to Docker, but my understanding is that the Dockerfile is used to create Docker images, and then you start containers from images.
If you want to run multiple containers you need to use an orchestrator like docker swarm or Kubernetes.
Those have their own configuration files that tell it which images to spin up.

Resources