Tuesday, August 30, 2022

Dockerfile Commands

FROM <Base Image>

RUN  <To install software/packages>

ENV  <Pass env variable>

ARG <Its will  used image creation time>

FROM alpine
ARG TARGETPLATFORM
RUN echo "I'm building for $TARGETPLATFORM"
FROM ubuntu
ARG CONT_IMG_VER
ENV CONT_IMG_VER=$CONT_IMG_VER
RUN echo $CONT_IMG_VER

USER <Will switch user>

WORKDIR <Ir set working directory>

COPY  <copy files from host to container>

ADD  <copy remote files and extract it>

VOLUME /myvol  <mount volume>

ENTRYPOINT 

CMD

Dockerfile reference | Docker Documentation


Docker has a default entrypoint which is /bin/sh -c but does not have a default command.

The ENTRYPOINT specifies a command that will always be executed when the container starts.

The CMD specifies arguments that will be fed to the ENTRYPOINT.


If you want to make an image dedicated to a specific command you will use ENTRYPOINT ["/path/dedicated_command"]

Otherwise, if you want to make an image for general purpose, you can leave ENTRYPOINT unspecified and use CMD ["/path/dedicated_command"] as you will be able to override the setting by supplying arguments to docker run.


For example, if your Dockerfile is:

FROM debian:wheezy
ENTRYPOINT ["/bin/ping"]
CMD ["localhost"]

The ENTRYPOINT specifies a command that will always be executed when the container starts. 
The CMD specifies arguments that will be fed to the ENTRYPOINT


FROM debian:wheezy
CMD ["/bin/ping", "localhost"]



The ENTRYPOINT is the program that will be run, and the value passed to the container will be appended to it.


The ENTRYPOINT can be overridden by specifying an --entrypoint flag, followed by the new entry point you want to use.

====================

CMD defines default commands and/or parameters for a container. CMD is an instruction that is best to use if you need a default command which users can easily override. If a Dockerfile has multiple CMDs, it only applies the instructions from the last one.


ENTRYPOINT is preferred when you want to define a container with a specific executable.

You cannot override an ENTRYPOINT when starting a container unless you add the --entrypoint flag.


CMD Docker file


  FROM centos:8.1.1911
  CMD ["echo", "Hello Docker"]

Run result

$ sudo docker run <image-id>
Hello Docker
$ sudo docker run <image-id> hostname   # hostname is exec to override CMD
244be5006f32

ENTRYPOINT

Docker file


  FROM centos:8.1.1911
  ENTRYPOINT ["echo", "Hello Docker"]

Run result

$ sudo docker run <image-id>
Hello Docker
$ sudo docker run <image-id> hostname   # hostname as parameter to exec
Hello Docker hostname

  1. There are many situations in which combining CMD and ENTRYPOINT would be the best solution for your Docker container. In such cases, the executable is defined with ENTRYPOINT, while CMD specifies the default parameter.

Docker file

  FROM centos:8.1.1911

  ENTRYPOINT ["echo", "Hello"]
  CMD ["Docker"]

Run result

$ sudo docker run <image-id>
Hello Docker
$ sudo docker run <image-id> Ben
Hello Ben

No comments:

Post a Comment

Sample Game App Deployment on EKS cluster

 https://padmakshi.medium.com/setting-up-an-eks-cluster-and-deploying-a-game-application-a-step-by-step-guide-08790e0be117