DEV Community

Cover image for docker learn #04: Hanging Out with Ubuntu
Alex Ortiz
Alex Ortiz

Posted on • Updated on

docker learn #04: Hanging Out with Ubuntu

Sometimes, the best way to practice is to, well, practice. For this week's learning snapshot, I'm going to share what it's like to use a few basic commands in Docker. Let's use Ubuntu, one of the most popular Linux distributions out there, to very temporarily run a container. And let's see how to check that we've done so.

Hanging Out with Ubuntu

Ubuntu is an operating system in the Debian family, as opposed to, for instance, Fedora. For practice, let's use Docker to run a Linux container on the Ubuntu operating system.

Say you run this command in Docker for the first time from your Docker client:

docker run ubuntu

Your client will pass the run instruction to the docker daemon (the server that hosts the Docker Engine, which is the runtime on which containers run).

First, the daemon will try to find the ubuntu image locally: it will check to see if the image is already on the server itself. If the daemon doesn't find the image—and it won't in this case, because this is the first time you've tried to run an ubuntu container—then it will display a series of messages on your Docker command line interface (CLI) as it does each of the following:

  • display a message confirming it did not find the image locally
  • pull the image from the Docker registry where the image is stored, defaulting to the image with the latest tag
  • show statuses, one per line, as it downloads each of the image layers associated with the ubuntu image—in this case, the ubuntu:latest image had four layers. As each layer succeeded, the CLI displayed a message that contained a short version of the SHA256 hash for that layer, along with the words "Pull complete"
  • display the full SHA256 digest for the ubuntu image in question
  • show a confirmation message, Status: Downloaded newer image for ubuntu:latest

So for example:

Example of Docker CLI Messages on Docker RunFrom Docker Playground

Great! So now we have our Ubuntu image on our Docker host, and we've actually run a Linux container based on Ubuntu. You can verify this by typing docker ps.

Uh-oh: Where's that Container?

But wait! You won't see anything listed. As I mentioned in my last post, certain containers, such as containers based on base operating system images, are exited almost immediately after they run. This is because if a container doesn't have a running process (the whole point of a container), it will be exited. And in the case of an Ubuntu container that's not doing anything special, its Bash shell will run for a split second, then not see any script to run in that shell, and therefore exit. Then the container will have nothing to do, so the daemon will stop the container.

(Sidebar: If you're wondering what I'm on about, remember that every docker image is created from a Dockerfile, which has one or more lines of instructions in it. Each line in a Dockerfile is used to create one layer, and all those layers collectively make up the actual docker image. From those four status messages I mentioned up above, we can deduce that the Dockerfile used to create the particular Ubuntu image we are now using had four lines, and thus four layers, in total—one of which is for the command /bin/bash. See below.)

Ah, There it Is

You can confirm that all this happened by running the command docker ps -a, whereupon your CLI will show you the following information:

  • a Container ID
  • the docker image the container was based on
  • a command if one was executed in the container ("/bin/bash", in this case)
  • when said container was created
  • the Status of the container, along with an exit code (like "0", which means "no errors"
  • a randomly generated but usually whimsical Name for the container (e.g., "youthful_galileo")
  • some other etceteras

So for example:

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
blahblahblah        ubuntu              "/bin/bash"         21 minutes ago      Exited (0) 21 minutes ago                       youthful_galileo

And there you have it. We've met our goals of running an Ubuntu Linux container using Docker and checking that we indeed ran it.

From here, there's tons more that we could do:

  • Use docker start <containername> to restart the exited container
  • Use docker inspect <containername> to get loads of detailed information about the container
  • Use docker rm <containername> to remove the container from the host
  • Use docker rmi ubuntu to remove the Ubuntu image itself from the host, in preparation for other cool commands

We might use docker pull ubuntu to pull the Ubuntu image to our daemon, but not create a container from it just yet. Or we could use docker create ubuntu to both pull the Ubuntu image and create a container but not yet run it. Or we could go all in with docker create --name="cool_fridaynight" ubuntu to do everything we just did plus give the container a cool custom name. 😎

But alas, we're out of time this week. Till next time!


docker shoutout

There's a joy to learning new things, and I hope you're enjoying these writings. So if you like my series, hit the follow button and share the link with one other person. Show some <3 or a 🦄. And follow my Educative team here on Dev.to. Speaking of which, I'd like to plug a very special announcement:

💪🏾🎓

See you next week! Happy learning.

Top comments (0)