As explained in the Basics section one of the great features of Docker is its images system. There are already many images available (for enterprise and community). Having these images available makes it very easy to customize them.
For example we don't have to start from scratch every time, making an image for a Linux distro first and then configuring it. In most cases we can find an image that almost does what we want it to do and install some extra packages that we need or change a configuration file that is needed by one of the applications running in the container.
For some time Ubuntu was the base for most images, recently this started shifting towards Alpine. Both are Linux distro's, so why choose one over the other?
Well when it comes to containers size matters and smaller is better. The Alpine base image from the Docker store is about 5MB, while the Ubuntu base image takes up about 190MB of disk space. Choosing the smaller image means faster building (downloading it is a lot faster) and less wasted disk space.
Images are build from a file that usually has the name Dockerfile
. We can give it a different name, but then we have to specify an option to the docker build command to tell it where to look. To build the image Docker will pull the base image (and recursively the image that image is based on), and execute what we put in the Dockerfile in an intermediate container. When execution is completed it saves the state of the intermediate container as an image to the local repository.
It is recommended to place the Dockerfile
in its own directory.
To go over the entire syntax of the file would be beyond the scope of this article, so instead we will go over a few basics and show you how to make a basic Dockerfile.
For a more complete reference, please visit: https://docs.docker.com/engine/reference/builder/
Adhering to the official documentation we will call the "directives" in the Dockerfile "instructions". A few basic instructions:
src
(path on our file system) and dest
(the destination in the image).The instructions listed above are very common, but of course there are more of them, you can find more details here:
https://docs.docker.com/engine/reference/builder/
The very first step to writing our first Dockerfile is to create a text file with the name "Dockerfile" in a new directory.
The first instruction we need to add to the file is the FROM
instruction. It will tell docker to base our image on the image specified as an argument to the instruction.
FROM alpine:latest
This will use the latest version of the Alpine Linux distro. "latest" is a tag, to see what other tags are available have a look in the docker store:
https://store.docker.com/images/alpine
In most cases tags will be version numbers.
On the next line we will probably want to install some package, we can do that using the RUN instruction in combination with a command specified as an argument. Since we are using Alpine Linux we have to use the package manager for Alpine, which is called apk
.
RUN apk add --no-cache vim
The line above will install vim using the apk package manager.
Next let's copy a text file to the image:
COPY ./textfile /tmp/textfile
The file "textfile" will be copied from the current directory (same as the Dockerfile) and placed it inside the image in the /tmp
directory.
Now let's add a default command to execute when running this container:
ENTRYPOINT ["vim", "/tmp/textfile"]
This instruction will execute vim
with the argument /tmp/textfile when running the container.
So putting this all together we will end up with the following Dockerfile:
FROM alpine:latest
RUN apk add --no-cache vim
COPY ./textfile /tmp/textfile
ENTRYPOINT ["vim", "/tmp/textfile"]
We can build this image with the following command:
docker build --tag=scuti/docker-vim .
Atfer the build process is completed successfully we can run the container with the following command:
docker run -it --rm --name test scuti/docker-vim
When you execute this your terminal will be attached to the console of the container and see that the text file we copied is open in vim.
Now when you quit vim (good luck with that) the container will be stopped and removed, but the image still persists. You can find it by executing:
docker images
Congratulations we just wrote our first Dockerfile and built our first image.