Docker image

  • Create a Dockerfile
  # Always start with baseimage -> ubuntu
  FROM  ubuntu

  RUN  apt update -y
  RUN apt install nginx -y

  VOLUME [ "/var/www/html" ] # Information only

  LABEL version="1" description="This is alpha image"

  RUN  apt install curl -y

  EXPOSE 80 # Information only

  HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 CMD ["sh", "-c", "curl -f localhost || exit 1" ]

  CMD  [ "nginx", "-g", "daemon off;"]

Points to remember

  • Always start with base image (ubuntu : example)

  • Volume : it's just for information, it's actually does not expose volume

  • expose : it's just for information, it's actually does not expose port

  • To expose port and volume must specific in cli

DockerFile reference : https://docs.docker.com/engine/reference/builder/#workdir

Argumentoptioncomment
FROMDownload OS
RUNRun command shell command
WORKDIRSwitch to particular dir and subsequent command will execute in switched dir
EXPOSEInformation
VOLUMEinformation
LABELFor meta information
COPYcopy file to directory
HEALTHCHECKShow status of container
CMDSpecifies the command to be executed when the image starts
ENTRYPOINTSets the default command to be executed when the image starts

CMD VS ENTRYPOINT : https://docs.docker.com/engine/reference/builder/#understand-how-cmd-and-entrypoint-interact

Command to build image


docker build -t username/image_name .

#to push to repo
docker pull username/image

CLI

docker run -p 80:80 -v /home/ak/nginx:/var/www/html -d --name nginx nginx

ENV

In a Dockerfile, you can set environment variables using either $key or ${key} syntax. The ${key} syntax offers additional benefits with Bash modifiers like ${variable:-word} or ${variable:+word}

For example:

  • ${variable:-word}: Expands to the value of variable if it's set; otherwise, expands to word.

  • ${variable:+word}: Expands to word if variable is set; otherwise, expands to an empty string

When using these patterns in CMD or ENTRYPOINT, remember that variable substitution is handled by the command shell, not the Docker builder. Here's a concise example

ENV name=somevalue
ENTRYPOINT ["sh", "-c", "echo hello world"]

In this setup, the ${name} in sh -c is interpreted by the shell during runtime, not during Docker image building. This flexibility allows you to leverage Bash features in your Docker environment