WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

DevOps · Intermediate · question 22 of 100

What is a Dockerfile and what are its typical uses?

📕 Buy this interview preparation book: 100 DevOps questions & answers — PDF + EPUB for $5

A Dockerfile is a script containing a set of instructions used to define and build a Docker image. Docker images are lightweight, portable, and self-contained executables that are used to package applications, libraries, dependencies, and configurations. Dockerfiles ensure the consistency and reproducibility of the environment across different stages of development and deployment.

The typical uses of a Dockerfile include:

1. Defining a base image: Specify a starting point for the Docker image. An example of a base image is a minimal Linux distribution or an official language-specific image like ‘nginx‘, ‘python‘, or ‘node‘.

FROM python:3.8

2. Setting up environment variables: Store configuration values which can be used globally across the entire container during runtime.

ENV APP_ENV production

3. Adding files and directories: Copy files and directories from the build context (local filesystem or URL) to the Docker image.

COPY . /app

4. Running commands: Execute commands and scripts during the image building process to set up the environment, install dependencies, or perform configuration tasks.

RUN apt-get update && apt-get install -y build-essential

5. Exposing ports: Specify the network ports that the containerized application will use. This makes it easier for users to know which ports to forward when running the container.

EXPOSE 8080

6. Defining the entry point and command: Specify the initial command and arguments to run when the Docker container starts.

ENTRYPOINT ["python"]
CMD ["app.py"]

Here’s an example of a complete Dockerfile for a simple Python web application:

# Use the official Python 3.8 image as the base image
FROM python:3.8

# Set environment variables
ENV APP_ENV production

# Set the working directory to /app
WORKDIR /app

# Copy the contents of the current directory to the /app directory in the image
COPY . /app

# Install required packages from the requirements.txt file
RUN pip install --trusted-host pypi.python.org -r requirements.txt

# Expose port 8080 for the application to listen on
EXPOSE 8080

# Define the command to run the application
CMD ["python", "app.py"]

To build a Docker image from this Dockerfile, you’d run the following command:

docker build -t my-python-app .

When the build is complete, you can run a container from the newly built image:

docker run -p 8080:8080 my-python-app

In summary, a Dockerfile is a vital component in the containerization process, providing instructions to create a Docker image tailored to a specific application. It ensures the consistency and reproducibility of the environment, simplifying deployment and development across different stages and platforms.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic DevOps interview — then scores it.
📞 Practice DevOps — free 15 min
📕 Buy this interview preparation book: 100 DevOps questions & answers — PDF + EPUB for $5

All 100 DevOps questions · All topics