The DevOps bootcamp · Chapter 5 of 14 · DevOps · new chapter every Thursday morning
By the end of this chapter: Build a small, reproducible image and explain every layer in it.
The problem
You are a junior engineer on your first backend project, and you need to test a branch on the team's shared staging server. You SSH into the server, pull your code, and run the start command. However, the terminal spits back Error: listen EADDRINUSE: address already in use :::8080. You try changing the port, but the proxy routing traffic to your application expects 8080. You assume the previous version of the application is still running, but you do not know how to find it. You try running your new application as the superuser to force it through, and it starts, but immediately crashes with Permission denied: /var/log/staging-api/error.log. This is not only frustrating but also wastes valuable time, and you are under pressure to deliver.
The issue here is not just about finding a way to run your application, but also about managing the underlying infrastructure and ensuring that your application is properly configured and isolated from other applications running on the same server. This is where containers come into play. Containers provide a lightweight and portable way to package your application and its dependencies, ensuring that it runs consistently across different environments.
In this chapter, we will focus on building a small, reproducible image and explaining every layer in it. We will explore how containers work, how to create and manage them, and how to optimize them for production use.
Before you start
To follow along with this chapter, you will need to have Docker installed on your machine. You can download the latest version of Docker from the official Docker website. Once installed, you can verify that Docker is working by running the command docker --version in your terminal. This should output the version of Docker that you just installed.
You should also have a basic understanding of Linux and the shell, as covered in chapters 2 and 3 of this course. Specifically, you should be familiar with concepts such as processes, files, permissions, and networking.
Understanding Containers
A container is a lightweight and portable way to package your application and its dependencies. It provides a consistent and reliable way to run your application across different environments, from development to production. Containers are similar to virtual machines, but they are much lighter and more efficient.
A container consists of several layers, each of which represents a specific component of the application. These layers are stacked on top of each other to form the final container image. Each layer is immutable, meaning that once it is created, it cannot be changed.
Building a Container Image
To build a container image, you need to create a Dockerfile. A Dockerfile is a text file that contains instructions for building a Docker image. It specifies the base image, copies files, installs dependencies, and sets environment variables.
Here is an example of a simple Dockerfile:
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "app.py"]
This Dockerfile uses the official Python 3.9 image as its base, sets the working directory to /app, copies the requirements.txt file, installs the dependencies, copies the application code, and sets the command to run the application.
Build it
To build the container image, follow these steps:
- Create a new directory for your project and navigate to it in your terminal.
- Create a new file called Dockerfile and add the above contents to it.
- Create a new file called requirements.txt and add the dependencies required by your application to it.
- Create a new file called app.py and add your application code to it.
- Run the command
docker build -t my-app .to build the container image. - Verify that the image was built successfully by running the command
docker imagesand looking for the my-app image.
When this breaks
One common issue that can occur when building a container image is that the image becomes too large. This can happen when the Dockerfile copies too many files or installs too many dependencies. To fix this issue, you can use the docker history command to see the layers that make up the image and identify which layers are taking up the most space.
Another issue that can occur is that the container fails to start due to a missing dependency. To fix this issue, you can use the docker logs command to see the output of the container and identify which dependency is missing.
What it costs
Using containers can have several benefits, including improved isolation, portability, and efficiency. However, it also has some costs, such as increased complexity and overhead. Containers require a significant amount of disk space and memory, especially if you have a large number of images and containers.
In addition, containers can be slower than running the application directly on the host machine, due to the overhead of the container runtime. However, this overhead is typically small and can be mitigated by using a high-performance container runtime such as Docker.
In the interview
In an interview, you may be asked to explain the benefits and trade-offs of using containers. A weak answer might focus only on the benefits, such as improved isolation and portability, without discussing the potential drawbacks, such as increased complexity and overhead.
A strong answer would discuss both the benefits and trade-offs, and provide specific examples of how containers can be used to improve the development and deployment process. For example, you might explain how containers can be used to create a consistent and reliable development environment, or how they can be used to improve the efficiency of the deployment process.
As a junior engineer, you might be expected to explain the basics of containers, such as how they work and how to create and manage them. As a senior engineer, you might be expected to discuss more advanced topics, such as container orchestration and networking.
Your tasks
- Create a new Dockerfile that uses the official Node.js image as its base and installs the dependencies required by a simple web application.
- Build the container image and verify that it was built successfully.
- Run the container and verify that it is working as expected.
- Use the
docker historycommand to see the layers that make up the image and identify which layers are taking up the most space. - Create a new Dockerfile that uses a multi-stage build to reduce the size of the final image.
Your tasks this week
Do the exercises above before the next chapter. Reading a tutorial and doing
one are different activities and only one of them changes what you can build.
Stuck on any of them? Say so — describe what you tried and what happened:
tell me where you got stuck. I read every one, and the questions
that come back more than twice get answered in the next chapter.
The DevOps bootcamp
Chapter 5 of 14. New chapter every Thursday morning.
Next: Compose: running a real stack locally.
· The full syllabus and every chapter so far
· Subscribers also get the condensed notes for this chapter, the running
recap of everything the series has covered, and the extended guidance:
subscribe
Written by Amit Chakraborty — founding engineer and senior architect: React Native, AI and RAG systems, production architecture. Portfolio · LinkedIn · GitHub.
Need this built, reviewed or taught to your team? Get in touch or email amit@devamit.co.in. Available for senior and founding engineering roles, consulting and training, remote worldwide.
Top comments (0)