Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

300
Visualizações
docker restart container failed: "already in use", but there's no more docker image

I first got my nginx docker image:

docker pull nginx

Then I started it:

docker run -d -p 80:80 --name webserver nginx

Then I stopped it:

docker stop webserver

Then I tried to restart it:

$docker run -d -p 80:80 --name webserver nginx
docker: Error response from daemon: Conflict. The container name "/webserver" is already in use by container 036a0bcd196c5b23431dcd9876cac62082063bf62a492145dd8a55141f4dfd74. You have to remove (or rename) that container to be able to reuse that name..
See 'docker run --help'.

Well, it's an error. But in fact there's nothing in container list now:

docker container list
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

Why I restart nginx image failed? How to fix it?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

It is because

  • you have used --name switch.
  • container is stopped and not removed

You find it stopped

docker ps -a

You can simply start it using below command:

docker start webserver

EDIT: Alternatives If you want to start the container with below command each time,

docker run -d -p 80:80 --name webserver nginx

then use one of the following:

method 1: use --rm switch i.e., container gets destroyed automatically as soon as it is stopped

docker run -d -p 80:80 --rm --name webserver nginx

method 2: remove it explicitly after stopping the container before starting the command that you are currently using.

docker stop <container name>
docker rm <container name>
over 4 years ago · Santiago Trujillo Relatório

0

As the error says.

You have to remove (or rename) that container to be able to reuse that name

This leaves you two options.

  • You may delete the container that is using the name "webserver" using the command

    docker rm 036a0bcd196c5b23431dcd9876cac62082063bf62a492145dd8a55141f4dfd74

and retry.

  • Or you may use a different name during the run command. This is not recommended, as you no longer need that docker.

It's better to remove the unwanted docker and reuse the name.

over 4 years ago · Santiago Trujillo Relatório

0

While the great answers are correct, they didn't actually solve the problem I was facing.

How To:

Safely automate starting of named docker container regardless of its prior state

The solution is to wrap the docker run command with an additional check and either do a run or a stop + run (effectively restart with the new image) based on the result.

This achieves both of my goals:

  • Avoids the error
  • Allows me to periodically update the image (say new build) and restart safely
#!/bin/bash
# Adapt the following 3 parameters to your specific case
NAME=myname
IMAGE=myimage
RUN_OPTIONS='-d -p 8080:80'

ContainerID="$(docker ps --filter name="$NAME" -q)"

if [[ ! -z "$ContainerID" ]]; then
    echo "$NAME already running as container $ContainerID: stopping ..."
    docker stop "$ContainerID"
fi
echo "Starting $NAME ..."
exec docker run --rm --name "$NAME" $RUN_OPTIONS "$IMAGE"

Now I can run (or stop + start if already running) the $NAME docker container in a idempotent way, without worrying about this possible failure.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda