Open In App

Building Lightweight Containers Using Alpine Linux and Docker

Last Updated : 17 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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 .

file

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"

list

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

inside-container

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:

  1. Alpine images are used in CI/CD Pipelines for faster execution.
  2. It is used in environments where resources are limited like for instance in IoT
  3. They are also used because of the simple architecture and security.
  4. Alpine Linux images are useful for testing applications.

Advantages and Disadvantages

Advantages of Alpine Linux Images

  1. The booting time for Alpine Linux is typically less when compared with other Operating Systems
  2. This Operating System has only C libraries and is compatible with most of the software.
  3. Alpine images are customizable thereby enabling developers to customize as per their needs
  4. It is simple, fast and reliable.
  5. 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.

Next Article
Article Tags :

Similar Reads