Once you get up to speed with the CLI you should also get to know the Dockerfile. One Dockerfile defines one Docker image that will be loaded into your container at run time. Don't forget, in the Docker style guide there are both Dockerfile requirements and some Dockerfile practices that you must follow - you might as well do it from the beginning. Actually there are quite a few published practices for writing Dockerfiles:
Important to know about Dockerfiles is that, you can not only build an image in a Dockerfile, you can also do things like set environment variables and specify commands to run from the the bash console of running containers. Each new instruction in the Dockerfile creates a layer - which is a little like a code check in for a versioning system and is explained here. This is one of the ways that Docker can stay efficient. To keep Docker images lean and mean, it is recommended that you concatenate your instructions or insert them into a shell script and run the script from Dockerfile. This reduces the number of layers created by your Docker images.
Achtung: There are two types of command directives in a Dockerfile and, in the Haufe docker landscape, it is important for you to understand how to use them. They are:
Shall be used to start any executables within a container. For example, if your container uses MongoDB as a database, you must start the MongoDB with ENTRYPOINT in your dockerfile: otherwise MongoDB will not be available.
Shall be used to run commands within the container that are likely to change. You can find the Docker practice for this in our style guide.
Docker Docs also has a Dockerfile Reference that contains all of the Dockerfile instructions. It makes sense to get acquainted with the following basic instructions:
|FROM||Defines the base image you shall use for your Docker image.|
|COPY/ADD||Adds files from a specific location to your image.|
|RUN||Runs commands on top of your image, creating a new image layer on top of the existing image|
|WORKDIR||Specifies the start directory in your image|
|ENV||Sets environment variable in your image|
|USER||After image construction is finished you must switch user. It is required that containers default user is NOT "root".|
|LABEL||Data label your image to provide helpful additional information about it.|
Just like with Docker CLI commands there are many more Dockerfile instructions in the Dockerfile Reference that will prove useful.
Achtung: Data labeling your images with metadata is very important for image discovery and description purposes, and also to organize docker images in the Haufe Group Docker Registry. In my short research, it looks the only way to easily add labels to an image is to use the LABEL instruction in your Dockerfile. You may want to make use of this proposed labeling schema to provide good, semantic data labeling to your images and containers.
The Docker Whalesay tutorial is a good basic tutorial to understand how to create images with the CLI and to build your own image using Dockerfile. Go ahead and do this tutorial. While you are doing this one, also note that at this stage you will always need to run an image to create a Docker container that actually does something.
Achtung: On Windows (With Docker for Windows), I have also encountered some problems at create image time when extending base images by using package managers like “apt” on Linux from a Dockerfile. The workaround for this was to add build arguments that set the proxy server with the Docker Builder. These are environment variables that are only set at image build time. In Docker you can do this two ways.
- Use the -–build-arg option with the “docker build” command
- Use the ARG instruction in a Dockerfile
These arguments are only passed to the builder and do not persist in image or running container.
There are also many more tutorials that show you how to dockerize applications and also how to perform tasks with Docker and Dockerfile. These tuts are basically to develop your Docker skills, but with a little work you may be able to reuse the these images for your own purposes. Here is a list:
|MongoDB||Learn how to dockerize MongoDB, push your image to your DockerHub account and share|
|PostgreSQL||Learn how to dockerize PostgreSQL, learn how to use container volumes|
|SSH service||Learn how to set environment variables in Dockerfile (and propagate further), review removing containers and images|
Dockerfile cheat sheet
Some more good tips on what you can do with Dockerfiles are here
- Understand Dockerfile - Instructions like VOLUME, WORKDIR, ADD, COPY, and LABEL
- Know Haufe Group practices for Dockerfile
- Build your own images with Dockerfile.
- Understand how to develop locally and add these changes to your Docker image by building a Dockerfile