This website uses cookies to enhance the user experience

Dockerfile Basics

Share:

In the heart of every Docker container, there's a Dockerfile. It's just like the script behind your favorite films, guiding and orchestrating every move and sequence. A Dockerfile is a text file that Docker reads to build an image automatically by reading the instructions from the Dockerfile. Consider it as a blueprint for creating a Docker image. Within a Dockerfile, one can specify the Operating System of the image, the installation processes, and the dependencies the application requires to run. When Docker reads a Dockerfile, it builds an image which can then be used to run an instance of a container.

Time to dive right in and begin with the creation of a basic Dockerfile. Let's use the filmic analogy to set our example, creating a container with necessary dependencies for a hypothetical application called "MovieList".

Before we begin, there is a convention to be maintained with Dockerfiles. They should be named "Dockerfile" with a capital 'D' and no file extension.

Take a look at the following simple Dockerfile:

# Use an official Python runtime as the parent image
FROM python:3.7-slim

# Set the working directory in the container to /app
WORKDIR /app

# Add the current directory contents into the container at /app
ADD . /app

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Run app.py when the container launches
CMD ["python", "app.py"]

It's useful to step through each line of the Dockerfile to understand how it works.

  • The FROM command specifies the parent image from which you are building. In our movie application case, we are using a slimmed down version of Python 3.7 as the base image.
  • WORKDIR sets the working directory inside the container. In our case, when the container is run, it starts at the /app directory.
  • ADD . /app copies the current directory (i.e., the directory containing our Dockerfile) on the host and adds them to the /app directory in the container.
  • RUN pip install --no-cache-dir -r requirements.txt will then install the necessary dependencies for our MovieList application that we've specified in requirements.txt into the Docker image.
  • EXPOSE 80 means that Docker makes port 80 available outside of the Docker container, necessary if our application needed to communicate outward.
  • The CMD command is the final script run after every other line in the Dockerfile is processed. For our film buff application, the Python command runs app.py.

To build our Docker image from this Dockerfile, we navigate to the directory containing the Dockerfile and execute the Docker build command. If our Dockerfile resides in the directory movieapp, for example, we would use the following bash command in our terminal to build the Docker image:

cd movieapp
docker build -t movie_list_image .

Here, docker build is the command to build the Docker image. -t movie_list_image is an optional flag to tag the image with a name. The . at the end signifies that the Dockerfile is located in the current directory.

Upon successful execution of the command, Docker will output logs from each executable command in the Dockerfile. When complete, the image should be available in Docker's local image storage, ready to launch into a container.

To create a container from this image, we simply run the command docker run -p 4000:80 movie_list_image. This tells Docker to run a container using movie_list_image and map it to port 4000.

Just like a movie's script shapes its plot and scenes by dictating how, when, and where things unfold, our Dockerfile has defined the structure, configuration, and progression of our Docker image. Its sequences of commands have given life to our Docker container.

A Dockerfile provides a mighty blueprint, offering the power to stand up an isolated environment rapidly and consistently. Thus, Dockerfiles are an essential tool in a developer's toolkit, driving the dynamic narrative of development in today's containerized application landscape. Naturally, further scripting commands are available to use within Dockerfiles, building more complex and fine-tuned images and containers.

In the next tutorials, we'll explore deeper into the functions Dockerfile allows, such as ENTRYPOINT, ENV, USER, and many other exciting features. We will also learn how to optimize Dockerfiles and practices to get smaller images and faster build times. In the meantime, happy 'Dockerfile'ing!

0 Comment


Sign up or Log in to leave a comment


Recent job openings