1. Introduction
Alpine is a lightweight Linux distribution designed to be small, simple, and secure. Because of its features, it has been widely used to run containerized applications. In this tutorial, we’ll learn how to install OpenSSH in an Alpine container.
First, let’s examine some common issues we might face when trying to install OpenSSH in an Alpine container. Then, we’ll examine an effective method to install OpenSSH in a running Alpine container. Finally, we’ll see how to install OpenSSH when building an Alpine image.
2. Issues on Installing OpenSSH in an Alpine Container
On Linux systems, a good option for searching, installing, upgrading, and removing applications is to use a package manager. In the case of Alpine Linux, the default package manager is the apk tool.
Therefore, the most straightforward way to search for and/or install OpenSSH in an Alpine container is to use the apk tool. However, this may sometimes cause issues.
For example, we might get the following result when trying to search for OpenSSH packages:
# apk search openssh
WARNING: opening from cache https://dl-cdn.alpinelinux.org/alpine/v3.20/main: No such file or directory
WARNING: opening from cache https://dl-cdn.alpinelinux.org/alpine/v3.20/community: No such file or directory
In addition, we can get output similar to the following when trying to install OpenSSH packages via apk:
# apk add openssh
WARNING: Ignoring APKINDEX.d3812b7e.tar.gz: No such file or directory
WARNING: Ignoring APKINDEX.bb2c5760.tar.gz: No such file or directory
ERROR: unsatisfiable constraints:
openssh (missing):
required by: world[openssh]
Next, we’ll examine how to avoid these errors and successfully install OpenSSH via apk in two different situations: when the container is already running or when we’re building the Alpine image.
3. Installing OpenSSH in a Running Alpine Container
To properly install OpenSSH in a running Alpine container, we first need to update the index of available packages:
# apk update
fetch https://dl-cdn.alpinelinux.org/alpine/v3.20/main/x86_64/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.20/community/x86_64/APKINDEX.tar.gz
v3.20.2-168-gb223e202b56 [https://dl-cdn.alpinelinux.org/alpine/v3.20/main]
v3.20.2-171-g647c797a189 [https://dl-cdn.alpinelinux.org/alpine/v3.20/community]
OK: 24162 distinct packages available
The above command allows the apk tool to find the OpenSSH packages in the repositories, avoiding the previously mentioned issues. Now, we can just install OpenSSH:
# apk add openssh
(1/10) Installing openssh-keygen (9.7_p1-r4)
(2/10) Installing ncurses-terminfo-base (6.4_p20240420-r0)
(3/10) Installing libncursesw (6.4_p20240420-r0)
(4/10) Installing libedit (20240517.3.1-r0)
(5/10) Installing openssh-client-common (9.7_p1-r4)
(6/10) Installing openssh-client-default (9.7_p1-r4)
(7/10) Installing openssh-sftp-server (9.7_p1-r4)
(8/10) Installing openssh-server-common (9.7_p1-r4)
(9/10) Installing openssh-server (9.7_p1-r4)
(10/10) Installing openssh (9.7_p1-r4)
Executing busybox-1.36.1-r29.trigger
OK: 14 MiB in 24 packages
Alternatively, and most recommended, we can use a single command that properly installs OpenSSH in Alpine:
# apk add --update --no-cache openssh
fetch https://dl-cdn.alpinelinux.org/alpine/v3.20/main/x86_64/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.20/community/x86_64/APKINDEX.tar.gz
OK: 14 MiB in 24 packages
The above command updates the index of available packages and installs the OpenSSH packages without caching things locally, reducing the container’s disk space consumption.
Finally, we can confirm that OpenSSH has been successfully installed using the following command:
# ssh -V
OpenSSH_9.7p1, OpenSSL 3.3.1 4 Jun 2024
4. Installing OpenSSH in an Alpine Container via Dockerfile
Another option to install OpenSSH in Alpine is via Dockerfile. In this way, we’ll build an Alpine image that will include OpenSSH installed.
Basically, we need to add a RUN statement to the Dockerfile that executes the same apk command that we used in the previous section:
FROM alpine:latest
RUN apk add --update --no-cache openssh
Then, we can build the image using the command below. In this example, we’re assigning the name alpine-ssh to the image.
$ docker build -t alpine-ssh .
To check that it works, we can create a new Alpine container using the newly created image and access the shell in interactive mode:
$ docker run -it alpine-ssh /bin/ash
Note that in Alpine, the default shell is called ash, instead of bash as usual.
Finally, we can run the command below to see which version of OpenSSH we installed.
# ssh -V
OpenSSH_9.7p1, OpenSSL 3.3.1 4 Jun 2024
5. Conclusion
In this tutorial, we studied two different ways of installing OpenSSH in an Alpine container. The first method allows us to install OpenSSH in a running Alpine container, accessing the container shell interactively. A second method allows us to install OpenSSH when we’re building the Alpine image.
However, both methods rely on using the apk tool. Therefore, to avoid potential errors, the most important step is to update the index of available packages before installing OpenSSH.