Now that we have everything setup, it's time to get our hands dirty. In this section, we are going to run a Busybox
container on our system and get a taste of the docker run command.
To get started, let's run the following in our terminal:
docker pull busybox
Using default tag: latest
latest: Pulling from library/busybox
ec562eabd705: Pull complete
Digest: sha256:5eef5ed34e1e1ff0a4ae850395cbf665c4de6b4b83a32a0bc7bcb998e24e7bbb
Status: Downloaded newer image for busybox:latest
docker.io/library/busybox:latest
The pull command fetches the busybox image from the Docker registry and saves it to our system.
Now, try pulling another image named alpine
.
docker pull alpine
Using default tag: latest
latest: Pulling from library/alpine
d25f557d7f31: Pull complete
Digest: sha256:77726ef6b57ddf65bb551896826ec38bc3e53f75cdde31354fbffb4f25238ebd
Status: Downloaded newer image for alpine:latest
docker.io/library/alpine:latest
You can use the docker images command to see a list of all images on your system.
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest f8c20f8bbcb6 5 months ago 7.38MB
busybox latest 65ad0d468eb1 12 months ago 4.26MB
Great! Let's now run a Docker container based on this image. To do that we are going to use the almighty docker run
command.
docker run busybox
$
Wait, nothing happened! Is that a bug? Well, no. Behind the scenes, a lot of stuff happened. When you call run
, the Docker client finds the image (busybox in this case), loads up the container and then runs a command in that container. When we run docker run busybox
, we didn't provide a command, so the container booted up, ran an empty command and then exited. Well, yeah - kind of a bummer. Let's try something more exciting.
docker run busybox echo "hello from busybox"
hello from busybox
Nice - finally we see some output. In this case, the Docker client dutifully ran the echo
command in our busybox container and then exited it. If you've noticed, all of that happened pretty quickly. Imagine booting up a virtual machine, running a command and then killing it. Now you know why they say containers are fast! Ok, now it's time to see the docker ps
command. The docker ps
command shows you all containers that are currently running.
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
As you can see, there are no running containers. We see a blank line. Let's try a more useful variant: docker ps -a
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8dcd96ac52d8 busybox "echo hello from busybox" 6 seconds ago Exited (0) 5 seconds ago priceless_yalow
c899304a9faf busybox "sh" 22 seconds ago Exited (0) 20 seconds ago fervent_haibt
14e5bd11d164 hello-world "/hello" 2 minutes ago Exited (0) 2 minutes ago thirsty_euclid
As you can see, there are three containers that have exited. The first two are the ones we created earlier. The third one is the hello-world container that we created earlier.
So what we see above is a list of all containers that we ran. Do notice that the STATUS
column shows that these containers exited a few minutes ago.
You're probably wondering if there is a way to run more than just one command in a container. Let's try that now:
docker run -it busybox sh
/ # ls
bin dev etc home proc root sys tmp usr var
/ # uptime
05:45:21 up 5:58, 0 users, load average: 0.00, 0.01, 0.04
Running the run
command with the -it
flags attaches us to an interactive tty in the container. Now we can run as many commands in the container as we want. Take some time to run your favorite commands.
That concludes a basics of the docker run
command, which would most likely be the command you'll use most often. It makes sense to spend some time getting comfortable with it. To find out more about run
, use docker run --help
to see a list of all flags it supports. As we proceed further, we'll see a few more variants of docker run
.
Now that we've seen how to create containers, let's see how to delete them.
We saw above that we can still see remnants of the container even after we've exited by running docker ps -a
. Throughout this tutorial, you'll run docker run
multiple times and leaving stray containers will eat up disk space. Hence, as a rule of thumb, I clean up containers once I'm done with them. To do that, you can run the docker rm
command. Just copy the container IDs from above and paste them alongside the command.
docker rm 8dcd96ac52d8
8dcd96ac52d8
On deletion, you should see the IDs echoed back to you. If you have a bunch of containers to delete in one go, copy-pasting IDs can be tedious. In that case, you can simply run -
docker rm $(docker ps -a -q -f status=exited)
This command deletes all containers that have a status of exited. In case you're wondering, the -q
flag, only returns the numeric IDs and -f
filters output based on conditions provided. One last thing that'll be useful is the --rm
flag that can be passed to docker run
which automatically deletes the container once it's exited from. For one off docker runs, --rm
flag is very useful.
docker run --rm busybox echo "Hello from busybox again"
Hello from busybox again
Now lets check docker process using docker ps -a
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
As you can see there is no remants of the container, docker automatically deleted the container once it's exited from.
We can also use docker container prune
command can be used to achieve the same effect.
docker run --rm busybox echo "Hello from busybox again for testing docker dontainer prune"
Hello from busybox again for testing docker dontainer prune
Now run docker container prune
command.
docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
900abe321309d9a17fb277e49d63e82ff689de744aa8cf51ff300e624fac2bc9
Total reclaimed space: 0B
Lastly, you can also delete images that you no longer need by running docker rmi
.
docker rmi busybox
Untagged: busybox:latest
Untagged: busybox@sha256:5eef5ed34e1e1ff0a4ae850395cbf665c4de6b4b83a32a0bc7bcb998e24e7bbb
Deleted: sha256:65ad0d468eb1c558bf7f4e64e790f586e9eda649ee9f130cd0e835b292bbc5ac
Deleted: sha256:d51af96cf93e225825efd484ea457f867cb2b967f7415b9a3b7e65a2f803838a
Now if to run docker images
, you will not find busybox
image no more.
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest f8c20f8bbcb6 5 months ago 7.38MB
In the previous section, we have seen how to run a container in the foreground. But in most cases, you will want to run a container in the background. To do this, you can use the -d
flag.
Now, Let us run busybox
container in background and keep it running.
docker run -d -it busybox
a5266ecae48b4985bca87e3733a1ce5fa369ce8f5fdea3b43c2fbdbb61b97549
Now lets check busybox container exists and running.
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a5266ecae48b busybox "sh" 43 seconds ago Up 42 seconds festive_buck
Now, Let us run some commands on just created busybox
container.
docker exec a5266ecae48b echo "hello from daemonized busybox"
hello from daemonized busybox
Now, try runnning some of these commands inside busybox
container like ls, uptime, date, whoami, hostname
.
To stop a running container, you can use the docker stop
command. This command sends a SIGTERM signal to the container, which gives the container a chance to gracefully shutdown. If the container does not shutdown within a certain amount of time, the SIGKILL signal is sent to force the container to stop.
Let us stop busybox
container.
docker stop a5266ecae48b
a5266ecae48b
Now, check if busybox
container is running or not.
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a5266ecae48b busybox "sh" 3 minutes ago Exited (137) About a minute ago festive_buck
Do notice that the STATUS
column shows that busybox
container exited a few minutes ago.
To start a stopped container, you can use the docker start
command. This command starts the container in the same state as it was when it was stopped.
Let us start busybox
container.
docker start a5266ecae48b
a5266ecae48b
Now, check if busybox
container is running or not.
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a5266ecae48b busybox "sh" 23 minutes ago Up 37 seconds festive_buck
To restart a container, you can use the docker restart
command. This command stops the container and then starts it again.
Let us restart busybox
container.
docker restart a5266ecae48b
a5266ecae48b
Now, check if busybox
container is running or not.
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a5266ecae48b busybox "sh" 32 minutes ago Up About a minute festive_buck
You can name a container using the docker run
command. This command creates a new container and names it using the name you specify.
Let us name busybox
container as busybox-container
.
docker run -it --name busybox-container busybox sh
5de293fa722d
Now, check if busybox-container
is running or not.
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5de293fa722d busybox "sh" 7 seconds ago Up 5 seconds busybox-container
Do notice that the NAMES
column shows that our container is named busybox-container
that we gave during docker run
command. If new do not provide --name <container name>
then docker will automatically generate for us like festive_buck
on our earler docker container.