Alpine linux docker run

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 ,

  1. The Docker client contacts the Docker daemon
  2. 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)
  3. The Docker daemon creates the container and then runs a command in that container.
  4. 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

Источник

Docker

Contents

Installation

The Docker package is in the ‘Community’ repository. See Alpine_Linux_package_management how to add a repository.

Connecting to the Docker daemon through its socket requires you to add yourself to the `docker` group.

To start the Docker daemon at boot, see Alpine_Linux_Init_System.

For more information, have a look at the corresponding Github issue.

This weakening of security is not necessary to do with Alpine 3.4.x and Docker 1.12 as of August 2016.

Docker Compose

‘docker-compose’ is in the ‘Community’ repository starting with Alpine Linux 3.10.

For older releases:

To install docker-compose, first install pip:

Isolate containers with a user namespace

add to /etc/docker/daemon.json

You may also consider these options : ‘

You’ll find all possible configurations here[1].

Example: How to install docker from Arch

«WARNING: No limit support»

You might encounter this message when executing docker info . To correct this situation, we have to enable the cgroup_enable=memory swapaccount=1

Alpine 3.8

It may not have been the case before, but with Alpine 3.8, you must config cgroups properly

Warning: This seems not to work with Alpine 3.9 and Docker 18.06. Follow the instructions for grub or extlinux below instead.

If you use Grub, add the cgroup condition into /etc/default/grub , then upgrade your grub

Extlinux

With Extlinux, you add the cgroup condition, but inside of /etc/update-extlinux.conf

then update the config and reboot

How to use docker

The best documentation on using Docker and creating containers is at the main docker site. Adding anything to it here would be redundant.

If you create an account at docker.com, you can browse through user images and learn from the syntax in contributed dockerfiles.

Official Docker image files are denoted on the website by a blue ribbon.

Источник

Creating an Alpine Docker Container

Alpine is a Linux Distribution. Docker provides you with the low sized (only 5 MB) Alpine Linux Image. The Alpine Linux Docker Image has advantages over the Ubuntu Image because of its relatively lower size and it provides almost all the functionalities that an Ubuntu Image can. In this article, we will see how to build an Alpine Linux Image. We will try to install MySQL client, Python 3, and Firefox inside the Alpine Linux Docker Container as well.

To create the Alpine Docker Container follow the below steps:

Step 1: Pull and Run the Alpine Image

To run the Alpine Image Docker Container, you can use the Docker run command.

Running the Alpine Container

Once the Image is loaded, it opens up the shell for you automatically.

Step 2: Install Python 3

To install python 3 inside the Alpine Container, you can use the apk add command inside the shell.

Installing Python 3

Step 3: Install MySQL inside the Container

You can install the My-SQL client using the following command.

Step 4: Install Firefox

To install Firefox inside the Container, you can use the following command.

Step 5: Commit the changes in the Image

You need the Container Id to commit the changes in the Image.

To find the Container ID, use this command.

Copy the Container ID and paste it in this command.

Источник

How to Use the Alpine Docker Official Image

With its container-friendly design, the Alpine Docker Official Image (DOI) helps developers build and deploy lightweight, cross-platform applications. It’s based on Alpine Linux which debuted in 2005, making it one of today’s newest major Linux distros.

While some developers express security concerns when using relatively newer images, Alpine has earned a solid reputation. Developers favor Alpine for the following reasons:

  • It has a smaller footprint, and therefore a smaller attack surface (even evading 2014’s ShellShock Bash exploit!).
  • It takes up less disk space.
  • It offers a strong base for customization.
  • It’s built with simplicity in mind.

In fact, the Alpine DOI is one of our most popular container images on Docker Hub. To help you get started, we’ll discuss this image in greater detail and how to use the Alpine Docker Official Image with your next project. Plus, we’ll explore using Alpine to grab the slimmest image possible. Let’s dive in!

In this tutorial:

What is the Alpine Docker Official Image?

The Alpine DOI is a building block for Alpine Linux Docker containers. It’s an executable software package that tells Docker and your application how to behave. The image includes source code, libraries, tools, and other core dependencies that your application needs. These components help Alpine Linux function while enabling developer-centric features.

The Alpine Docker Official Image differs from other Linux-based images in a few ways. First, Alpine is based on the musl libc implementation of the C standard library — and uses BusyBox instead of GNU coreutils. While GNU packages many Linux-friendly programs together, BusyBox bundles a smaller number of core functions within one executable.

While our Ubuntu and Debian images leverage glibc and coreutils, these alternatives are comparatively lightweight and resource-friendly, containing fewer extensions and less bloat.

As a result, Alpine appeals to developers who don’t need uncompromising compatibility or functionality from their image. Our Alpine DOI is also user-friendly and straightforward since there are fewer moving parts.

Alpine Linux performs well on resource-limited devices, which is fitting for developing simple applications or spinning up servers. Your containers will consume less RAM and less storage space.

The Alpine Docker Official Image also offers the following features:

  • The robust apk package manager
  • A rapid, consistent development-and-release cycle vs. other Linux distributions
  • Multiple supported tags and architectures, like amd64 , arm/v6+ , arm64 , and ppc64le

Multi-arch support lets you run Alpine on desktops, mobile devices, rack-mounted servers, Raspberry Pis, and even newer M-series Macs. Overall, Alpine pairs well with a wide variety of embedded systems.

These are only some of the advantages to using the Alpine DOI. Next, we’ll cover how to harness the image for your application.

When to use Alpine

You may be interested in using Alpine, but find yourself asking, “When should I use it?” Containerized Alpine shines in some key areas:

  • Creating servers
  • Router-based networking
  • Development/testing environments

While there are some other uses for Alpine, most projects will fall under these two categories. Overall, our Alpine container image excels in situations where space savings and security are critical.

How to run Alpine in Docker

Before getting started, download Docker Desktop and then install it. Docker Desktop is built upon Docker Engine and bundles together the Docker CLI, Docker Compose, and other core components. Launching Docker Desktop also lets you use Docker CLI commands (which we’ll get into later). Finally, the included Docker Dashboard will help you visually manage your images and containers.

After completing these steps, you’re ready to Dockerize Alpine!

Note: For Linux users, Docker will still work perfectly fine if you have it installed externally on a server, or through your distro’s package manager. However, Docker Desktop for Linux does save time and effort by bundling all necessary components together — while aiding productivity through its user-friendly GUI.

Use a quick pull command

You’ll have to first pull the Alpine Docker Official Image before using it for your project. The fastest method involves running docker pull alpine from your terminal. This grabs the alpine:latest image (the most current available version) from Docker Hub and downloads it locally on your machine:

Your terminal output should show when your pull is complete — and which alpine version you’ve downloaded. You can also confirm this within Docker Desktop. Navigate to the Images tab from the left sidebar. And a list of downloaded images will populate on the right. You’ll see your alpine image, tag, and its minuscule (yes, you saw that right) 5.29 MB size:

Other Linux distro images like Ubuntu, Debian, and Fedora are many, many times larger than Alpine.

That’s a quick introduction to using the Alpine Official Image alongside Docker Desktop. But it’s important to remember that every Alpine DOI version originates from a Dockerfile . This plain-text file contains instructions that tell Docker how to build an image layer by layer. Check out the Alpine Linux GitHub repository for more Dockerfile examples.

Next up, we’ll cover the significance of these Dockerfiles to Alpine Linux, some CLI-based workflows, and other key information.

Build your Dockerfile

Because Alpine is a standard base for container images, we recommend building on top of it within a Dockerfile . Specify your preferred alpine image tag and add instructions to create this file. Our example takes alpine:3.14 and runs an executable mysql client with it:

In this case, we’re starting from a slim base image and adding our mysql-client using Alpine’s standard package manager. Overall, this lets us run commands against our MySQL database from within our application.

This is just one of the many ways to get your Alpine DOI up and running. In particular, Alpine is well-suited to server builds. To see this in action, check out Kathleen Juell’s presentation on serving static content with Docker Compose, Next.js, and NGINX. Navigate to timestamp 7:07 within the embedded video.

The Alpine Official Image has a close relationship with other technologies (something that other images lack). Many of our Docker Official Images support -alpine tags. For instance, our earlier example of serving static content leverages the node:16-alpine image as a builder .

This relationship makes Alpine and multi-stage builds an ideal pairing. Since the primary goal of a multi-stage build is to reduce your final image size, we recommend starting with one of the slimmest Docker Official Images.

Grabbing the slimmest possible image

Pulling an -alpine version of a given image typically yields the slimmest result. You can do this using our earlier docker pull [image] command. Or you can create a Dockerfile and specify this image version — while leaving room for customization with added instructions.

In either case, here are some results using a few of our most popular images. You can see how image sizes change with these tags:

Image tag Image size image:[version number]-alpine size
python:3.9.13 867.66 MB 46.71 MB
node:18.8.0 939.71 MB 164.38 MB
nginx:1.23.1 134.51 MB 22.13 MB

We’ve used the :latest tag since this is the default image tag Docker grabs from Docker Hub. As shown above with Python, pulling the -alpine image version reduces its footprint by nearly 95%!

From here, the build process (when working from a Dockerfile ) becomes much faster. Applications based on slimmer images spin up quicker. You’ll also notice that docker pull and various docker run commands execute swifter with -alpine images.

However, remember that you’ll likely have to use this tag with a specified version number for your parent image. Running docker pull python-alpine or docker pull python:latest-alpine won’t work. Docker will alert you that the image isn’t found, the repo doesn’t exist, the command is invalid, or login information is required. This applies to any image.

Get up and running with Alpine today

The Alpine Docker Official Image shines thanks to its simplicity and small size. It’s a fantastic base image — perhaps the most popular amongst Docker users — and offers plenty of room for customization. Alpine is arguably the most user-friendly, containerized Linux distro. We’ve tackled how to use the Alpine Official Image, and showed you how to get the most from it.

Want to use Alpine for your next application or server? Pull the Alpine Official Image today to jumpstart your build process. You can also learn more about supported tags on Docker Hub.

Источник

Читайте также:  Как перезапустить apache2 linux
Поделиться с друзьями
КомпСовет
Adblock
detector