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

881
Visualizações
How to restart container using container-id?

I created a container using the command

docker run ubuntu /bin/bash -c "echo 'cool content' > /tmp/cool-file"

Now I see the container has exited

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
9e5017aef3f9        ubuntu              "/bin/bash -c 'echo '"   38 seconds ago      Exited (0) 36 seconds ago                       elegant_euler

Question: How can I restart and get in to the interactive mode for this container using its container-id?
I cannot use docker run -it <image_name> since this expects image name and not container id. I tried using docker attach , but I think this only works for running containers. I don't want to commit this container just yet so, how can I restart and get in to interactive mode for this container using it's container-id?
EDIT: I'm able to get in to other containers using docker start {container-id} and then running docker attach {container-id}. I wonder if there is something peculiar with the way I created the container which would result in this behavior. I'm just starting out with docker so do direct me in right direction if I'm missing some basic bit.

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

0

A container exits when it completes its command. So the container started with

docker run ubuntu /bin/bash -c "echo 'cool content' > /tmp/cool-file"

will exit as soon as the command echo is completed. In this case it doesn't make sense to restart that container.

If you run a new container in detached mode you'll be able to keep it live and to attach it in a second time.

So, in your case you should run a new container from the image in detached mode running a command like /bin/bash , then you can run the echo and attach it

docker run -d -ti ubuntu /bin/bash
docker exec -ti <containerId> /bin/bash -c "echo 'cool content' > /tmp/cool-file"

The container will be kept alive, so you can exec more commands on it, e.g. docker exec -ti /bin/bash -c "cat /tmp/cool-file"

or run a new /bin/bash to "attach" your container and to work in it as a command prompt

docker exec -ti <containerId> /bin/bash
root@<containerId>:/# cat /tmp/cool-file 
cool content

You can succesfully stop / start /restart this container

docker stop <containerId> && docker start <containerId>

or

docker restart <containerId>

Remind that when you restart a container it executes again its original command. So if you would able to restart the container of your use case (but you dont't) it would run again /bin/bash -c "cat /tmp/cool-file"

Restarting a container that run with command /bin/bash , it will run again the same command when restarting.

You normally can't change the command to RUN when restarting an existing container; to do it you can try some tricks as suggested at How to start a stopped docker container with a different command.

over 4 years ago · Santiago Trujillo Relatório

0

i tried myself:

docker restart <container_id>

docker exec -it <container_id> bash

works both perfect to restart and get into interactive terminal.

over 4 years ago · Santiago Trujillo Relatório

0

Check Docker start command

docker stop {containerId} && docker start -i {containerId}
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