What are Dockerfile Directives

What are Dockerfile Directives

 What are Dockerfile Directives


In our previous tutorial, you learned how to build images with Dockerfile. This tutorial will help you to understand the basic Dockerfile directives and their uses.

FROM

The from directive is used to set the base image for the subsequent instructions. A Dockerfile must have FROM directive with a valid image name as the first instruction.

Examples:

FROM ubuntu
FROM test/ubuntu-ssh:16.04

LABEL

Using labels you can organize images in a proper way. this is useful to set maintainer address, vendor name, version of image, release date, etc. The line must begin with the keyword “LABEL”.

LABEL maintainer="rahul@souysoeng.com"
LABEL vendor="TecAdmin"
LABEL com.example.version="0.0.1"

You can add multiple labels in a single line with space-separated, or you can define in multiple lines as follows.

LABEL maintainer="rahul@souysoeng.com" vendor="Test" \
com.example.version="0.0.1"

RUN

Using RUN directing, you can run any command to image during build time. For example, you can install required packages during the build of the image.

RUN apt-get update 
RUN apt-get install -y apache2 automake build-essential curl

As a more formatted syntax, you can use it as follows.

RUN apt-get update && apt-get install -y \
    automake \
    build-essential \
    curl \

COPY

The COPY directive is used for copying files and directories from the host system to the image during the build. For example, the first commands will copy all the files from hosts html/ directory /var/www/html image directory. Second command will copy all files with extension .conf to /etc/apache2/sites-available/ directory.

COPY html/* /var/www/html/
COPY *.conf /etc/apache2/sites-available/

WORKDIR

The WORKDIR directive is used to set the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD commands during the build.

WORKDIR /opt

CMD

The CMD directive is used to run the service or software contained by your image, along with any arguments during the launching of the container. CMD uses the following basic syntax

CMD ["executable","param1","param2"]
CMD ["executable","param1","param2"]

For example, to start Apache service during the launch of the container, Use the following command.

CMD ["apachectl", "-D", "FOREGROUND"]

EXPOSE

The EXPOSE directive indicates the ports on which a container will listen for the connections. After that, you can bind the host system port with the container and use them.

EXPOSE 80
EXPOSE 443

ENV

The ENV directive is used to set environment variables for specific services of the container.

ENV PATH=$PATH:/usr/local/pgsql/bin/ \
    PG_MAJOR=9.6.0

VOLUME

The VOLUME directive creates a mount point with the specified name and marks it as holding externally mounted volumes from the native host or other containers.

VOLUME ["/data"]
Reactions

Post a Comment

0 Comments

close