{"id":953,"date":"2024-04-26T01:37:00","date_gmt":"2024-04-26T01:37:00","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=953"},"modified":"2024-04-26T01:37:00","modified_gmt":"2024-04-26T01:37:00","slug":"keep-docker-container-running","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=953","title":{"rendered":"How to Keep Docker Container Running for Debugging"},"content":{"rendered":"<p>This post will look at how to keep a <a href=\"https:\/\/devopscube.com\/what-is-docker\/\" rel=\"noreferrer noopener\">Docker container<\/a> running for testing, debugging, and troubleshooting purposes.<\/p>\n<h2 id=\"why-does-docker-container-exit-immediately-after-starting\">Why Does Docker Container Exit Immediately After Starting?<\/h2>\n<p>When you are getting started with Docker, you might face problems with Docker container exiting immediately after starting.<\/p>\n<p>It does not happen if you run an official Nginx container.<\/p>\n<p>However, if you run a base image like Ubuntu, busybox etc, or <a href=\"https:\/\/devopscube.com\/build-docker-image\/\">build a Docker image<\/a> without a long running process, the container will exit right after running it.<\/p>\n<p>The following screenshot shows both the running Nginx container and exited Ubuntu container.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/docker-running-min-1.png\" class=\"kg-image\" alt=\"contianer running and exiting examples.\" loading=\"lazy\" width=\"2000\" height=\"946\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/docker-running-min-1.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w1000\/2025\/03\/docker-running-min-1.png 1000w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w1600\/2025\/03\/docker-running-min-1.png 1600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/docker-running-min-1.png 2008w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<p>Here is why the container exit.<\/p>\n<ol>\n<li>To keep the container running, you need a foreground process added to the <a href=\"https:\/\/devopscube.com\/run-scripts-docker-arguments\/\" rel=\"noreferrer noopener\">Docker Entrypoint.<\/a> It should be a long running process.<\/li>\n<li>In the official Nginx image, the Nginx foreground process is part of the Dockerfile Entrypoint . See <a href=\"https:\/\/github.com\/nginxinc\/docker-nginx\/blob\/464886ab21ebe4b036ceb36d7557bf491f6d9320\/mainline\/debian\/Dockerfile?ref=devopscube.com\" rel=\"noreferrer noopener\">official Nginx Dockerfile<\/a>.<\/li>\n<li>Whereas, in the base Ubuntu image, there is no Entrypoint for the foreground process. That&#8217;s why it <strong>gets exited<\/strong> right after you execute the docker run command.<\/li>\n<\/ol>\n<p>Let&#8217;s look at how to add an <strong><code>ENTRYPOINT<\/code><\/strong> to Docker that keeps the conatiner image running.<\/p>\n<h2 id=\"dockerfile-command-to-keep-the-container-running\">Dockerfile Command to Keep the Container Running<\/h2>\n<p>Also, let&#8217;s look at another four differnt methods to keep the container running with the docker run command.<\/p>\n<h3 id=\"method-1-interactive-shell-session-with-pseudo-tty\">Method 1: Interactive Shell Session with pseudo-tty<\/h3>\n<p>You can use the <code>-t<\/code> (pseudo-tty) docker parameter to keep the container running. Replace <strong><code>tty-container <\/code><\/strong>with required name and <strong><code>ubuntu<\/code><\/strong> with required image.<\/p>\n<pre><code>docker run -it --name tty-container ubuntu \/bin\/bash<\/code><\/pre>\n<h3 id=\"method-2-using-the-tail-command\">Method 2: Using the tail command<\/h3>\n<p>You can run the container directly by passing the tail command via <a href=\"https:\/\/devopscube.com\/entrypoint-vs-cmd-explained\/\">CMD arguments<\/a> as shown below.<\/p>\n<pre><code> docker run -d ubuntu tail -f \/dev\/null<\/code><\/pre>\n<h3 id=\"method-3-using-sleep-infinity\"><strong>Method 3:<\/strong> Using sleep infinity<\/h3>\n<p>Another method is to execute a Linux sleep command to infinity. It essentially keeps the container running indefinitely.<\/p>\n<pre><code>docker run -d ubuntu sleep infinity<\/code><\/pre>\n<p>Once you have the running container, you can attach the container to the terminal session using the <code>exec<\/code> parameter shown below.<\/p>\n<p><code>0ab99d8ab11c<\/code> is the container ID<\/p>\n<pre><code>docker exec -it 0ab99d8ab11c \/bin\/bash<\/code><\/pre>\n<h3 id=\"method-4-using-keep-alive-command-in-entrypoint\">Method 4: Using <strong>Keep-Alive<\/strong> command in ENTRYPOINT<\/h3>\n<p>You can also use the keep alive command like <strong><code>tail -f \/dev\/null<\/code><\/strong> the Dockerfile.<\/p>\n<p>Here is a basic Dockerfile with an ENTRYPOINT that will keep on running without getting terminated.<\/p>\n<pre><code>FROM busybox:latest\n\nENTRYPOINT [\"tail\", \"-f\", \"\/dev\/null\"]<\/code><\/pre>\n<p>The tail command follows the <strong><code>\/dev\/null<\/code><\/strong>, which discards all the data written to it. Due to which the tail command stays active.<\/p>\n<h2 id=\"keep-container-running-in-kubernetes\">Keep Container Running in Kubernetes<\/h2>\n<p><a href=\"https:\/\/devopscube.com\/kubernetes-pod\/\">Kubernetes pods<\/a> exits if you the main container running inside the pod doest have long-running process or a command that keeps the container running.<\/p>\n<p>So if the default container images <a href=\"https:\/\/devopscube.com\/entrypoint-vs-cmd-explained\/\">Entrypoint<\/a> doest have command that keeps running, the container exits immediately and the pod will go the completed state.<\/p>\n<p>For example, if you run a pod with ubuntu or busybox base image, the pod will exit immediately.<\/p>\n<p>Now, if you want to run a pod with image that doesnt have long running process, you will have to add <strong>custom command and arguments<\/strong> to the container specification.<\/p>\n<p>Here is a <a href=\"https:\/\/devopscube.com\/kubernetes-deployment-tutorial\/\">Kubernetes deployment<\/a> example that uses a plain busybox image where we add a custom command sleep infinity to keep the pod running.<\/p>\n<pre><code>apiVersion: apps\/v1\nkind: Deployment\nmetadata:\n  name: test-deployment\nspec:\n  replicas: 1\n  selector:\n    matchLabels:\n      app: test-app\n  template:\n    metadata:\n      labels:\n        app: test-app\n    spec:\n      containers:\n      - name: my-container\n        image: busybox:latest\n        command: [\"sleep\", \"infinity\"]<\/code><\/pre>\n<h2 id=\"in-summary\">In summary:<\/h2>\n<p>Following are the use cases where you will need a basic running container.<\/p>\n<ol>\n<li>To test\/develop docker images and their configuration<\/li>\n<li>To troubleshoot systems using utilities inside a container.<\/li>\n<li>To troubleshoot the <a href=\"https:\/\/devopscube.com\/kubernetes-tutorials-beginners\/\" rel=\"noreferrer noopener\">Kubernetes<\/a> cluster with required utilities on a pod.<\/li>\n<\/ol>\n<p>We hope this article helps! If it doesn&#8217;t solve your problem or have a different use case, please drop a comment below.<\/p>\n<p>If you want to learn about optimizing Docker images, check out our guide on <a href=\"https:\/\/devopscube.com\/reduce-docker-image-size\/\" rel=\"noreferrer noopener\">reducing docker image size<\/a>.<\/p>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/keep-docker-container-running\/\" target=\"_blank\" rel=\"noopener noreferrer\">How to Keep Docker Container Running for Debugging \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/keep-docker-container-running\/<\/p>\n","protected":false},"author":1,"featured_media":954,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-953","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/953","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=953"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/953\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/954"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=953"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=953"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}