Enter Your Email Address to Watch This Lesson

Your link to unlock this lesson will be sent to this email address.

Unlock this lesson and all 832 of the free egghead.io lessons, plus get Tools content delivered directly to your inbox!



Existing egghead members will not see this. Sign in.

Just one more step!

Check your inbox for an email from us and click link to unlock your lesson.



Run Short-Lived Docker Containers

1:56 Tools lesson by

Learn the benefits of running one-off, short-lived Docker containers. Short-Lived containers are useful to execute one-line commands or setup scheduled tasks. We'll demonstrate setting up a cronjob from the host machine to tap into the benefits of running automated, timed scripts with Docker.


egghead.io comment guidelines

Avatar
egghead.io

Learn the benefits of running one-off, short-lived Docker containers. Short-Lived containers are useful to execute one-line commands or setup scheduled tasks. We'll demonstrate setting up a cronjob from the host machine to tap into the benefits of running automated, timed scripts with Docker.

Avatar
David

Wow, this is really cool!

Let's say I have a javascript file, destined to run via node. We'll call this 'my-test.js'

I don't think I could execute that script without creating a volume for the container for it to even have access to the script. After I did that, I could run the script.

But I'm just curious if there's a shorter/terser way of doing exactly that.

In reply to egghead.io
Avatar
Brandon

Can you watch this if you've never used Docker before? Is this suitable for beginners? Or is this more for someone who has already worked some with Docker before?

In reply to egghead.io
Avatar
Mark

Hi Brandon,

You can to grasp some of the concepts to Docker. However, watching through some of my other Docker videos will help understand more broader concepts.

Mark

In reply to Brandon
Avatar
Mark

Hi David,

You can also use the CP command to copy the my-test.js file into your Docker container, and that would eliminate the need to mount a volume. You have to get that file into the container somehow though :)

Mark

In reply to David

You will typically run docker containers as background daemons by specifying the -d flag. This keeps the container running in the background until it is terminated. However, sometimes you want to execute just a single command either to test something out or to execute a short lived task.

We can do this with --rm flag. Since docker containers only run as long as the specific command is active, the container will be removed immediately when the running command has completed.

We can see the actively run in command here "nginx -g 'daemon off." What if we don't know what the nginx -g flag does? Probably you want to do some research into the nginx help documentation.

We can do this by typing "docker run --rm nginx," and then the command which is "nginx -h." What just happened when you ran this command is that docker created the container, ran the command when we passed it, and then immediately terminated it and removed the container all in under a second.

It's possible to run worker jobs or queue task by setting up a cron job on the host server to run one of docker commands. Let's see an example. Let's type "docker run --rm debian hostname >> /tmp/containers."

Running the hostname command within docker will print out the container's container ID. Then we append the result to the temp container's directory of our host machine. We can inspect this successfully ran by typing "cat /tmp/containers."

We can set this up to run as a cron job on our host. Let's make this container run every minute of every day and continue to append to temp containers with a string of the container ID of our temporary one off container. If wait until the next minute and check the temp container's file, we'll see another container ID appended to the file.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?