DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How To Use the Node Docker Official Image
  • Using Environment Variable With Angular
  • Docker Commands Beginners Should Know
  • How to Integrate a Distributed Database With Event Streaming

Trending

  • Scaling DevOps With NGINX Caching: Reducing Latency and Backend Load
  • Cosmos DB Disaster Recovery: Multi-Region Write Pitfalls and How to Evade Them
  • Accelerating Debugging in Integration Testing: An Efficient Search-Based Workflow for Impact Localization
  • Endpoint Security Controls: Designing a Secure Endpoint Architecture, Part 2
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Understanding Dockerfile

Understanding Dockerfile

Get a in-depth look at the internals make up of a Dockerfile and the commands within it.

By 
Manjula Jayawardana user avatar
Manjula Jayawardana
·
Jul. 23, 19 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
40.2K Views

Join the DZone community and get the full member experience.

Join For Free

We have used Docker images to create containers. We used images from Docker Hub to create those containers. But have you ever wondered how to create a Docker image? Docker can build images automatically by reading the instructions from a Dockerfile. DZone previously covered building Docker images with Jenkins.

Image title

Dockerfile

A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Think of it as a shell script. It gathered multiple commands into a single document to fulfill a single task.

build command is used to create an image from the Dockerfile.

 $ docker build 

You can name your image as well.

 $ docker build -t my-image 

If your Dockerfile is placed in another path,

 $ docker build -f /path/to/a/Dockerfile . 

Let's first look at a Dockerfile and discuss those commands.

We are going to take the official MySQL Dockerfile as an example.

FROM debian:stretch-slim

# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added

RUN groupadd -r mysql && useradd -r -g mysql mysql

RUN apt-get update && apt-get install -y --no-install-recommends gnupg dirmngr && rm -rf /var/lib/apt/lists/*

RUN mkdir /docker-entrypoint-initdb.d

ENV MYSQL_MAJOR 8.0

ENV MYSQL_VERSION 8.0.15-1debian9

VOLUME /var/lib/mysql

# Config files

COPY config/ /etc/mysql/

COPY docker-entrypoint.sh /usr/local/bin/

RUN ln -s usr/local/bin/docker-entrypoint.sh /entrypoint.sh # backwards compat

ENTRYPOINT ["docker-entrypoint.sh"]

EXPOSE 3306 33060

CMD ["mysqld"]


Here's what's being done here:

  • Select the operating system.

  • Create a user for MySQL.

  • Update packages and install software.

  • Configure the environment.

These are the steps we will use to install MySql in our Linux machine. Now it's bundled inside the dockerfile to anyone to get and create images.

Read DZone’s DevOps’ tutorial on Docker, Kubernetes, and Azure.

Let's try to understand the purposes of these commands.

Dockerfile Commands

  •  FROM - specifies the base(parent) image. Alpine version is the minimal docker image based on Alpine Linux which is only 5mb in size.

  •  RUN - runs a Linux command. Used to install packages into container, create folders, etc

  •  ENV - sets environment variables. We can have multiple variables in a single dockerfile.

  •  COPY - copies files and directories to the container.

  •  EXPOSE - expose ports

  •  ENTRYPOINT - provides command and arguments for an executing container.

  •  CMD - provides a command and arguments for an executing container. There can be only one CMD.

  •  VOLUME - create a directory mount point to access and store persistent data.

  •  WORKDIR - sets the working directory for the instructions that follow.

  •  LABEL - provides metada like maintainer.

  •  ADD - Copies files and directories to the container. Can unpack compressed files.

  •  ARG - Define build-time variable.

Review a list of all Docker commands. There are a few commands which are little confusing. Let's have a look at them.

COPY vs. ADD

Both commands serve a similar purpose, to copy files into the image.

  •  COPY - let you copy files and directories from the host.

  •  ADD - does the same. Additionally it lets you use URL location and unzip files into image.

Docker documentation recommends using the COPY command.

ENTRYPOINT vs. CMD

  •  CMD - allows you to set a default command which will be executed only when you run a container without specifying a command. If a Docker container runs with a command, the default command will be ignored.

  •  ENTRYPOINT - allows you to configure a container that will run as an executable. ENTRYPOINT command and parameters are not ignored when Docker container runs with command line parameters.

Related tutorial: How to perform Docker health checks.

VOLUME

You declare volume in your Dockerfile to denote where your container will write application data. When you run your container using -v   you can specify its mounting point.

Conclusion

Studying official Dockerfiles is a good way to learn more about the Dockerfile. We definitely have to start making our own Dockerfiles. 

Docker (software) Command (computing)

Opinions expressed by DZone contributors are their own.

Related

  • How To Use the Node Docker Official Image
  • Using Environment Variable With Angular
  • Docker Commands Beginners Should Know
  • How to Integrate a Distributed Database With Event Streaming

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!