0

I m aware of the docker run command to start an existing image as a container in the background.

G:\>docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
todoapp      latest    79fa51e9ccbb   4 hours ago   956MB

G:\>docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED       STATUS                   PORTS     NAMES
b74e0eafc4ce   todoapp   "docker-entrypoint.s…"   4 hours ago   Exited (1) 4 hours ago             example1

docker run -d -it -p 8000:8000 --name example1 todoapp

However, because the image is already build I now get this error.

G:\>docker run -d -it -p 8000:8000 --name example1 todoapp
docker: Error response from daemon: Conflict. The container name "/example1" is already in use by container "b74e0eafc4ce1eabef8ac0eeb041d8d14dce679273c98f88db5c9e2657422e95". You have to remove (or rename) that container to be able to reuse that name.
See 'docker run --help'.

Without needing to build the image again or deleting the image using rm I would like to simply start the docker container for this image in the background.

I however, did not find any option of sending the container to the background using docker start.

Can you please suggest?

Ashar
  • 2,942
  • 10
  • 58
  • 122
  • The problem is not the image, but the container name (`--name example1`). We can either change the name to something else (`docker run ... --name example2 ...`) or stop and remove the `example1` container (`docker stop b74e && docker rm b74e`). – Turing85 Aug 26 '21 at 21:03
  • The `--name` attribute is for runtime (for container and not for image) is what I thought. We are sure that the container is not running as the status is `Exited`. So is it not possible to start a non-running containing with the same name i.e `example1` ? Please confirm @Turing85 – Ashar Aug 26 '21 at 21:09
  • The container is stopped, but still present. If we want to get rid of it, we have to `docker rm ...` it. After that, it will no longer appear when we call `docker ps -a` and only then can its name be reused. – Turing85 Aug 26 '21 at 21:10
  • I just tested with a detached container. When I killed it and started it again with `docker start`, it ran detached. Isn't that what you want? – Hans Kilian Aug 26 '21 at 21:14
  • @HansKilian the problem - as described in the post - is that the "old container" is still present (container was not started with `--rm`) and the "new container" tries to use the same name. – Turing85 Aug 26 '21 at 21:18
  • @HansKilian it could be what i m looking for but the name=example1 container was not run with the `-d` option so will I have no option but to `stop & rm` to use the same name? Meanwhile, can you share your detach command along with start command for me to test your scenario? – Ashar Aug 26 '21 at 21:27
  • @Ashar `docker rm b74e` to remove the container (since it is already stopped, we do not need to stop it). – Turing85 Aug 26 '21 at 21:29
  • @Ashar I did `docker run -d --name=xyzzy nginx:alpine`. Then `docker stop xyzzy`. Then `docker start xyzzy`. – Hans Kilian Aug 27 '21 at 05:16

0 Answers0