Docker Community Forums

Share and learn in the Docker community.

Basic question about starting and restarting containers


(Vytautasrask) #1

I just started to use docker days ago and do not understand one thing.
Basically that container vs image thing.
if I do:
docker run image_name
According to instructions from tutorials like (https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-centos-7).
I get running container and I can type commands into it.
But as I soon noticed this command creates new container for me from image every time I use it, right?
If I do:
docker container ls -a
My container list is getting larger and larger.
So can I stick to one container? Run it again and again?
Or maybe it is how the things are done, maybe its normal to get more and more containers after each ‘docker run’ command?


(Docker741) #2

Hi vytautasrask,

Dont use docker run [image name] again and again , you are just creating multiple containers using docker run.
if u want to have one container to work on , use docker run once and then u can docker stop [container id] and docker start [container id] to reuse the same container.


(Vytautasrask) #3

And what should I do if I want:
docker start [container id]
but interactive mode to type commands into it?
(Like run -it)


(Docker741) #4

when u start the container using docker start after that use following command for using interactive mode:
docker exec -it conatinerid bash

by using this u can interact with the container


(D95096201) #5

Hello,
Think of the image as a class and the container as an instance of that image .


(Vytautasrask) #6

Sorry I still do not get it. I do:
$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

148b2b8da957 gentoo/stage3-amd64 “/bin/bash” 19 hours ago Exited (0) 19 hours ago focused_brahmagupta

$ docker start 148b2b8da957 && docker exec -it 148b2b8da957 bash
148b2b8da957
Error response from daemon: Container 148b2b8da95708ff7dbac29605842423797dbb44bfd5ea48cfbe0a4c19c3e5c1 is not running
What is wrong?


(Docker741) #7

docker ps -a --> shows container which has stopped running as well as container which are running
docker exec command will run on those container which are in running state
docker ps will tell u which containers are running(only)

1)do a ---- docker start container id
2) type – docker ps
3)if u see ur container running do a docker exec

in the above example u can see the state of the container is exited thats y u cant do docker exec


(Vytautasrask) #8

hmm, I do:
$ docker start 148b2b8da957 && docker container ls
148b2b8da957
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
148b2b8da957 gentoo/stage3-amd64 “/bin/bash” 20 hours ago Up Less than a second focused_brahmagupta
Seems that my container runs for less than one second. How can I keep it running?


(Docker741) #9

may be while running a container u are using following step (just a guess) :
docker run image:tag
then u go inside the container

exit (type exit to get out of it)
then container stops itself .
to run a container in detached mode , spinup a new container using
docker run -d image:tag

if that doesnt work use docker logs container id(whiche exited) and see the error message


(Vytautasrask) #10

That does not work and:
$ docker logs b42f27efad2b
give no output. I use gentoo/stage3-amd64 image from hub.


(Docker741) #11

Please provide exact steps you are trying on docker and what are you trying to achiever.

PS - detailed Screenshots might help


(Vytautasrask) #12
-bash-4.2$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                 NAMES
-bash-4.2$ docker run -d gentoo/stage3-amd64
4c5396a521288ece0e0587ef448104d1ef2bd26e1214419a9303893d19993758
-bash-4.2$ docker ps -a
CONTAINER ID        IMAGE                 COMMAND             CREATED             STATUS                     PORTS               NAMES
4c5396a52128        gentoo/stage3-amd64   "/bin/bash"         3 seconds ago       Exited (0) 2 seconds ago                       laughing_visvesvaraya
-bash-4.2$

I want to do docker exec -it 4c5396a52128 bash now, but obviously it will not work. I want to keep that container running in other words, but it stops for some reason. It needs some process inside?


(Vytautasrask) #13

Oh I found answer:


docker run -t -d gentoo/stage3-amd64
Now I can:
docker exec -it 25963d3c576c bash
Even:
docker stop 25963d3c576c
25963d3c576c
docker start 25963d3c576c
25963d3c576c
docker exec -it 25963d3c576c bash

Thanks all for replies.


(David Maze) #14

When you’re done with a container, you can docker rm it. Or you can docker run --rm it initially and it will delete itself when it exits.

While typing interactive commands into containers is useful, Docker’s real power is the ability to spin up containers with software preinstalled and ready to provide network services. You should read the official Docker tutorial on building and running custom images as a next step.

(docker exec is a useful debugging tool but not part of my core Docker-based workflows. I almost never docker start. I docker rm extremely routinely.)


(Vytautasrask) #15

Thanks, David. I use it for slightly other thing now than network services, but docker run --rm seems quite good way too.