Video Lesson: Run one-off containers in Docker

Run one-off containers in Docker

Learn how to run disposable Docker containers for isolated, one off tasks and automatically remove them when complete.


Lesson Content

Docker containers are cheap to run: they use little disk space and memory, start quickly, and can be removed as soon as their work is complete. This disposable nature makes them useful for isolated, one-off tasks.

Use docker run with the -it flag to start an interactive terminal, and the --rm flag to remove the container automatically when its process exits. Start an Ubuntu container and open a Bash prompt:

docker run -it --rm ubuntu bash

You are now at a command prompt inside the Ubuntu operating system. You can run any command available in the container. When you finish, exit the shell:

exit

Because you used --rm, Docker removes the container as soon as the Bash process exits. Check all containers, including stopped containers, to confirm that it is gone:

docker ps -a

You can also skip the interactive Bash prompt and run a command directly. Pass the command after the image name:

docker run --rm ubuntu ls

Docker starts the Ubuntu container, runs ls, displays its output on standard output, exits the Ubuntu process, and removes the container. The entire operation completes in less than a second.

Maggie

Hey, I'm Maggie! 👋

Ask me anything.

Want to chat with Maggie?

Log in for AI tutor access.

Comments

Join the discussion!

Notes

Keep track of what you learn!