Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to "docker run" a shell session on a minimal linux install and immediately tear down the container?

I just started using Docker, and I like it very much, but I have a clunky workflow that I'd like to streamline. When I'm iterating on my Dockerfile script I will often test things out after a build by launching a bash session, running some commands, finding out that such and such package didn't get installed correctly, then going back and tweaking my Dockerfile.

Let's say I have built my image and tagged it as buildfoo, I'd run it like this:

      $>  docker run -t -i  buildfoo 

              ... enter some bash commands.. then  ^D to exit

Then I will have a container running that I have to clean up. Usually I just nuke everything like this:

docker rm --force `docker ps -qa`

This works OK for me.. However, I'd rather not have to manually remove the container.

Any tips gratefully accepted !


Some Additional Minor Details:

Running minimal centos 7 image and using bash as my shell.

like image 912
Chris Bedford Avatar asked Sep 27 '15 08:09

Chris Bedford


People also ask

How do you run a container in detach mode?

To start a container in detached mode, you use -d=true or just -d option. By design, containers started in detached mode exit when the root process used to run the container exits, unless you also specify the --rm option.

Which docker command is used to attach to a running container?

Use docker attach to attach your terminal's standard input, output, and error (or any combination of the three) to a running container using the container's ID or name. This allows you to view its ongoing output or to control it interactively, as though the commands were running directly in your terminal.


3 Answers

Please use -rm flag of docker run command. --rm=true or just --rm.

It automatically remove the container when it exits (incompatible with -d). Example:

docker run -i -t --rm=true centos /bin/bash

or

docker run -i -t --rm centos /bin/bash
like image 70
spectre007 Avatar answered Oct 11 '22 02:10

spectre007


Even though the above still works, the command below makes use of Docker's newer syntax

docker container run -it --rm centos bash
like image 44
ruby_slinger Avatar answered Oct 11 '22 03:10

ruby_slinger


I use the alias dr

alias dr='docker run -it --rm'

That gives you:

dr myimage
ls
...
exit

No more container running.

like image 20
VonC Avatar answered Oct 11 '22 04:10

VonC