- Name already in use
- labs / beginner / chapters / alpine.md
- Users who have contributed to this file
- How to install Docker CE on Alpine Linux
- Steps to install Docker on Alpine Linux
- Step 1: Download and Setup Alpine Linux
- Step 2: Command to install Docker on Alpine
- Step 3: Apk add fails with unsatisfiable constraints error
- How to install docker on Alpine Linux VM
- Introduction
- Create Alpine VM on Proxmox
- Initial Setup
- Install qemu-guest-agent [optional]
- Install Docker Engine
- How to set up Docker on Alpine Linux
- What is Alpine Linux?
- What is Docker?
- What is LXC?
- What is Portainer?
- How to setup Docker on Alpine Linux
- Setup Docker
- Setup Docker-Compose
- Setup Portainer
- How to use Docker on Alpine Linux
- Docker basics
- Pull a Docker image from the Docker Hub
- Run a Docker container in interactive mode on Alpine Linux
- Run a Docker container in detached mode
- Run a Docker container with volume
- Copy a file to and from the Docker container
- Start, stop and pause the container
- Remove Docker container
- Remove Docker images
- Wrapping up
- You may also be interested in
- Related Posts
- Update Ubuntu using Apt & Cron
- Set up CrowdSec to protect your WordPress site
- PHP: How to fix the “Call to undefined function curl_init()” error
- About Anto Online
- Leave a Reply
Name already in use
labs / beginner / chapters / alpine.md
- Go to file T
- Go to line L
- Copy path
- Copy permalink
6 contributors
Users who have contributed to this file
Copy raw contents
Copy raw contents
1.0 Running your first container
Now that you have everything setup, it’s time to get our hands dirty. In this section, you are going to run an Alpine Linux container (a lightweight linux distribution) on your system and get a taste of the docker run command.
To get started, let’s run the following in our terminal:
Note: Depending on how you’ve installed docker on your system, you might see a permission denied error after running the above command. Try the commands from the Getting Started tutorial to verify your installation. If you’re on Linux, you may need to prefix your docker commands with sudo . Alternatively you can create a docker group to get rid of this issue.
The pull command fetches the alpine image from the Docker registry and saves it in our system. You can use the docker images command to see a list of all images on your system.
Great! Let’s now run a Docker container based on this image. To do that you are going to use the docker run command.
What happened? Behind the scenes, a lot of stuff happened. When you call run ,
- The Docker client contacts the Docker daemon
- The Docker daemon checks local store if the image (alpine in this case) is available locally, and if not, downloads it from Docker Store. (Since we have issued docker pull alpine before, the download step is not necessary)
- The Docker daemon creates the container and then runs a command in that container.
- The Docker daemon streams the output of the command to the Docker client
When you run docker run alpine , you provided a command ( ls -l ), so Docker started the command specified and you saw the listing.
Let’s try something more exciting.
OK, that’s some actual output. In this case, the Docker client dutifully ran the echo command in our alpine container and then exited it. If you’ve noticed, all of that happened pretty quickly. Imagine booting up a virtual machine, running a command and then killing it. Now you know why they say containers are fast!
Try another command.
Wait, nothing happened! Is that a bug? Well, no. These interactive shells will exit after running any scripted commands, unless they are run in an interactive terminal — so for this example to not exit, you need to docker run -it alpine /bin/sh .
You are now inside the container shell and you can try out a few commands like ls -l , uname -a and others. Exit out of the container by giving the exit command.
Ok, now it’s time to see the docker ps command. The docker ps command shows you all containers that are currently running.
Since no containers are running, you see a blank line. Let’s try a more useful variant: docker ps -a
What you see above is a list of all containers that you ran. Notice that the STATUS column shows that these containers exited a few minutes ago. You’re probably wondering if there is a way to run more than just one command in a container. Let’s try that now:
Running the run command with the -it flags attaches us to an interactive tty in the container. Now you can run as many commands in the container as you want. Take some time to run your favorite commands.
That concludes a whirlwind tour of the docker run command which would most likely be the command you’ll use most often. It makes sense to spend some time getting comfortable with it. To find out more about run , use docker run —help to see a list of all flags it supports. As you proceed further, we’ll see a few more variants of docker run .
In the last section, you saw a lot of Docker-specific jargon which might be confusing to some. So before you go further, let’s clarify some terminology that is used frequently in the Docker ecosystem.
- Images — The file system and configuration of our application which are used to create containers. To find out more about a Docker image, run docker inspect alpine . In the demo above, you used the docker pull command to download the alpine image. When you executed the command docker run hello-world , it also did a docker pull behind the scenes to download the hello-world image.
- Containers — Running instances of Docker images — containers run the actual applications. A container includes an application and all of its dependencies. It shares the kernel with other containers, and runs as an isolated process in user space on the host OS. You created a container using docker run which you did using the alpine image that you downloaded. A list of running containers can be seen using the docker ps command.
- Docker daemon — The background service running on the host that manages building, running and distributing Docker containers.
- Docker client — The command line tool that allows the user to interact with the Docker daemon.
- Docker Store — A registry of Docker images, where you can find trusted and enterprise ready containers, plugins, and Docker editions. You’ll be using this later in this tutorial.
For the next step in the tutorial, head over to 2.0 Webapps with Docker
How to install Docker CE on Alpine Linux
For Docker containers, Alpine Linux is an optimum choice because of lightweight, low RAM consumption and optimization. However, yet Ubuntu is the best.
Alpine Linux is a free and open-source Linux operating system for routers, firewalls, VPNs, VoIP computers, servers, virtual machines and containers. It has proactive security features that prevent security holes in the software from being exploited. The small, lightweight Linux distribution based on musl libc and busybox.
Steps to install Docker on Alpine Linux
I am not going to talk much on Docker here because if you are reading this tutorial then you would already know what it is. Well, in short Docker is a containerized Virtualization platform that allows running different virtual machines in the form of isolated containers. The best thing about it, lightweight and easy to handle; one can simply pull existing Linux or apps images to setup a container from hub.docker.com.
Step 1: Download and Setup Alpine Linux
If you already have a working Alpine Linux then simply move to the next step and if not then go to this link and download one . Alpine Linux is available in multiple forms such as Standard, Extended with some extra packages, Net-install, Optimized Virtual ISO image, for Xen with built-in support for Xen Hypervisor and in Minimal root filesystem for containers. You can download one as per your choice, however, here we are using Alpine Linux Extended version 3.11.
After downloading boot your PC or VirtualBox with it and run the command setup-alpine to follow the installation steps.
Step 2: Command to install Docker on Alpine
Run the following single command to fetch Docker packages for its installation on Alpine.
Step 3: Apk add fails with unsatisfiable constraints error
Incase after executing the above command you get an error “apk add fails with unsatisfiable constraints” then we have to add the following repository to Alpine.
How to install docker on Alpine Linux VM
Introduction
Alpine linux is a lighweight linux distro, making it small, fast and ideal for VM’s when server resources are limited. Especially when talking about running docker containers, a VM is the only way to go since LXC containers are not supported and its hacky to make docker run inside an LXC.
The Docker containers can be administered through the command line or by using a GUI tool.The two most lightweight administration tools are:
- Cockpit, it can run only in systemd distros so it is not an option for Alpine.
- Portainer, comes as a docker container, ideal for Alpine.
Following are the steps required to configure an Alpine system and install Docker.
Create Alpine VM on Proxmox
Download the latest .iso from Virtual category in https://alpinelinux.org/downloads/ .
Upload it to Proxmox VE from gui and create VM.
Typical example settings with one network card on Intel Core2 quad core PC
-
- OS -> Other OS types
- CD/DVD -> Use disk image (iso)
- Hard Disk -> Bus/Device IDE 0 , Storage local-lvm, Cache Default (no cache)
- CPU -> Type Default(kvm64)
- Memory -> Auto allocate and input the desired range
- Network -> Bridged mode , Model VirtIO (paravirtualized)
Again these are suggested defaults and depend on the machine and the network plan we have in mind.
Initial Setup
Still in Proxmox GUI , go to the new VM’s console, login as root and type:
and answer the questions of the wizard,including changing the root password.Alpine setup is possibly the easiest linux setup.Defaults are fine except in the disk creation.After selecting disk (sda) and purpose (sys), at least for this simple use case, type (y) in the warning:erase the above disk and continue ? question.
Alpine will prepare the portion of the hard disk allocated to our VM and will ask to reboot.
After rebooting the vm, from the proxmox console of the Alpine VM create a group and a user so we can ssh to the vm remotely from the terminal of our choice and not be bound to the builtin console of the proxmox GUI anymore:
We use docker group name on purpose because it will be needed later.
Now add the user in sudoers file to be able to execute all commands:
add anywhere this line:
Now press ESC and :wq to save and exit the editor. :q! to exit without saving.
Note:If for any reason we need to ssh as root,from proxmox GUI console:
and replace #permitrootlogin no with permitrootlogin yes .Save and exit the editor.Then restart ssh service:
Now we can leave the proxmox console and ssh to the server from another machine:
Install qemu-guest-agent [optional]
Install Docker Engine
We need to add the community repository in alpine that contains docker:
Now update the repositories, check that docker is present in apk and install:
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.
Table of Contents
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 Kubernetes, Docker, Docker 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
How to setup Docker on Alpine Linux
Setup Docker
First, update the repository index:
Next, run the following command to install Docker on Alpine Linux:
To start the Docker daemon at boot:
To check the status of Docker, run:
Setup Docker-Compose
To install Docker Compose, run:
Setup Portainer
To install Portainer:
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:
You can list all available options with Docker by running the following command:
You should see the complete list of docker sub-commands in the following output:
Run ‘docker COMMAND –help’ for more information on a command.
You can also display more information about Docker using the following command:
You should see the following output:
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:
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:
You can list your downloaded image by running the following command:
You should see all the downloaded images in the following output:
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:
This command will bring inside the docker container as shown below:
Here, you can run any command in the container. For example, run the following command to update the package repository:
You can exit from the container with the following command:
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:
Once the container is started, you should see the following output:
You can now list the running containers with the following command:
You should see your Nginx container in the following output:
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:
Next, run the Nginx container and mount the /volume directory to the Nginx container on the /var/www/html directory.
Next, connect to the Nginx container with the following command:
Then, create a file named test.html inside /var/www/html directory:
Now, exit from the Nginx container with the following command:
Now, run the following command to list the available files in the /volume directory:
You should see the file named test.html, which you have created inside the Nginx container:
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:
To copy a file named /etc/fstab from the Nginx container to the host system, run the following command:
Start, stop and pause the container
To stop the Nginx container, run the following command:
To start the Nginx container, run the following command:
You can also pause the Nginx container using the following command:
To stop all running containers, run the following command:
Remove Docker container
To remove a container, you will need to stop it first.
For example, stop the Nginx container with the following command:
Next, remove the Nginx container using the following command:
If you want to remove all the stopped containers, run the following command:
Remove Docker images
To remove the Docker images, list all images in your system with the following command:
You should see the following output:
Now, remove the Nginx image with the following command:
You should see the following output if any container uses the image:
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:
You will be asked to confirm before removing the image as shown below:
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
Sources
About the Authors
Anto’s editorial team loves the cloud as much as you! Each member of Anto’s editorial team is a Cloud expert in their own right. Anto Online takes great pride in helping fellow Cloud enthusiasts. Let us know if you have an excellent idea for the next topic! Contact Anto Online if you want to contribute.
Support the Cause
Support Anto Online and buy us a coffee. Anything is possible with coffee and code.
Related Posts
Update Ubuntu using Apt & Cron
October 11, 2022 October 11, 2022
Set up CrowdSec to protect your WordPress site
June 10, 2022 June 10, 2022
PHP: How to fix the “Call to undefined function curl_init()” error
November 10, 2021 June 11, 2022
About Anto Online
Having started his career in 1999 as a Desktop Support Engineer, Anto soon changed paths and became a developer. After several years of development experience, he transitioned into a consultant. As an enterprise application consultant for a leading SaaS software provider, Anto specializes in AWS’s serverless technologies. By day, Anto focuses on helping customers leverage the power of serverless technologies. By night, he indulges his passion for cloud computing by playing with Python and trying out things that are currently beyond the scope of his work. Sometimes Anto needs help as there are not enough hours at night. So Anto relies on a team of fellow Cloud enthusiasts to help him out. Each one is a Cloud expert in their own right, and Anto takes great pride in helping them learn and grow.
Leave a Reply
You must be logged in to post a comment.
This site uses Akismet to reduce spam. Learn how your comment data is processed.