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 bashYou 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:
exitBecause 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 -aYou can also skip the interactive Bash prompt and run a command directly. Pass the command after the image name:
docker run --rm ubuntu lsDocker 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.
Join the discussion!
Comments
Want to comment on this lesson?
Log in to leave comments.