Content Introduction Prerequisites Virtual Host Redirect to HTTPS Conclusion Introduction To secure the data transfer redirecting the HTTP traffic to...Read More
In this tutorial, we recommended methods and best practices for building efficient images.
The Docker builds images automatically by following or reading the instructions from a Dockerfile (here, Dockerfile is a text file that store or contains all the commands, in order, that are needed/required to build a given image).
Dockerfile stick to a specific format and set of instructions. A Docker image consists of read-only layers each of which reflects a Dockerfile instruction. The layers are stacked and each layer is a delta of the changes from the previous layer.
The sample Dockerfile is shown as below:
COPY . /app
RUN make /app
CMD python /app/app.py
Each and every instruction creates a layer:
When the image runs and generates a new container, it adds a new writable layer (known as the “container layer”) on top of the underlying layers. All the changes made to the running container, such as modifying existing files, deleting files, and writing new files are written to this thin writable container layer.
To organize the images by project, you can add labels to your image.
Add a line beginning with LABEL for each label and with one more key-value pairs.
Strings with spaces are quoted or the spaces are escaped. Inner quote characters (“), must also be escaped.
# Set one or more individual labels
LABEL vendor1="ACME Incorporated"
LABEL vendor2=ZENITH\ Incorporated
Split the complex and long RUN statements which are separated on multiple lines with the backslashes for making your Dockerfile more maintainable, understandable, and readable.
It is the most common use-case for RUN in an application of apt-get.
This installs packages. Always combine RUN apt-get update with apt-get install in the same RUN statement.
RUN apt-get update && apt-get install -y \
&& rm -rf /var/lib/apt/lists/*
COPY requirements.txt /tmp/
RUN pip install --requirement /tmp/requirements.txt
COPY . /tmp/
ADD https://example.com/big.tar.xz /usr/src/things/
RUN tar -xJf /usr/src/things/big.tar.xz -C /usr/src/things
RUN make -C /usr/src/things all
Below are some Official Images which have exemplary Dockerfiles: