691

Running the docker registry with below command always throws an error:

dev:tmp me$ docker run \
     -d --name registry-v1 \
     -e SETTINGS_FLAVOR=local \
     -e STORAGE_PATH=/registry \
     -e SEARCH_BACKEND=sqlalchemy \
     -e LOGLEVEL=DEBUG \
     -p 5000:5000 \
     registry:0.9.1
Error response from daemon: Conflict. The name "registry-v1" is already in use by container f9e5798a82e0. You have to delete (or rename) that container to be able to reuse that name.

How can I prevent this error ?

Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
Rubytastic
  • 15,001
  • 18
  • 87
  • 175

22 Answers22

670

I got confused by this also. There are two commands relevant here:

docker run  # Run a command in a **new** container
docker start  # Start one or more stopped containers
vvvvv
  • 25,404
  • 19
  • 49
  • 81
Walt Howard
  • 7,676
  • 1
  • 16
  • 11
  • 166
    So it is also important to use `docker ps -a` to see all containers in this case. – Sławosz Oct 03 '16 at 10:48
  • 37
    yes, `docker start -a container-name` is the command you can use to start a container that has been created with `docker run`. Note the `-a` flag which is shorthand for `--attach`. This way the container is started in the foreground, just like when you use `docker run` (which runs a container in the foreground by default). – Krzysztof Wołowski Jan 09 '19 at 21:38
  • 2
    does `start` command rerun the container with the same settings (ports, dependencies, etc)? – Suncatcher Sep 02 '19 at 04:32
  • 2
    @Suncatcher Yes. – Henrik Sachse Oct 09 '19 at 11:04
  • this is another case of unfortunate naming. I try to exec a container which errors, tells me it's not running. Intuitively I run the container to resolve the issue but that creates a container ? jeez – aaaaaa Feb 14 '23 at 18:30
  • this may be useful information, still it's not a proper answer to the question. – johannes_lalala May 02 '23 at 15:13
369

That means you have already started a container in the past with the parameter

docker run --name registry-v1 ...

You need to delete that first before you can re-create a container with the same name with

docker rm registry-v1

When that container is sill running you need to stop it first before you can delete it with

docker stop registry-v1

Or simply choose a different name for the new container.

To get a list of existing containers and their names simply invoke

docker ps -a
John Smith
  • 7,243
  • 6
  • 49
  • 61
Henrik Sachse
  • 51,228
  • 7
  • 46
  • 59
  • 5
    But why are the ones that are stopped for? Or whats good if they are still there if they are stopped? – mskw Jan 17 '16 at 05:56
  • 5
    So you can run them later without recreating them – Scott Stensland Jan 19 '16 at 03:06
  • 3
    So you are basically saying "so that you can RUN the image once (i.e. produce the container then run the command in it), then START the container as many times as you wish". But why would we need to RUN something just once? Without even persisting the possible changes that "something" made to the container (remember, docker container state changes are lost unless committed). – Maksim Gumerov Apr 23 '16 at 10:09
  • 23
    If you know the name of your container you can remove it using this shortcut `docker rm $(docker ps -aq --filter name=myContainerName)` – Jujhar Singh May 06 '16 at 15:48
  • 1
    I had one stopped because I suspended my machine while it was running. I had started it with `--rm` but in this case, it was stopped (and with my limited skills, at least, there doesn't seem to be a way to resume it interactively). – tripleee Nov 11 '16 at 04:47
  • 9
    @JujharSingh Why not only `docker rm myContainerName`? – Rodrigo Jul 13 '19 at 00:48
218

Here what i did, it works fine.

step 1:(it lists docker container with its name)

docker ps -a

step 2:

docker rm name_of_the_docker_container
Thavaprakash Swaminathan
  • 6,226
  • 2
  • 30
  • 31
106

When you are building a new image you often want to run a new container each time and with the same name. I found the easiest way was to start the container with the --rm option:

--rm        Automatically remove the container when it exits

e.g.

docker run --name my-micro-service --rm <image>

Sadly it's used almost randomly in the examples from the docs

Edit: Read Lepe's comment below.

Martin
  • 2,316
  • 1
  • 28
  • 33
  • image or container? – Pim van der Heijden Jun 21 '19 at 16:26
  • 2
    Containers are run from built images. Roughly similar to class definition (image) and class instance (container). – Martin Jun 24 '19 at 08:16
  • First you say container, then image. That's unclear. Moreover, if a container for that image is already running, it will remain running after using `--rm`. It doesn't restart. – Pim van der Heijden Jun 25 '19 at 03:30
  • 52
    Just a note: `--rm` only works if you have created the container using `--rm` flag. This means, if you created the container without the `--rm` flag, you will have to remove it manually before you can take the advantage of the `rm` flag. – lepe Mar 22 '20 at 12:39
  • Sadly `--rm` doesn't work with `--restart always`. – Ralf Hundewadt Mar 04 '23 at 14:28
  • Does `--restart always` work with `--name`? Logically if you are always restarting the container then it makes little sense to try and delete it when it exits. – Martin Mar 06 '23 at 12:24
94

Just to explain what others are saying (it took me some time to understand) is that, simply put, when you see this error, it means you already have a container and what you have to do is run it. While intuitively docker run is supposed to run it, it doesn't. The command docker run is used to only START a container for the very first time. To run an existing container what you need is docker start $container-name. So much for asking developers to create meaningful/intuitive commands.

mithunpaul
  • 3,268
  • 22
  • 19
39

You have 2 options to fix this...

  1. Remove previous container using that name, with the command docker rm $(docker ps -aq --filter name=myContainerName)

    OR

  2. Rename current container to a different name i.e change this portion --name registry-v1 to something like --name myAnotherContainerName

You are getting this error because that container name ( i.e registry-v1) was used by another container in the past...even though that container may have exited i.e (currently not in use).

Edwin O.
  • 4,998
  • 41
  • 44
24

Cause

A container with the same name is still existing.

Solution

To reuse the same container name, delete the existing container by:

docker rm <container name>

Explanation

Containers can exist in following states, during which the container name can't be used for another container:

  • created
  • restarting
  • running
  • paused
  • exited
  • dead

You can see containers in running state by using :

docker ps

To show containers in all states and find out if a container name is taken, use:

docker ps -a
Roderick Jonsson
  • 1,111
  • 1
  • 10
  • 16
  • If one wishes to re-use the container, then --rm is not the appropriate switch. A simple "docker start {container name}" is sufficient. – Ken Ingram Jan 03 '20 at 19:36
20

Here is how I solved this on ubuntu 18:

  1. $ sudo docker ps -a
  2. copy the container ID

For each container do:

  1. $ sudo docker stop container_ID
  2. $ sudo docker rm container_ID
colidyre
  • 4,170
  • 12
  • 37
  • 53
Ali Faghihinia
  • 301
  • 2
  • 3
16

removing all the exited containers

docker rm $(docker ps -a -f status=exited -q)
max
  • 5,963
  • 12
  • 49
  • 80
13

The Problem: you trying to create new container while in background container with same name is running and this situation causes conflicts.

The error would be like:

Cannot create continer for service X :Conflict. The name X is already in use by container abc123xyz. You have to remove ot delete (or rename) that container to be able to reuse that name.

Solution rename the service name in docker-compose.yml or delete the running container and rebuild it again (this solution related to Unix/Linux/macOS systems):

  1. get all running containers sudo docker ps -a
  2. get the specific container id
  3. stop and remove the duplicated container / force remove it
sudo docker stop <container_id>
sudo docker rm <container_id>

or

sudo docker rm --force <container_id>
avivamg
  • 12,197
  • 3
  • 67
  • 61
13

I was running into this issue that when I run docker rm (which usually works) I would get:

Error: No such image

The easiest solution to this is removing all stopped containers by running:

docker container prune
Potherca
  • 13,207
  • 5
  • 76
  • 94
11

You can remove it with command sudo docker rm YOUR_CONTAINER_ID, then run a new container with sudo docker run ...; or restart an existing container with sudo docker start YOUR_CONTAINER_ID

Edwin O.
  • 4,998
  • 41
  • 44
donglei
  • 159
  • 2
  • 6
8

I have solved the issue by doing following steps and I hope it helps.

  1. Type docker ps -a to list all the containers in your system.
  2. Check the NAMES part where you have initialized your docker container.
  3. Then type docker rm --force name_of_container
  4. Install the docker container as you wish.

I had problem using NIFI and I have removed and reinstalled using docker. Good luck.

Nijat Mursali
  • 930
  • 1
  • 8
  • 20
7

TL:DR;

List all containers:
docker ps -a
Remove the concerned container by id:
docker container rm <container_id>

6

The OP's problem is the error. Deleting state isn't the only solution - or even a good one. The problem is docker run isn't re-entrant, and docker start is impotent w/o run. So we have to combine them.

For example to run Postgres w/o destroying previous state, try this:

docker start postgres || docker run -d -p 5432:5432 --name postgres -e POSTGRES_PASSWORD=password postgres:13-alpine
Michael Cole
  • 15,473
  • 7
  • 79
  • 96
5

I'm just learning docker and this got me as well. I stopped the container with that name already and therefore I thought I could run a new container with that name.

Not the case. Just because the container is stopped, doesn't mean it can't be started again, and it keeps all the same parameters that it was created with (including the name).

when I ran docker ps -a that's when I saw all the dummy test containers I created while I was playing around.

No problem, since I don't want those any more I just did docker rm containername at which point my new container was allowed to run with the old name.

Ah, and now that I finish writing this answer, I see Slawosz's comment on Walt Howard's answer above suggesting the use of docker ps -a

C. Tewalt
  • 2,271
  • 2
  • 30
  • 49
3

Ok, so I didn't understand either, then I left my pc, went to do other things, and upon my return, it clicked :D

  1. You download a docker image file. docker pull *image-name* will just pull the image from docker hub without running it.

  2. Now, you use docker run, and give it a name (e.g. newWebServer).

    docker run -d -p 8080:8080 -v volume --name newWebServer image-name/version

You perhaps only need docker run --name *name* *image*, but the other stuff will become useful quickly.

-d (detached) - means the container will exit when the root process used to run the container exits.

-p (port) - specify the container port and the host port. Kind of the internal and external port. The internal one being the port the container uses, and the external one is the port you use outside of it and probably the one you need to put in your web browser if that's how you access your app.

--name (what you want to call this instance of the container) - you could have several instances of the same container all with different names, which is useful when you're trying to test something.

image-name/version is the actual image you want to create the container from. You can see a list of all the images on your system with docker images -a. You may have more than one version, so make sure you choose the correct one/tag.

-v (volume) - perhaps not needed initially, but soon you'll want to persist data after your container exits.

OK. So now, docker run just created a container from your image. If it isn't running, you can now start it with it's name:

docker start newWebServer

You can check all your containers (they may or may not be running) with

docker ps -a

You can stop and start them (or pause them) with their name or the container id (or just the first few characters of it) from the CONTAINER ID column e.g:

docker stop newWebServer

docker start c3028a89462c

And list all your images, with

docker images -a

In a nutshell, download an image; docker run creates a container from it; start it with docker start (name or container id); stop it with docker stop (name or container id).

Coffee and Code
  • 885
  • 14
  • 16
3

I had this issue because I had two or more containers with the same container_name in the docker-compose.yml file.

T Olaleye
  • 112
  • 4
1

Simple Solution: Goto your docker folder in the system and delete .raw file or docker archive with large size.

Santosh
  • 1,275
  • 1
  • 10
  • 21
1

For me, the issue was that I used an image name more than once in the dockerfile.

enter image description here

Pavel M.
  • 374
  • 6
  • 7
0

When getting

Error: error creating container storage: the container name "{NAME}" is already in use by "{ID}". You have to remove that container to be able to reuse that name`

While docker/podman ps doesn't list that container, the container might still be registered in the containers.lock file: /var/lib/containers/storage/overlay-containers/containers.lock

Clearing the content of that file has resolved it for me, and I could run the same container name once more.

Noam Manos
  • 15,216
  • 3
  • 86
  • 85
-3

This happened to me on the docker tutorial! The port I tried to use was taken, but docker still created.. an image? A process to run docker? I'll find out soon. Anyways, to choose a different port, I had to remove the older image, and then docker run again.

Sometimes a tutorial can be too terse. What you want is concise, not terse, or even succinct.

Gerard ONeill
  • 3,914
  • 39
  • 25