Docker has become a popular solution for building and deploying applications across development environments and operating systems. Due to its effectiveness and popularity, some may be wondering how to build a Docker image with Alpine Linux. This post will provide the answer, step by step.
Building a Docker image that is both lightweight and feature-rich is a significant achievement in web development environments. We’ll expand on our previous guide to include the full Dockerfile and explain the rationale behind each step.
This Docker image, which totals a mere 34.7MB in its compressed form, is a stark contrast to the combined sizes of Node (381.8 MB) and Composer (46.23 MB) images, making it an ideal candidate for efficient pipeline usage.
Let’s go through the steps of building a Docker image in Alpine Linux with PHP 8.2.10 and Node 18.17.1, one by one.
How to Build a Docker Image in Alpine Linux: Step-by-Step Guide
Step 1: The Base Image
FROM amd64/alpine:3.18.4
We start with amd64/alpine:3.18.4
, a minimal Linux distribution that is known for its small footprint.
Step 2: Getting the Essentials
# Set timezone
RUN echo "UTC" > /etc/timezone
# Install essential tools
RUN apk add --no-cache zip unzip curl
In this step, we set up the time zone to UTC and install essential tools like zip
, unzip
, and curl
for basic file operations and data transfer.
Step 3: Installing Bash
# Installing bash
RUN apk add bash
RUN sed -i 's/bin\/ash/bin\/bash/g' /etc/passwd
Alpine Linux comes with ash
shell by default. In this step, we install bash
for enhanced scripting capabilities and switch the default shell to Bash.
Step 4: Installing PHP 8.2.10 and Node 18.17.1
# Install PHP 8.2.10 and Node 18.17.1
RUN apk add --no-cache php82 \
php82-phar \
php82-curl \
php82-openssl \
php82-mbstring \
php82-tokenizer \
php82-xml \
php82-xmlwriter \
php82-xmlreader \
php82-simplexml \
php82-dom \
nodejs \
npm \
git
This step installs PHP 8.2.10 and its extensions, along with Node 18.17.1 and git
. These are critical for modern web development tasks.
Step 5: Installing Composer 2.6.4
# Installing Composer
RUN curl -sS https://getcomposer.org/installer -o composer-setup.php
RUN php composer-setup.php --install-dir=/usr/local/bin --filename=composer
RUN rm -rf composer-setup.php
Composer manages PHP dependencies. The installation script is downloaded, executed, and then deleted to keep the image clean.
Step 6: Configuring PHP
# Configure PHP
RUN mkdir -p /run/php/
RUN touch /run/php/php8.2-fpm.pid
This step prepares necessary directories and files for PHP’s operation.
Step 7: Finalizing the Dockerfile
# Expose port 3000
EXPOSE 3000/tcp
# Set working directory
WORKDIR /media
This is the last step
We expose port 3000
for the application and define /media
as the working directory.
The Complete Dockerfile
Here is all the code you need to build a Docker image with Alpine Linux, PHP 8.2.10, and Node 18.17.1, without any comments:
FROM amd64/alpine:3.18.4
# Essentials
RUN echo "UTC" > /etc/timezone
RUN apk add --no-cache zip unzip curl
# Installing bash
RUN apk add bash
RUN sed -i 's/bin\/ash/bin\/bash/g' /etc/passwd
# Installing PHP 8.2.10 and Node 18.17.1
RUN apk add --no-cache php82 \
php82-phar \
php82-curl \
php82-openssl \
php82-mbstring \
php82-tokenizer \
php82-xml \
php82-xmlwriter \
php82-xmlreader \
php82-simplexml \
php82-dom \
nodejs \
npm \
git
RUN ln -s /usr/bin/php82 /usr/bin/php
# Installing Composer 2.6.4
RUN curl -sS https://getcomposer.org/installer -o composer-setup.php
RUN php composer-setup.php --install-dir=/usr/local/bin --filename=composer
RUN rm -rf composer-setup.php
# Configure PHP
RUN mkdir -p /run/php/
RUN touch /run/php/php8.2-fpm.pid
EXPOSE 3000/tcp
WORKDIR /media
Deployment to Docker Hub
Curious about how to automatize the deployment of your images to the Docker registry? Just check our post about CI/CD automation.
The Extra Mile
Here you have a Docker-Compose file example to use this image. Just create a docker-compose.yml
file and paste the following code.
version: "3.9"
services:
builder:
container_name: your-project
image: whitecanvas/builder:1.0.4 #MACOS whitecanvas/builder-v8:1.0.4
volumes:
- ./:/media/
extra_hosts:
- "yoursite.local:192.168.1.6"
command:
- sh
tty: true
networks:
- project
networks:
project:
Finally, run the following command in your CLI to release the Kraken.
docker compose up -d
Conclusion
This Docker image, available on Docker Hub, is a testament to efficient resource utilization. With its compact size and comprehensive toolset, it is perfect for deployment in pipelines where speed and efficiency are crucial.
The detailed breakdown of the Dockerfile should help you deploy your Alpine Linux project using PHP 8.2.10, and Node 18.17.1. Good luck!
If you found this post useful, read our blog for more WordPress tips, trick, and guides.