0

A related question & answer on How to start a docker container (ubuntu image) suggest using docker run -it ubuntu to start a ubuntu container and connect to it. However the run command creates and starts a new ubuntu container.

How do we start an existing docker container (ubuntu image) given it's CONTAINER_ID without creating a new container?

Example:

docker ps -a

CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS                          PORTS               NAMES
9f297d02f419        ubuntu              "/bin/bash"         3 seconds ago        Exited (0) 1 seconds ago                            cranky_wilson

How do we start 9f297d02f419 ?

Community
  • 1
  • 1
Pranjal Mittal
  • 10,772
  • 18
  • 74
  • 99

2 Answers2

0

you can start the stopped container using docker start command -

eg: docker start 9f297d02f419

vulture
  • 9
  • 2
0

If you just use run on the Ubuntu image it will start a container that's running no command, which will immediately stop. You can docker start it but it will stop again. You can see it with docker ps -a.

The accepted answer in that question is very old and not very good. If you run that command on the current Docker version you get an error No command specified!

What you need to do is tell that container to run a command:

docker run ubuntu date

Will run a container from the image, run the date command, then exit. If you want to keep it running indefinitely, try something like:

docker run -d ubuntu tail -f /dev/null

You should see that the container is now running. The -d makes it run in the background, otherwise it will occupy your shell. And the final piece of the puzzle: since we have a container now that's configured to run a command, you can use docker ps to find its ID, and you can docker stop and docker start it at will.

Nauraushaun
  • 1,484
  • 12
  • 10