How to retain docker alpine container after "exit" is used?

Viewed 23687

Like for example if I use command docker run -it alpine /bin/sh it starts a terminal after which I can install packages and all. Now when I use exit command it goes back to the terminal. (main one)

So how can I access the same container again? When I run that command again, I get a fresh alpine.

Please help

5 Answers

Pull an image

docker image pull alpine

See that image is there

docker image ls   OR  just docker images

see what is inside the alpine

docker run alpine ls -al

Now your question is how to stay with the shell

docker container run -it alpine /bin/sh

You are inside shell script command line. Some distribution may have bash shell.

 docker exec -it 5f4 sh
 / # (<-- you can run linux command here!)

At this point, you can use command line of alpine and do

ls -al

type exit to come out- You can run it in detached mode and it will keep running.

With exec command we can login again

docker container run -it -d alpine /bin/sh

verify that it is UP and copy the FIRST 2 -3 digits of the container ID

docker container ls

login with exec command

docker exec -it <CONTAINER ID or just 2-3 digits> sh

You will need to STOP otherwise it will keep running.

docker stop <CONTAINER ID>

Run Alpine in background

$ docker run --name alpy -dit alpine
$ docker ps

Attach to Alpine

$ docker attach alpy
Related