Dockerfile Part-2

EXPOSE:
EXPOSE instruction informs docker that the container listens on specifies network ports at runtime. We can specify the port listens on TCP/UDP. By default it will take TCP.

Publishing the container can be done while running the containers. we will use -p flag on docker run to publish and map one or more ports.

EXPOSE 80

ENV:
The ENV instruction set the environment varoable <key> to the value <value>. These values will be in environment for all subsequent instructions in the build stage.

ENV JDBC_USERNAME="admin" JDBC_PASSWORD="admin"

COPY:
COPY instruction copies files or directories from <src> and adds them to the filesystem of the container <dest> path.

COPY test.txt /opt/test/
COPY test /opt/test/

ADD:
ADD instruction copies files, directories, archives or remote URLS from <src> and adds them to the filesystem of image at <dest> path.  <src> may contains wildcards and matching pattern.

ADD test.txt /opt/test
ADD test  /opt/test
ADD test.tar /opt/
ADD https://raw.githubusercontent.com/daticahealth/java-war-deploy-example/master/README.md /opt/test

COPY vs ADD:
Copy can only copy files from src to dest in local file system.
ADD can do more than copy files from local file system to docker file system. ADD can copy files from remote URLs and also it can copy archive data to file system in docker image.

ENTRYPOINT:
The entrypoint is an default executable for an image. i.e when a container spins up, it runs the executable configured by ENTRYPOINT.

ENTRYPOINT ["java","-jar","helloworld.jar"]

RUN vs CMD vs ENTRYPOINT:
We will use RUN instruction to build your image by adding layers on top of initial image.

We will use CMD instruction if we need to provide a default command and/or args that can be overwritten from CLI when docker container run.

We prefer ENTRYPOINT to CMD when building executable Docker images and we need a command always to be executed. Additionally we use CMD instruction if we need to provide extra default args that could be overwritten from CLI when docker container runs.

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

Using VOLUME we can have mount point from host machine to docker container to store any important data.

VOLUME [“/data”]

USER:
USER instruction sets the username (or UUID) and optionally the user group (or gid) to use when running the image and for any RUN, CMD and ENTRYPOINT instructions that follows in Dockerfile

USER vagrant


WORKDIR:
WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow in Dockerfile.

If WORKDIR is not created/exists already it will create directory structure.
WORKDIR /opt/app


ONBUILD:
ONBUILD instruction adds to the image a trigger instruction to be executed at a later time, when the image is used as the base image for another build.

The trigger will be executed in the context of downstream build, as if it had been inserted immediately after the FROM instruction in the downstream Dockerfile.
ONBUILD ADD . /opt/app
ONBUILD RUN npm install


HEALTHCHECK:
HEALTHCHECK instruction tells docker how to test a container to check  that it is still working. This can detect cases such as web server that is stuck infinite loop and unable to handle new connections even though the server process is still running.

HEALTHCHECK –interval=5m –timeout=5s CMD curl –f http://localhost/ || exit 1
To disable any healthchecks inherited from base image.
HEALTHCHECK NONE

<< Previous


No comments:

Post a Comment