Building Lightweight Containers Using Alpine Linux and Docker
Last Updated :
17 Sep, 2024
Microservices is a category of architecture that allows small services to communicate over the network independently. Since it is a decentralized architectural style, each service has its own functionality, and if any service fails the rest of the services work uninterrupted. Now to host these services over the network we need Docker that will help in management of microservices.
What is Docker
Docker is an application software that helps us to manage our applications or projects in containers. Containers are standalone units that provide the environment for the applications to run. Now we need images to specify the dependencies required for a particular container. A mage is a read only file that comprises of all the necessary details including packages and dependencies that are required to run the application.
What is Alpine Linux
Alpine Linux is a Linux-distributed Operating System that is simple and lightweight. This Operating System is preferred over Debian, and Ubuntu because it is simple, secure, small size and efficient.
- Simple: Alpine Linux provides a minimalist environment. It focuses on providing the basic environment that is required to run the applications.
- Small Size: Alpine Linux Images are smaller in size and consumes less resources. Typically the size of the the image is around 5 MB, thereby making it more suitable for use.
- Secure: Alpine images are less susceptible to attacks because of their straightforward environment. Also, it makes use of BusyBox which is more secured as compared to GNU ones.
- Efficient: Since Alpine Linux is simple and consumes fewer resources, it is most suitable in Kubernetes clusters where there is always a need for handling resources.
Building Lightweight Containers with Alpine Linux and Docker
Here we will create a calculator using a Python file and execute that file in the Alpine Linux environment present inside the container. To make use of Alpine images, we have to install Docker Desktop. After installation of Docker Desktop, create the dockerfile and Python file. Finally, we will open the Command Prompt and execute some commands. The steps are as follows:
1. Open Docker Desktop and ensure that the Engine is in running mode
2. Create a simple calculator Python file and name it as app.py
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
if y == 0:
return "Error: Division by zero"
return x / y
def main():
print("Simple Calculator")
print("Select operation:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
choice = input("Enter choice (1/2/3/4): ")
if choice in ['1', '2', '3', '4']:
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
if choice == '1':
print(f"{num1} + {num2} = {add(num1, num2)}")
elif choice == '2':
print(f"{num1} - {num2} = {subtract(num1, num2)}")
elif choice == '3':
print(f"{num1} * {num2} = {multiply(num1, num2)}")
elif choice == '4':
print(f"{num1} / {num2} = {divide(num1, num2)}")
else:
print("Invalid input")
if __name__ == "__main__":
main()
3. In the Dockerfile, define all the dependencies
# Using the official Alpine Linux base image
FROM alpine:latest
# Installing Python and necessary packages
RUN apk add --no-cache python3 py3-pip
# Setting the working directory in the container
WORKDIR /app
# Copying the Python script into the container
COPY app.py .
# Setting the default command to run the Python script inside bash
CMD ["bash"]
4. Ensure that the Dockerfile, Python file are in same directory. Now we will use the build command to build our images.
docker build -t my-app -f Dockerfile.txt .
5. Now to get the status of the container use docker ps and find command. Here the name of the image is my-app and name of the container is my-python-app.
docker ps -a| find "my-python-app"
6. Use docker run and at the end provide sh so that you can use the Alpine Linux inside your container. Once your are inside the container execute the python file using Python3 command
docker run -it --name my-python-app my-app sh
Use of Alpine Linux Images
Alpine Linux is widely used as base image in Dockerfile because of it's simplicity, security and efficiency. Some uses of Alpine Linux images are as follows:
- Alpine images are used in CI/CD Pipelines for faster execution.
- It is used in environments where resources are limited like for instance in IoT
- They are also used because of the simple architecture and security.
- Alpine Linux images are useful for testing applications.
Advantages and Disadvantages
Advantages of Alpine Linux Images
- The booting time for Alpine Linux is typically less when compared with other Operating Systems
- This Operating System has only C libraries and is compatible with most of the software.
- Alpine images are customizable thereby enabling developers to customize as per their needs
- It is simple, fast and reliable.
- It's small size makes it most preferred choice in Images.
Disadvantages
Although Alpine Linux Images are widely used, yet they have some drawbacks. Some of them are as follows
- Since Alpine Linux makes use of musl library, it has certain compatibility issues with other software.
- It can perform slow in certain tasks.
- Since it's lightweight, there have been issues regarding the stability of the OS.
Similar Reads
Podman and CRI-O: Building, Running, and Managing Containers
Podman is daemonless, open-source, and natively runs on Linux. Podman makes it easy to identify, execute, develop, share, and deploy applications using Open Containers Initiative (OCI) containers and container images. Podman has a command-line interface that is well-known to everyone who uses the Do
4 min read
How to Use AWS CLI in Docker Container ?
The AWS Command Line Interface (CLI) is a powerful tool that allows users to interact with AWS services directly from the terminal. Integrating AWS CLI within a Docker container can significantly streamline workflows, especially for development and deployment processes that rely on cloud infrastruct
4 min read
Understanding the Docker Desktop Architecture and Linux Building Block for Containers
In this article we are going to do a technology deep dive and start understanding the foundational Linux kernel features that software like Docker is using to make the containers work and provide those isolated environments that we all use. then towards the end of this section, we'll also take a loo
9 min read
Docker - Remove All Containers and Images
In Docker, if we have exited a container without stopping it, we need to manually stop it as it has not stopped on exit. Similarly, for images, we need to delete them from top to bottom as some containers or images might be dependent on the base images. We can download the base image at any time. So
10 min read
Docker Swarm: Building a Highly Scalable Cluster
Docker Swarm is one of the most popular container orchestration engines. It is not only used by professional engineers but also by people is their learning phase of how operations work. What makes Docker Swarm so popular is the fact that is very lightweight and very simple to understand - you don't
10 min read
Docker: How To Use Bash With An Alpine Based Docker Image?
Docker is a tool that is used to encapsulate the application with all its dependencies, called Docker containers. On the other hand, Alpine Linux is a lightweight and minimal Linux distribution. Here in this guide, I will first discuss what Docker is. Then I will discuss what Alpine is. After this,
5 min read
How to Export and Import Docker Containers and images
In software development, flexibility and portability are important. Suppose youâve built a perfect application in a Docker container on your development machine and now you need to move this same setup to a colleagueâs machine or a production server. How do you ensure your application, and all its d
6 min read
Getting Docker Container ID from Container Name
Docker is a platform that allows developers to design, deploy, and manage programs within lightweight, portable containers. Containers bring together a program and its dependencies to maintain consistency across environments. Container management is critical in DevOps, and one typical duty is obtain
4 min read
Running Commands Inside Docker Container
If you are working on an application inside the Docker Container, you might need commands to install packages or access file system inside the Docker Container. Executing commands inside Docker Containers should be easy enough for you since you have to do it multiple times across your development ph
6 min read
Building and Managing .NET Applications with Docker Compose
Modern software development involves putting together applications from a variety of services that have to collaborate. For .NET developers, this can be really complex if everything is done in the development environment. Docker Compose provides an easy way for developers to define and manage multi-
5 min read