How to retain docker alpine container after exit is used?
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>
Docker container will automatically stop after docker run -d
The centos dockerfile has a default command bash
.
That means, when run in background (-d
), the shell exits immediately.
Update 2017
More recent versions of docker authorize to run a container both in detached mode and in foreground mode (-t
, -i
or -it
)
In that case, you don't need any additional command and this is enough:
docker run -t -d centos
The bash will wait in the background.
That was initially reported in kalyani-chaudhari's answer and detailed in jersey bean's answer.
vonc@voncvb:~$ d ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4a50fd9e9189 centos "/bin/bash" 8 seconds ago Up 2 seconds wonderful_wright
Note that for alpine, Marinos An reports in the comments:
docker run -t -d alpine/git
does not keep the process up.
Had to do:docker run --entrypoint "/bin/sh" -it alpine/git
Original answer (2015)
As mentioned in this article:
Instead of running with
docker run -i -t image your-command
, using-d
is recommended because you can run your container with just one command and you don’t need to detach terminal of container by hitting Ctrl + P + Q.
However, there is a problem with
-d
option. Your container immediately stops unless the commands keep running in foreground.
Docker requires your command to keep running in the foreground. Otherwise, it thinks that your applications stops and shutdown the container.
The problem is that some application does not run in the foreground. How can we make it easier?
In this situation, you can add
tail -f /dev/null
to your command.
By doing this, even if your main command runs in the background, your container doesn’t stop because tail is keep running in the foreground.
So this would work:
docker run -d centos tail -f /dev/null
Or in Dockerfile:
ENTRYPOINT ["tail"]
CMD ["-f","/dev/null"]
A docker ps
would show the centos container still running.
From there, you can attach to it or detach from it (or docker exec
some commands).
How to keep Docker container running after starting services?
This is not really how you should design your Docker containers.
When designing a Docker container, you're supposed to build it such that there is only one process running (i.e. you should have one container for Nginx, and one for supervisord or the app it's running); additionally, that process should run in the foreground.
The container will "exit" when the process itself exits (in your case, that process is your bash script).
However, if you really need (or want) to run multiple service in your Docker container, consider starting from "Docker Base Image", which uses runit
as a pseudo-init process (runit
will stay online while Nginx and Supervisor run), which will stay in the foreground while your other processes do their thing.
They have substantial docs, so you should be able to achieve what you're trying to do reasonably easily.
How to continue a Docker container which has exited
You can restart an existing container after it exited and your changes are still there.
docker start `docker ps -q -l` # restart it in the background
docker attach `docker ps -q -l` # reattach the terminal & stdin
Why docker container exits immediately
A docker container exits when its main process finishes.
In this case it will exit when your start-all.sh
script ends. I don't know enough about hadoop to tell you how to do it in this case, but you need to either leave something running in the foreground or use a process manager such as runit or supervisord to run the processes.
I think you must be mistaken about it working if you don't specify -d
; it should have exactly the same effect. I suspect you launched it with a slightly different command or using -it
which will change things.
A simple solution may be to add something like:
while true; do sleep 1000; done
to the end of the script. I don't like this however, as the script should really be monitoring the processes it kicked off.
(I should say I stole that code from https://github.com/sequenceiq/hadoop-docker/blob/master/bootstrap.sh)
How to make a docker container exit with a specific error-code with docker-compose?
Look at your error messages more closely:
ERROR: for docker-compose_dummy_1 Cannot start service dummy: OCI
runtime create failed: container_linux.go:345: starting container
process caused "exec: \"exit\": executable file not found in $PATH":
unknown
When you specify an entrypoint like ["exit", "42"]
, it's not executed in a shell. Docker is looking for a command in your $PATH
named exit
, and of course no such command exists.
You need to run your commands in a shell, because exit
is a shell command:
entrypoint: ["/bin/sh", "-c", "exit 42"]
Starting a shell in the Docker Alpine container
ole@T:~$ docker run -it --rm alpine /bin/ash
(inside container) / #
Options used above:
/bin/ash
is Ash (Almquist Shell) provided by BusyBox--rm
Automatically remove the container when it exits (docker run --help
)-i
Interactive mode (Keep STDIN open even if not attached)-t
Allocate a pseudo-TTY
Docker - Container is not running
Container 79b3fa70b51d
seems to only do an echo
.
That means it starts, echo and then exits immediately.
The next docker exec
command wouldn't find it running in order to attach itself to that container and execute any command: it is too late. The container has already exited.
The
docker exec
command runs a new command in a running container.The command started using
docker exec
will only run while the container's primary process (PID 1) is running
Related Topics
What Is Preemption/What Is a Preemtible Kernel? What Is It Good For
Selecting Text in Terminal Without Using the Mouse
Need Explanations for Linux Bash Builtin Exec Command Behavior
Execute a Shell Script Everyday at Specific Time
How Is It Possible That Kill -9 for a Process on Linux Has No Effect
Get First Line of a Shell Command's Output
Bash Foreach Loop Works Differently When Executed from .Sh File
Buffered Asynchronous File I/O on Linux
How to Run R Scripts on Servers Without X11
Differencebetween .Got and .Got.Plt Section
Tcp Handshake with Sock_Raw Socket
Split Output of Command by Columns Using Bash
How Does the 'Ls' Command Work in Linux/Unix
Track the Time a Command Takes in Unix/Linux
Getting Sudo and Nohup to Work Together
How to Force Linking with a Static Library When a Shared Library of Same Name Is Present