Getting started with Docker
This is a basic, straight forward, introduction to Docker. Docker is a container management tool. You can create containers based from images, an image is just a saved state of a container, or more properly it’s a snapshot of a container. There are a lot of images (snapshots) shared on the internet in places called registries, the official docker registry is located at Docker Hub but there are other registries like Bitnami, Quay.io and you can even use Gitlab as one of them, but the usage of these is for more specific tasks.
To get started we need to instantiate a container and for this we need to
specify the base image to use. If we have just installed docker, chances are
we don’t have any image on our computer (memes doesn’t count here xD … trt
tss) so let’s pull (download) alpine
which is one of the most lightweight
images out there and is based on the Alpine Linux distribution.
$ docker pull alpine
This effectively will download the alpine
image, we can verify this by
listing our available images:
$ docker images
We should see alpine listed there. At this point we can run a container based from that image:
$ docker run -it alpine sh
Ok, let’s explain here. This is running the sh
program (/bin/sh
) on a
newly created alpine
container. It is necessary to specify the -t
and -i
flag so we can attach our terminal to an interactive session in the
container. Without that it will just run sh
and exit.
Note that it isn’t strictly necessary to actually pull
the image before
running it, when we try to run a container from an image that doesn’t exist on
the computer but it does in the official docker registry, it will pull the
image for us.
When you’ve finished playing with the container type exit
or press Ctrl+d
,
at this stage you’ll have a stopped container. The way you see running
containers is with the command docker ps
, and to see all containers (even
stopped ones) you use the -a
flag.
$ docker ps -a # shows stopped containers also
At this point if you run
the container again you’ll be creating a new
container instead! So if you have a stopped container simply start
it.
$ docker start <container id>
The container ID is a hash that looks like fe0c150746ef
but you can also use
the name of the container (it’s the last column in the output of docker ps -a
).
Now to attach again to the container (remember you can’t use run
because
that will create a new container) we use the exec
command passing as an
argument the name of the binary to execute in the container, like so:
$ docker exec -ti <container id or name> sh
So here, we’re again running the program /bin/sh
on the container matching
the given container ID
.
Give me moar!
Ok, to destroy a container we use the rm (remove) command:
$ docker rm <container id>
If you have a lot of stopped containers (tends to happen often) you can remove all containers with:
$ docker rm $(docker ps -aq)
Using the -q
flag will return the container IDs that you can pass to the
rm
command. If you want to remove only stopped containers use:
$ docker rm $(docker ps -q -f "status=exited")
This filters (-f
) only containers with the exited
status
.
If you want to name your containers so you don’t have to use the container ID or the random name that docker gives to them you can do it this way:
$ docker rename <container id or name> new_name
Also note that you can pass the name when instantiating a container like this:
$ docker run --name demo_alpine -ti alpine /bin/sh
Finally, suppose you don’t longer want the ability to create containers based from an alpine image, so you can remove the image with:
$ docker rmi alpine
You can verify with docker images
that alpine
is not longer there, and by
the way, you can also list images like this:
$ docker image ls
Ok I think this is enough to get started with docker, the next time we’ll see more interesting docker commands and how to create and publish our own images.
And yes, better get used to use a lot of flags in docker commands.