In this article I will show you how you can move your very first steps in the container world using Docker. Docker has many features that can help resolve different problems, today it is mostly used for local development as an alternative to virtualization, but as time goes Docker is approaching to the enterprise world.
If you have no clue of what I’m talking about or you stumbled here not knowing what are containers or what is Docker, read this post then come back here. I’ll assume you are on a Linux machine and have already installed Docker. If you do want to use Docker under Windows/Mac I highly suggest you to run a Linux VM with Docker installed.
Everything starts from an image in the virtualization world, containerization is the same. One of the greatest features of Docker is the registry: a repository for images. Since we’re just starting we won’t create an image from scratch, rather we will use one from the registry. Ok, but which image? That’s the first thing we ought to know, there are two ways to find images:
- Through Docker Hub website.
- Using the docker search command.
I suggest you to use the latter, it is way faster to do everything in the CLI. This way you can search for images, the next step is importing that image in your current machine. Images are untouchable and are the basis for containers. To get an image you should just issue the docker pull command. For example if you wanted to pull Ubuntu issue docker pull ubuntu if you wish you can also select a precise version e.g. docker pull ubuntu:latest . These commands may require superuser privileges. Now that you have pulled an image you can list the available images on your current machines with:
$ docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE ubuntu latest 91e54dfb1179 4 weeks ago 188.3 MB
If you ever feel to remove an image from your local machine you have to use the docker rmi command.
Now that you know how to operate images it’s time to get to the juice of Docker: running containers. It all comes down to some simple commands: first docker run which starts the container. Important: containers stay alive as long as there is at least one active process running. With that in mind let’s launch our first container:
$ docker run ubuntu echo 'Hello, world!' Hello, world!
But once the command has ended? What happens to the container? Response is given with the docker ps command. Gone right? Let’s try docker ps -a.
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES $docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 93148e75ea5c ubuntu "echo 'Hello, world!'" About a minute ago Exited (0) About a minute ago stupefied_panini
docker ps is similar to the ps command, however it shows informations about containers. Notice the field CONTAINER ID, that ID represents the changes made to the original image! So if you want to refer to the image Ubuntu after using a the echo command you should use that ID (in my case 93148e75ea5c). Now suppose you want to show the standard output of a container. That can be easily achieved by:
$ docker logs 93148e75ea5c Hello, world!
Notice I used the CONTAINER ID, however I could’ve used the NAME of the container. Since I didn’t provide a name, Docker assigned a funny name when I created the container. That name is stupefied_panini.
Now you have a rough idea of Docker basics commands, you know how to search and pull images from the Docker Hub, how to list and remove local images, how to start and monitor basics containers.