How To Keep Docker Container Running For Debugging

Docker provides restart policiesto control whether your containers start automatically when they exit, or when Docker restarts. Restart policies ensure that linked containers are started in the correct order. Docker recommends that you use restart policies, and avoid using process managers to start containers. The container keeps running, and you can exec whatever you want, and you can stop, start or restart the container.

In case if you dont want any application to be running,you can install monit which will keep your docker container running. Please let us know if these two cases worked for you to keep your container running.

The right way to keep docker container started when it used for periodic tasks

In this case, as long as the httpd daemon is running, the container will remain alive. But if you run a base ubuntu image, the container will exit right after running it. A restart policy only takes effect after a container starts successfully. In this case, starting successfully means that the container is up for at least 10 seconds and Docker has started monitoring it. This prevents a container which does not start at all from going into a restart loop. Since you mentioned periodic tasks and you are probably using something like cron because of the way you want to use docker exec, I have just the medicine for you. At least I ended up doing something like this.

  • Docker provides restart policiesto control whether your containers start automatically when they exit, or when Docker restarts.
  • Additionally you can also use –details to show extra details provided to logs.
  • But if you still feel the issue is not resolved, you can try troubleshooting the issue by viewing the logs.
  • On Linux, you can modify the kernel’s buffer size by changing/proc/sys/fs/pipe-max-size.
  • Let’s see how to add an Entrypoint that keeps the ubuntu image running.

Do not try to combine Docker restart policies with host-level process managers, because this creates conflicts. Docker container exits if task inside is done, so if you want to keep it alive even if it does not have any job or already finished them, you can do docker run -di image. After you do docker container ls you will see it running. Now the containers are always running, use docker exec to enter the container and debug the program. It helps you build software with consistent environment across all platforms, so that you don’t have to worry about ‘It works on my machine’ issues.

Incorrect Path to App in Dockerfile:

In the official Nginx image, the Nginx foreground process is part of the Dockerfile. Another option is to use command option and specify the same commands which were used in Dockerfile above. Here are a couple of ways I found to keep the container running, and be able to inspect what’s going on in the container. I mainly use it together with docker-compose and ofelia to supply side-car containers to backup some other service in another container (e.g. MariaDB databases).

I also found -i work in place of -t, or you can also use both -it combined . A Docker container runs a process (the “command” or “entrypoint”) that keeps it alive. The container will continue to run as long as the command continues to run. This also works if you add the tail command at the end of an entrypoint file. Once you have the running container, you can attach the container to the terminal session using the exec parameter as shown below. Let’s see how to add an Entrypoint that keeps the ubuntu image running.

Sign up or log in

But if you still feel the issue is not resolved, you can try troubleshooting the issue by viewing the logs. A simple way to keep a container alive in daemon mode indefinitely is to run sleep infinity as the container’s command. This does not rely doing strange things like allocating a TTY in daemon mode. Although it does rely on doing strange things like using sleep as your primary command. Also, let’s look at another 3 methods to keep the container running with the docker run command. On Linux, you can avoid a restart by reloading the Docker daemon.

  • I actually like this answer the best, because the most popular answer suggests that you need a command (i.e. tail -f /dev/null).
  • But if you run a base ubuntu image, the container will exit right after running it.
  • Needs to review the security of your connection before proceeding.
  • Otherwise, send aSIGHUP signal to the dockerd process.
  • This whole business of whether or not you can start a stopped container, is dependant on how the container was originally created, i.e. run.

The live restore option only pertains to standalone containers, and not to swarm services. If swarm managers are not available, swarm services continue to run on worker nodes but cannot be managed until enough swarm managers are available to maintain a quorum.

Please Share This

Our goal is to continue to build a growing DevOps community offering the best in-depth articles, interviews, event listings, whitepapers, infographics and much more on DevOps. Running an instance of a Docker image, is known as a container. You can run multiple container of the same image. On Linux, you can modify the kernel’s buffer size by changing/proc/sys/fs/pipe-max-size. You cannot modify the buffer size on Docker Desktop for Mac or Docker Desktop for Windows. Needs to review the security of your connection before proceeding.

Following are the use cases where you will need a basic running container. Here is a basic Dockerfile with an ENTRYPOINT that will keep on running without getting terminated.

You can configure the daemon so that containers remain running if the daemon becomes unavailable. The live restore option helps reduce container downtime due to daemon crashes, planned outages, or upgrades. Restart policies for swarm services are configured differently.

docker keep container running

You can also set some limits if your periodic tasks get stuck & have memory leaks or whatever. There are multiple options out there to run the container in foreground/detached state.

Article was published on: 10/14/22

Author: Viktor Nikolaev

Victor is a professional crypto investor and stockbroker, specializing in such areas as trading on the stock exchange, cryptov currencies, forex, stocks and bonds. In this blog he shares the secrets of trading, current currency indices, crypt currency rates and tells about the best forex brokers. If you have any questions, you can always contact nikolaev@forexaggregator.com

Leave a Reply