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

6

21

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

Spock answered 11/8, 2017 at 15:28 Comment(2)
Possible duplicate of How to Keep Docker Container Running After Starting Services?Oxfordshire
may be not a duplicate questionFaustina
E
14

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>
Elisha answered 2/7, 2018 at 9:38 Comment(0)
A
38

The container lives as long as the specified run command process is still running. When you specify to run /bin/sh, once you exit, the sh process will die and so will you container.

If you want to keep your container running, you have to keep the process inside running. For your case (I am not sure what you want to acheive, I assume you are just testing), the following will keep it running

docker run -d --name alpine alpine tail -f /dev/null

Then you can sh into the container using

docker exec -it alpine sh  
Acie answered 11/8, 2017 at 15:31 Comment(4)
So how i can i keep it running and get back to it laterSpock
@user8094908 answer udpated.Acie
Here is what i want to do...I want to run alpine in docker, then install git,ant and maven on that command line. So when i exit the container, the installed things should stay and when i come back i could continue my work...Spock
@yamenK, I would avoid ping localhost for two reasons, one is constant log output and second it is actually doing something. You should be using variants which do nothing and print nothing. Bash infinite loop using ` while true; sleep 1; done` or the easier one to remember for me tail -f /dev/nullJuna
E
14

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>
Elisha answered 2/7, 2018 at 9:38 Comment(0)
M
4

Run Alpine in background

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

Attach to Alpine

$ docker attach alpy
Manizales answered 30/10, 2018 at 23:18 Comment(0)
S
2

You should use docker start, which allows you to start a stopped container. If you didn't name your container, you'll need to get it's name/id using docker ps.

For example,

$docker ps
CONTAINER ID        IMAGE                        COMMAND
4c01db0b339c        alpine                       bash    

$docker start -i -a 4c01db0b339c   
Stanislaw answered 11/8, 2017 at 15:35 Comment(3)
just prints the container id and does nothingSpock
@user8094908 Ah oops! That just starts the container. You also need to attach to it. I've updated my answer.Stanislaw
Thanks a lot i will bookmark this page for further reference.Spock
J
1

What you should do is below

docker run -d --name myalpine alpine tail -f /dev/null

This would make sure that your container doesn't die. Now whenever you need to install packages inside you just get inside the container using sh

docker exec -it myalpine /bin/sh

If for some reason your container dies, you can still start it again using

docker start myalpine
Juna answered 11/8, 2017 at 15:34 Comment(2)
It starts it in the background. I replaced you -it with -d which means detached mode. -d, --detach Run container in background and print container IDJuna
Thanks a lot. I guess there are many ways to do the same. I will check this method too.Spock
A
1

Another solution that probably is a little easier to remember. Simply use the sleep infinity ending (also supported by a few other Linux distributions) instead of the tail -f /dev/null ending suggested elsewhere:

docker run -d --name my_container alpine sleep infinity

Once you have the container running, you can attach the container to the terminal session using the exec parameter:

docker exec -it my_container /bin/sh
Ate answered 3/3, 2023 at 16:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.