What is Dockerfile and Basics of Dockerfile

Docker gives you the capability to create your own Docker images, and it can be done with the help of Docker Files. A Dockerfile is a text file that contains the necessary commands to create an image. Once a Docker file is created, you can use the docker build command to create an image based on the commands within the file.

Useful Articles:

In this article we will demonstrate you about Docker Files.

Dockerfile Basics

Before creating Dockerfile, you need to understand the necessary commands to create a Dockerfile. Once the Dockerfile created you can use the file to create a Docker image.

ADD: Copies the files or directories from a source and add them into the destination. The destination is the filesystem of the container images

ADD [src] [dest]
## For Example ##
ADD /opt/docker/source_file /opt/docker/destination_file

CMD: Execute a specific command within the container

CMD ["executable","param1","param2"]
CMD executable param1 param2
## For Example ##
CMD ["/bin/ping","localhost"]
OR
CMD ping localhost

ENTRYPOINT: Sets a default application to be used every time a container is created with the image

ENTRYPOINT ["executable", "param1", "param2"]
OR
ENTRYPOINT [command] [param1] [param2]
## For Example ##
ENTRYPOINT ["/bin/ping","-c","3"]
OR
ENTRYPOINT /bin/ping -c 3

ENV: Sets environment variables

ENV [key] [value]  
## For Example ##
ENV PORT 8080

EXPOSE: Associate a specific port to enable networking between the running process inside the container and the outside world

EXPOSE [port]
## For Example ##
ENV PORT 4362

FROM: Defines the base image to use to start the build process

FROM [image_name]
## For Example ##
FROM jenkins

MAINTAINER: Defines a full name and email address of the image creator

MAINTAINER [name]
## For Example ##
MAINTAINER Anuket Jain

RUN: is the central executing directive for Dockerfiles.

RUN [command]
## For Example ##
RUN apt update -y

USER: Sets the user name or UID to use when running the container

USER [username | UID]
## For Example ##
USER anuket

VOLUME: Enable access from the container to a directory on the host machine.

VOLUME [path]
## For Example ##
VOLUME /docker

WORKDIR: Set the path where the command, defined with CMD, is to be executed.

WORKDIR [/path]
## For Example ##
WORKDIR /data

LABEL: allows you to add a label to your docker image.

LABEL [key]=[value] [key]=[value]
## For Example ##
LABEL version="1.0" maintainer="Anuket Jain"

Reference: https://docs.docker.com/engine/reference/builder/

Enjoy it!

No Responses

Leave a Reply

Your email address will not be published.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.