Learn Docker With My Newest Course

Dive into Docker takes you from "What is Docker?" to confidently applying Docker to your own projects. It's packed with best practices and examples. Start Learning Docker →

Docker Tip #42: Using Docker for Long Running and One off Tasks

blog/cards/docker-tips-and-tricks.jpg

Docker is great at isolating processes, and they can either be long running processes (like a web server), or tasks that exit quickly.

A good example of a one off task that exits would be the official Docker “Hello World” image.

You can try it out by running docker container run hello-world. After a few seconds you’ll be greeted with a hello world message, but then the container / process stops.

It doesn’t run in an infinite loop until you manually kill it. Another good example of this would be a database backup script. That will run for X amount of time and then likely exit on its own when it finishes.

On the other hand, a long running process would be expected to live forever until it either crashes, or you manually kill it.

A text book example of that would be a web server, such as nginx. You can try that out by running docker container run -p 80:80 nginx:alpine. You’ll notice that the container does not exit on its own. It just waits to do work (in this case, serve web requests).

You could say that’s a long running process and we even published port 80 so you can check out http://localhost in your browser. It will log your request back to the terminal.

If you want nginx to stop, you would have to terminate it yourself, which you can do by pressing CTRL+C or stopping the container from a different terminal using docker stop <container id>.

Docker is a champion at running both types of tasks and you should use both when needed.

Free Intro to Docker Email Course

Over 5 days you'll get 1 email per day that includes video and text from the premium Dive Into Docker course. By the end of the 5 days you'll have hands on experience using Docker to serve a website.



Comments