Featured Video Play Icon

What is a Dockerfile?

When you’re getting started with Docker, it is important to understand what is a Dockerfile. You will work with Dockerfiles in every project you work on regardless of the operating system you’re using in your image. If you want to follow along with the demos in this blog post, you will need the food trucks repo from the Docker community repos. You can find it on their GitHub.

Lead Developer Career Guide Banner 01

A Dockerfile is similar to a batch script that contains all the commands to build a Docker image. The first line states the base image to begin with and then follow the instructions to install required modules, copy files, and deploy the code which results in a working environment. Every Dockerfile must start with the FROM instruction. This tells Docker what base image to use as you build your final image. In this case, we are using the ubuntu standard OS image. You can find images for many different operating systems and development environments for any technology you’re using.

Example Dockerfile

The LABEL instruction adds metadata to an image. A LABEL is a key-value pair. You can add multiple labels to a Dockerfile to include any custom information you want. In this example, we are creating a key named “maintainer” and setting it equal to the text on the right. Labels are visible when you use a docker inspect command to view low-level information on Docker objects which is helpful to keep your projects organized.

LABEL maintainer="Prakhar Srivastav <[email protected]>"

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. All instructions are executed in order so you have to be careful to add your run instructions in the exact order they should be run. In this example, we are installing the required system-wide dependencies for Python and node as these are needed to run the food trucks website.

# install system-wide deps for python and node
RUN apt-get -yqq update
RUN apt-get -yqq install python3-pip python3-dev curl gnupg
RUN curl -sL https://deb.nodesource.com/setup_10.x | bash
RUN apt-get install -yq nodejs

The ADD instruction copies new files, directories or remote file URLs from the <src> listed on the left and adds them to the filesystem of the image at the <dest> path listed on the right. The destination path is located inside the image that is being built by the Dockerfile.

# copy our application code
ADD flask-app /opt/flask-app

Open a terminal and execute the command docker-compose up and then inspect the filesystem of your container to find the destination file path. You can even save this image as a reference to use for later if you want.

To save an image from a running container, get the container id by typing docker ps. Then, run a docker commit command using the container id and then a custom image name and version. That’s it! You can run the docker images command to confirm your new image has been created.

docker commit [container id] [image name]:[image version]

The WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile. In this example, the node package manager commands are run in the working directory that has been defined.

WORKDIR /opt/flask-app

# fetch app specific deps
RUN npm install
RUN npm run build
RUN pip3 install -r requirements.txt

The EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime. EXPOSE does not make the ports of the container accessible to the host.

# expose port
EXPOSE 5000

And finally, we have the CMD instruction. There can only be one CMD instruction in a Dockerfile . If you list more than one CMD then only the last CMD will take effect. The main purpose of a CMD is to provide defaults for an executing container. In this example, we are running the app script for the website located in the root of the working directory and naming the app python3.

# start app
CMD [ "python3", "./app.py" ]

That’s it! Stop your running container by executing the docker-compose down command. Go back to the Dockerfile and uncomment everything using the shortcut Ctrl+k+u. Save your work and execute docker-compose up.

Open a browser and navigate to localhost on port 5000. You should see the foodtrucks website!

Food Trucks Website

Tactics and Tools for Troubleshooting Docker – Online Course Available on Pluralsight

Tactics and Tools for Troubleshooting Docker on Pluralsight
Tactics and Tools for Troubleshooting Docker – Online Course Available on Pluralsight

If you’re struggling with Docker errors, check out my Pluralsight course, Tactics and Tools for Troubleshooting Docker. This course provides developers with the skills they need to troubleshoot and fix the most common Docker errors. By the end of this course, you will have the skills you need to reduce the time it takes you to assess and fix issues. Click the link below to get started with a free trial today!

Categories: DevOps, DockerTags: