Understanding Docker Images.

Understanding Docker Images.

ยท

4 min read

In my last post of this series, we discussed what happens behind the scenes in containers, created them manually (without using Docker), and realized how miserably long it would take for us to create just a bare-minimum of what could be called a containerized environment, let alone other issues like network layer and managing it, etc. My hope is that exercise made you a bit more considerate towards Docker ๐Ÿ™‚

Now that we can imagine how immensely helpful Docker can be, going forward in this series, I'll explore some very helpful tools that can work with/comes with Docker, and can make our lives as developers, further easier. One such great resource are the tens of thousands of Docker images provided to us on Docker hub . Let's see how we can utilize them!

Wha...aa... Docker images, did you say?

docker-meme.png

I think Buzz is funny, who is with me? ๐Ÿ˜ƒ Anyway, let's not get distracted here.

There are many definitions for Docker images on the internet, but one way of understanding I find particularly helpful is that these are pre-built containers, that are readily available on Docker hub to just grab and run. More technically, they are an immutable file system, created using the build command and every time we run one of these images, it creates an instance of that type of container, for example say, Ubuntu, Alpine, Node to name a few.

Let's try to create an ubuntu:bionic container image and see this in action.

$ docker run --interactive --tty ubuntu:bionic # or: docker run -it ubuntu:bionic

It's that simple! ๐Ÿ’ƒ๐Ÿฝ

The above command would run aka execute an existing container image (as opposed to building anew) and drop you in a shell inside of the container as the root user of that container. When you're done, just run exit or hit CTRL+D.

If you tried the same command without the --interactive --tty or -it for short, you'd not see much happening. This is because -it tells Docker that you'd like to be dropped into the container interactively so you can run commands and inspect it. When you leave this flag out, the container starts and then because it has nothing to do, it just exists.

If there is exactly one command you'd like to execute, you could also do:

$ docker run ubuntu:bionic <CMD> # replace <CMD> with desired command like ls, cat, etc.

Some useful commands to note:

Let's look through a few more foundational docker commands:

1.detach : Let's you run a container in the background. You can use it as:

$ docker run --detach -it ubuntu:bionic # or: docker run -dit ubuntu:bionic

If you wanted to get a hold of it back on the foreground, we use another command called,

2.ps : This will print out all the running containers that Docker is managing for you, along with their PIDs and names.

$ docker ps

From the list we get back, go ahead and copy the one which you want on the foreground and run the next command,

3.attach : This allows you to attach a shell to a running container and lets you play around with it.

$ docker attach <PID or container_name>

After you are done with the container and want to get rid of it, we can use this next command,

4.kill : You guessed it, it kill s (๐Ÿ˜ˆ ) the container.

$ docker kill <IDs or names of containers>
$ docker kill $(docker ps -q) # to kill all containers running at any given time.

And now, I'd like to point to two other flags, I think would help simplify things and keep the number of containers on your system under check.

Honorary mentions ๐Ÿ‘๐Ÿผ

1.--name : You can name your own container! Isn't that exciting now. ๐Ÿค“

$ docker run -dit --name worlds-best-container ubuntu:bionic

Congrats! Now you can refer to your container as the worlds-best-container , undisputedly.

2.Now say, we went ahead and ran these commands,

$ docker kill worlds-best-container 
$ docker ps --all

What!? worlds-best-container still exists even after you ran the kill command on it. It's invincible! ๐Ÿคฏ

Nah, this is just because Docker keeps some metadata from the containers you have run until you tell it to stop doing that. So go ahead and use --rm like so to achieve just that.

$ docker rm worlds-best-container # or for any future containers run to automatically clean up: docker run --rm -dit --name <NAME> ubuntu:bionic

Conclusion

Yay! With all that you now know how to get an image from Docker hub and the basics of managing it from your terminal. In the next article of this series, I'll be going over some more cool features of the Docker CLI.

Till then, happy coding!