21 Days of Docker-Day 5-Introduction to Dockerfile

Prashant Lakhera
3 min readOct 11, 2019

--

Before we dig deeper into Docker Images, just want to clear one point that every docker container is based on an image. We can create our own image by reading instruction from Dockerfile or we can pull an image either from Docker Hub or private Docker registry.

A Dockerfile is a text document that contains all the commands/set of instructions a user could call on the command line to create an image. These instructions are called directives.

Format of Dockerfile

Here is the format of the Dockerfile:

# Comment
INSTRUCTION arguments

Some list of instructions in the Dockerfile

  • ADD: The ADD instruction copies new files, directories or remote file URLs from <src> and adds them to the filesystem of the image at the path <dest>
  • COPY: The COPY instruction copies new files or directories from <src> and adds them to the filesystem of the container at the path <dest>.
  • ENV
  • EXPOSE : Documents which port(s) are intended to published when running a container
  • FROM
  • LABEL
  • STOPSIGNAL
  • USER
  • VOLUME
  • WORKDIR : The WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile
  • STOPSIGNAL: The STOPSIGNAL instruction sets the system call signal that will be sent to the container to exit. This signal can be a valid unsigned number that matches a position in the kernel’s syscall table, for instance, 9, or a signal name in the format SIGNAME, for instance SIGKILL.
  • HEALTHCHECK: The HEALTHCHECK instruction tells Docker how to test a container to check that it is still working. This can detect cases such as a web server that is stuck in an infinite loop and unable to handle new connections, even though the server process is still running

A simple Dockerfile will look like this

* FROM: A Dockerfile must start with a FROM instruction. The FROM instruction specifies the Base Image from which you are building.
* RUN: The RUN instruction will execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the Dockerfile
* CMD: The main purpose of a CMD is to provide defaults for an executing container
  • To build the image out from Dockerfile we can use docker build command
* Step1: It download the ubuntu image
* Step2: It pull all the latest update
* Step3: Install the nginx binary
* Step4: Start the nginx daemon
  • To verify the new Nginx image, we just built
$ docker imagesREPOSITORY          TAG                 IMAGE ID            CREATED             SIZEmynginx             latest              43e91e210b23        4 minutes ago       152MB
  • To run the container
$ docker run -d -p 80:80 mynginx7f3d9fb752632f5acaee04b8ce18fca0e2b3da2dc5615a62253b9beae002436a[cloud_user@plakhera12c dockerfie]$ docker psCONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES7f3d9fb75263        mynginx             "nginx -g 'daemon of…"   3 seconds ago       Up 1 second         0.0.0.0:80->80/tcp   angry_ptolemy
  • To test it
$ curl localhost<!DOCTYPE html><html><head><title>Welcome to nginx!</title><style>body {width: 35em;margin: 0 auto;font-family: Tahoma, Verdana, Arial, sans-serif;}</style></head><body><h1>Welcome to nginx!</h1><p>If you see this page, the nginx web server is successfully installed andworking. Further configuration is required.</p><p>For online documentation and support please refer to<a href="http://nginx.org/">nginx.org</a>.<br/>Commercial support is available at<a href="http://nginx.com/">nginx.com</a>.</p><p><em>Thank you for using nginx.</em></p></body></html>

Official Nginx Dockerfile

To read the complete post

This is a good place, to stop for Day 5

Please follow me with my Journey

This time to make learning more interactive, I am adding

  • Slack
  • Meetup

Please feel free to join this group.

Slack:

https://100daysofdevops.slack.com/join/shared_invite/enQtNzg1MjUzMzQzMzgxLWM4Yjk0ZWJiMjY4ZWE3ODBjZjgyYTllZmUxNzFkNTgxZjQ4NDlmZjkzODAwNDczOTYwOTM2MzlhZDNkM2FkMDA

Meetup Group

If you are in the bay area, please join this meetup group https://www.meetup.com/100daysofdevops/

--

--

Prashant Lakhera
Prashant Lakhera

Written by Prashant Lakhera

AWS Community Builder, Ex-Redhat, Author, Blogger, YouTuber, RHCA, RHCDS, RHCE, Docker Certified,4XAWS, CCNA, MCP, Certified Jenkins, Terraform Certified, 1XGCP

Responses (1)