Subhadip's Blog

My experience with the computing world!

Run Alpine Linux as a Docker Container

If you have never heard of Alpine Linux before, it's a distribution of Linux, a very lightweight one . In fact it's so small, an empty container of Alpine Linux can be run with less than 8 MB of memory. And that's one of the reasons why it's extensively used in containers. Docker Hub has an official image of Alpine Linux. So it's fairly easy to run Alpine Linux as a docker container or build one on top of it.

Download the image

To download the alpine image from Docker Hub, run the below command, assuming you already have Docker installed in your system. I am using Alpine version 3.9 which is the latest at the time of writing the post. This link has a list of available tags and versions.

docker pull alpine:3.9

Run the image in interactive mode

To run the alpine image in interactive mode, run the following command:

docker run -it --rm --name alpine alpine:3.9

This should launch an alpine container and land you at a shell prompt. The "-it" flags tells Docker that the image should be run in interactive mode and a tty should be allocated. "--rm" flag ensures that the container is removed as soon as you exit the shell and come out of the interactive mode.

To check the version of Alpine Linux while inside the shell, run

cat /etc/alpine-release

Build your own Docker image

While it can be fun to run it in interactive mode, it's not very useful. To make something useful with docker, you need to create your custom image. In this post, I will create a custom docker image with Alpine Linux that will run a simple HTTP server and I will access the server from the host. Here are the steps.

Create the Dockerfile

Create a file with name Dockerfile and paste the following content.

FROM alpine
MAINTAINER Subhadip Ghosh
RUN apk update
RUN apk add python3
RUN echo "Hello from Alpine Linux" >index.html
ENTRYPOINT ["/usr/bin/python3", "-m", "http.server"]
EXPOSE 8000

Create the Docker Image

Run the following command to create the Docker image. Make sure you are in the same directory as the Dockerfile.

docker build -t alpine-python-server:latest .

Execute the following command to verify that an image has been created with the name "alpine-python-server".

docker images

Run the image

To run the image that was created in the previous step, execute the following command:

docker run -d -p 8000:8000 --name test-server alpine-python-server

This command does the following things:
- Creates a container from the "test-server" image created in the previous step.
- "-d" tells docker to run the container in detached mode ie in the back ground.
- Maps port 8000 of the container with port 8000 of the host so that you can open a web browser in your host system and point to http://localhost:8000 to visit the web-server running inside the container.
- I named our container as "test-server" for ease of reference.

Visit the web-server from host

Since we have mapped the container port on which the web-server is running to the same port on host, if you visit the URL http://localhost:8000 from any web-browser from your host system, you should see something like this:

Note

These steps were tested on a Debian Linux host. If you are using Docker desktop on Windows or MacOS, the docker container port mapping with the host OS may not work the way it is described in the post or may require additional steps as I have come to know. Please do your research before proceeding.



Tags