Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

879
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

0

Check Docker start command

docker stop {containerId} && docker start -i {containerId}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda