Options for Executing Docker Containers – All about “docker run”
In a previous post, I showed you how to build a powershell docker image from the powershell github repo using the included powershell Dockerfile. Before we jump into more complicated things, let’s first focus on some of the basic options for executing containers:
Before getting started let’s make sure that there aren’t any local containers running on our machine so we can see what happens. We can find out if anything is running on our machine by executing “docker ps”.
Executing your first container
The most simple way of executing a container in this case executing a “docker run ubuntu” doesn’t appear to do much other than download a bunch of files:
Under the covers docker has executed a ubuntu container and exited. That’s because the docker command expects the following syntax:
docker run [options] [image] [command] [args]
In this case, because we didn’t specify a command or any args, the container exited. To prove that, we can execute a ps command to show all containers (not just the ones running) with the following command docker ps -aq
Here you’ll notice that Container ID b48a7… exited successfully (o) 3 minutes ago and it has a Name prickly_shirley. Quick sidebar on container names. You have the ability to name a container whatever you’d like when you execute it assuming that a container with that name doesn’t already exist with the –name flag. In the event that a container doesn’t have a name docker randomly creates one with the format <adjective>_<scientist>.
Passing a command to a container:
Let’s expand on this just a bit more, by executing a ubuntu container that simply echos some output. This time we’ll execute the following:
docker run ubuntu echo “Howdy! My Ubuntu container threw this output your way.”
Getting a terminal with your container:
Now that we’ve used a container to echo some output let’s use a couple of extra flags to connect to our the container’s STDIN (the -i flag) and get a pseudo-terminal (the -t flag). Do that by executing a docker run -it ubuntu
Okay, so now we have a bit of a problem. You can see that we have a container running and we’re on the terminal of that container. Like most shell prompts a CTRL-C will exit the terminal and the container will stop. But with docker there’s also a handy way to keep the container running by detaching.
Detaching from a docker container:
Assuming that you’ve executed your Docker container with a “docker run -it” you can disconnect from your running container with a ^P^Q or CTRL+P, CTRL+Q.
You can find your container ID of your running container by looking at a “docker ps”. Here you see my ubuntu container has a container ID of 952f…
Re-Attach to your container
To re-attach to the ubuntu container execute a “docker attach <container id>”
A quick note here, you don’t need to type in all of the container ID just the number of digits that you need to show that the container is unique.
Running a container in detached mode
To start a container in detached mode leverage the -d flag. Here we’ll start a container to ping google and attach to it to see it working in the background.
Also please remember that because you’ve attached to the container you’ll need to then use a CTRL+P CTRL+Q to get out. But wait! This only works for containers executed with a -it so now what?
Using EXEC to jump into running containers:
Using the exec command allows us to enter a terminal on a container that’s already running in detached mode and detach easily with a CTRL+P, CTRL+Q. You’ also notice that below my ping container is still running after I disconnect. This is perfect for having a look around inside of a running container if you need to.
So there are tons of other things to consider that we’ll get to in later posts but for now, we’ve covered a bit of ground on our containerization journey. Next time, we’ll talk about how to clean up after our containers and images.