A comprehensive guide to Dockerfiles
In the world of Docker there are images for virtually anything, but chances are you will need to tweak them a bit if you need something more. That’s when Dockerfiles come in, Dockerfiles are used to build Docker Images and you can use them to build literally everything. Learn now how to write a Dockerfile today!
Anatomy of a Dockerfile
FROM ubuntu:18.04 MAINTAINER maintainer name ADD ./local-file /data/container-file RUN apt-get install apache2 RUN apt-get install php7.0 CMD "echo" "I'm not run during build" ENV MYVAR value EXPOSE 80 WORKDIR /var/www VOLUME ["/var/www", "/var/log/apache2", "/etc/apache2"] ENTRYPOINT ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
This is an example of a simple application, it may seem daunting but it really isn’t. Let’s review each line/directive one by one:
- FROM: Docker images are layered, each command creates a new layer, by using the FROM directive you specify a base image upon which to build.
- MAINTAINER: simply the name (and most of the times the email) of the maintainer.
- ADD: the add directive copies the files from a source destination inside the container at build time.
- RUN: the run directive runs a command at build time.
- CMD: like the run directive, but CMD doesn’t run at build time. Instead it runs at run time, when the container is created.
- ENV: this directive is one of the most used, it allows to define local variables just as you would when executing a script.
- EXPOSE: the expose directive is much like a firewall port, it tells that the container “listens” to that port.
- WORKDIR: the active directory where commands will operate. Think of this directory as the “cd” command inside the container to help you visualize what it does.
- VOLUME: volumes are a way for containers to store persistent data, if you don’t know about them I suggest you read here.
- ENTRYPOINT: this directive is the core of a Dockerfile, it is the process that is started when the container is run.
As you can see, breaking down each directive into smaller pieces makes a Dockerfile less daunting. There are a few more directives but you can accomplish almost every setup with the ones outlined above.
Building Dockerfiles into Docker Images
Dockerfiles remain files until you “compile” them into Docker Images. This “compiling procedure” is called building. Building an image is quite easy:
$ docker build CONTEXT
Where context is the path or URL used to build the image. Most of the times you will use the current directory “.” as context, but there are cases where you may use another context. For example, if you wanted to compile the example Dockerfile above you’d do:
$ cd /path/to/folder/containing/dockerfile $ docker build .
Most of the times you will want to tag your images so that you can catalog and use them in an organized fashion. To do so, simply add the -t flag to the build command:
$ docker build -t mytag:myversion .
Now that you have your image tagged you can push it to your own private registry, but that is an argument for another tutorial.
Latest posts by mark (see all)
- 2020 A year in review for Marksei.com - 30 December 2020
- Red Hat pulls the kill switch on CentOS - 16 December 2020
- OpenZFS 2.0 released: unified ZFS for Linux and BSD - 9 December 2020