Containers are meant to run to completion. You need to provide your container with a task that will never finish. Something like this should work:
apiVersion: v1
kind: Pod
metadata:
name: ubuntu
spec:
containers:
- name: ubuntu
image: ubuntu:latest
# Just spin & wait forever
command: [ "/bin/bash", "-c", "--" ]
args: [ "while true; do sleep 30; done;" ]
You could use this CMD in your Dockerfile
:
CMD exec /bin/bash -c "trap : TERM INT; sleep infinity & wait"
This will keep your container alive until it is told to stop. Using trap and wait will make your container react immediately to a stop request. Without trap/wait stopping will take a few seconds.
For busybox based images (used in alpine based images) sleep does not know about the infinity argument. This workaround gives you the same immediate response to a docker stop
like in the above example:
CMD exec /bin/sh -c "trap : TERM INT; sleep 9999999999d & wait"
A container exits when its main process exits. Doing something like:
docker run -itd debian
to hold the container open is frankly a hack that should only be used for quick tests and examples. If you just want a container for testing for a few minutes, I would do:
docker run -d debian sleep 300
Which has the advantage that the container will automatically exit if you forget about it. Alternatively, you could put something like this in a while
loop to keep it running forever, or just run an application such as top
. All of these should be easy to do in Kubernetes.
The real question is why would you want to do this? Your container should be providing a service, whose process will keep the container running in the background.
In your Dockerfile use this command:
CMD ["sh", "-c", "tail -f /dev/null"]
Build your docker image.
kubectl run debug-container -it --image=<your-image>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With