How to set up Docker on Alpine Linux

This post will show you how to set up Docker on Alpine Linux. You will also see how to: add Portainer to make Container management a breeze and use Docker to start and stop containers.

Heads up! This content was tested on Alpine Linux v3.13.

What is Alpine Linux?

Alpine Linux is a lightweight Linux distribution primarily made to run applications on Linux distributions. It is simple, secure, and resource-efficient. In fact, it is so efficient that it can run directly from RAM! In addition, Alpine Linux provides a full-fledged Linux environment built around BusyBox and musl libc, making it smaller and more efficient than traditional GNU / LINUX-based operating systems Ubuntu.

What is Docker?

Docker is a container platform that is used to host a wide range of applications. It originated from Linux Containers (LXC), part of the Linux Open Container Initiative (OCI). It is a user-friendly container technology that uses human-readable code to build container images from a Dockerfile.

What is LXC?

LXC is an operating system-level virtualization technology where multiple containers can run on a single host. The main difference between a Linux Container (LXC) and a kernel-based virtual machine (KVM) is that KVM’s have their own kernels for each virtualized operating system LXC’s the kernel is shared between virtualized operating systems. However, each LXC has its own separate virtual environment, file system, process, and network capabilities. This makes LXC quick, lightweight, and easy to maintain, with minimal deployment overhead.

What is Portainer?

Portainer is an open-source tool for managing containerized applications and works with KubernetesDockerDocker Swarm, and more. You can use Portainer to deploy and manage applications, observe containers’ behavior, and provide the security and governance necessary to deploy containers widely.

Portainer
Portainer

How to setup Docker on Alpine Linux

Setup Docker

First, update the repository index:

sudo apk update

Next, run the following command to install Docker on Alpine Linux:

sudo apk add docker

To start the Docker daemon at boot:

sudo rc-update add docker boot
sudo service docker start

To check the status of Docker, run:

sudo service docker status

Setup Docker-Compose

To install Docker Compose, run:

sudo apk add docker-compose

Setup Portainer

To install Portainer:

sudo docker volume create portainer_data
sudo docker run -d -p 8000:8000 -p 9000:9000 --name=portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce

You can access Portainer via the browser on port 9000.

How to use Docker on Alpine Linux

Docker basics

The basic syntax of the Docker command is shown below:

docker [option] [command] [arguments]

You can list all available options with Docker by running the following command:

docker

You should see the complete list of docker sub-commands in the following output:

Usage:	docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:
      --config string      Location of client config files (default "/home/anthony/.docker")
  -c, --context string     Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker
                           context use")
  -D, --debug              Enable debug mode
  -H, --host list          Daemon socket(s) to connect to
  -l, --log-level string   Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
      --tls                Use TLS; implied by --tlsverify
      --tlscacert string   Trust certs signed only by this CA (default "/home/anthony/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default "/home/anthony/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default "/home/anthony/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit

Management Commands:
  builder     Manage builds
  config      Manage Docker configs
  container   Manage containers
 ...
...

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  build       Build an image from a Dockerfile
  commit      Create a new image from a container's changes
  cp          Copy files/folders between a container and the local filesystem
  create      Create a new container
...
...

Run ‘docker COMMAND –help’ for more information on a command.

You can also display more information about Docker using the following command:

docker info

You should see the following output:

Client:
 Debug Mode: false

Server:
 Containers: 2
  Running: 1
  Paused: 0
  Stopped: 1
 Images: 9
 Server Version: 19.03.8
 Storage Driver: zfs
  Zpool: rpool
  Zpool Health: ONLINE
  Parent Dataset: rpool/ROOT/ubuntu_yybki6/var/lib
  Space Used By Parent: 3448905728
  Space Available: 946661916672
  Parent Quota: no
  Compression: lz4
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Plugins:
...
...

Why not run a container now that you have set up Docker? Next, we will pull and run an Nginx container.

Pull a Docker image from the Docker Hub

Docker Images are pre-built Containers that you can launch on your Docker host. For example, pull the Nginx image from the Docker Hub, run the following command:

sudo docker pull nginx

This will download an Nginx image from the Docker Hub to your local system. Once the download is completed, you should see the following output:

Using default tag: latest
latest: Pulling from library/nginx
bf5952930446: Pull complete 
cb9a6de05e5a: Pull complete 
9513ea0afb93: Pull complete 
b49ea07d2e93: Pull complete 
a5e4a503d449: Pull complete 
Digest: sha256:b0ad43f7ee5edbc0effbc14645ae7055e21bc1973aee5150745632a24a752661
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest

You can list your downloaded image by running the following command:

sudo docker images

You should see all the downloaded images in the following output:

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              4bb46517cac3        3 weeks ago         133MB

Run a Docker container in interactive mode on Alpine Linux

Running a docker container in interactive mode gives you interactive shell access into the container. You can do it using the switch -it as shown below:

sudo docker run -it nginx /bin/bash

This command will bring inside the docker container as shown below:

root@2cc89e08ffb2:/#

Here, you can run any command in the container. For example, run the following command to update the package repository:

root@2cc89e08ffb2:/# apt-get update -y

You can exit from the container with the following command:

root@2cc89e08ffb2:/# exit

Run a Docker container in detached mode

You can run the Docker container in detached mode or background using the -d switch. In this mode, you can run any other commands while the container is running.

You can run an Nginx container in detached mode using the following command:

sudo docker run -it -d -p 8080:80 nginx

Once the container is started, you should see the following output:

c135e6f9cb71940d3f033c2d1b5ea21766c86ca9908e0aa21e11f88fe5839302

You can now list the running containers with the following command:

sudo docker ps

You should see your Nginx container in the following output:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
1db0b827baa2        nginx               "/docker-entrypoint.…"   17 seconds ago      Up 15 seconds       0.0.0.0:8080->80/tcp   lucid_hofstadter

Run a Docker container with volume

A Volume is used when you want to store the data on the host system and share them across all containers.

First, create a new volume directory on the host system:

mkdir /volume

Next, run the Nginx container and mount the /volume directory to the Nginx container on the /var/www/html directory.

sudo docker run -it -d -v /volume:/var/www/html -p 8080:80 nginx

Next, connect to the Nginx container with the following command:

sudo docker exec -it aba00ef3968b /bin/bash

Then, create a file named test.html inside /var/www/html directory:

touch /var/www/html/test.html

Now, exit from the Nginx container with the following command:

exit

Now, run the following command to list the available files in the /volume directory:

ls /volume

You should see the file named test.html, which you have created inside the Nginx container:

test.html

Copy a file to and from the Docker container

The simple and easiest way to copy a file to and from the running container is to use the “docker cp” command.

To copy a file named /etc/hosts from the host system to the Nginx container, run the following command:

sudo docker cp /etc/hosts aba00ef3968b:/opt/

To copy a file named /etc/fstab from the Nginx container to the host system, run the following command:

sudo docker cp aba00ef3968b:/etc/fstab /opt/

Start, stop and pause the container

To stop the Nginx container, run the following command:

sudo docker container stop aba00ef3968b

To start the Nginx container, run the following command:

sudo docker container start aba00ef3968b

You can also pause the Nginx container using the following command:

sudo docker container pause aba00ef3968b

To stop all running containers, run the following command:

sudo docker container stop $(docker container ls -aq)

Remove Docker container

To remove a container, you will need to stop it first.

For example, stop the Nginx container with the following command:

docker container stop aba00ef3968b

Next, remove the Nginx container using the following command:

docker container rm aba00ef3968b

If you want to remove all the stopped containers, run the following command:

docker container rm $(docker container ls -aq)

Remove Docker images

To remove the Docker images, list all images in your system with the following command:

docker images

You should see the following output:                                                         

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              4bb46517cac3        3 weeks ago         133MB	

Now, remove the Nginx image with the following command:

docker image rm 4bb46517cac3

You should see the following output if any container uses the image:

Error response from daemon: conflict: unable to delete 4bb46517cac3 (cannot be forced) - image is being used by running container 798c8c54736c

In this case, you will need to stop that container then remove the image.

To remove all  unused images in your system, run the following command:

sudo docker image prune -a

You will be asked to confirm before removing the image as shown below:

WARNING! This will remove all images without at least one container associated to them.
Are you sure you want to continue? [y/N] y
Deleted Images:
untagged: nginx:latest
untagged: nginx@sha256:b0ad43f7ee5edbc0effbc14645ae7055e21bc1973aee5150745632a24a752661
deleted: sha256:4bb46517cac397bdb0bab6eba09b0e1f8e90ddd17cf99662997c3253531136f8
deleted: sha256:80b21afd8140706d5fe3b7106ae6147e192e6490b402bf2dd2df5df6dac13db8
deleted: sha256:0f04ae71e99f5ef9021b92f76bac3979e25c98d73a51d33ce76a78da6afa9f27
deleted: sha256:9a14852344d88a1fdf8297914729834521ec1c77a27e7e7e394f9c1ef9b87f9d
deleted: sha256:74299126f8099031c5bbd4774147f4ab6b0d0c3afcec774be65d4d07b956752e
deleted: sha256:d0f104dc0a1f9c744b65b23b3fd4d4d3236b4656e67f776fe13f8ad8423b955c

Total reclaimed space: 132.6MB

Wrapping up

Now you have learned how to set up Docker on Alpine Linux, and you now also know some of the basic Docker commands to get you up and running.

You may also be interested in: How to Setup Docker on Ubuntu.

You may also be interested in

Sources

About Anto Online

Anto, a seasoned technologist with over two decades of experience, has traversed the tech landscape from Desktop Support Engineer to enterprise application consultant, specializing in AWS serverless technologies. He guides clients in leveraging serverless solutions while passionately exploring cutting-edge cloud concepts beyond his daily work. Anto's dedication to continuous learning, experimentation, and collaboration makes him a true inspiration, igniting others' interest in the transformative power of cloud computing.

View all posts by Anto Online

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.