Docker Alpine Install Curl

2/12/2022by admin
  • Alpine: Install cURL - ShellHacks › See more all of the best images on Images. Posted: (5 days ago) May 25, 2020 A minimal Docker image based on Alpine Linux has only 5 MB in size, but a lot of tools common for Linux distributions (e.g. Curl) are not installed by default.
  • Install Compose on Linux systems. On Linux, you can download the Docker Compose binary from the Compose repository release page on GitHub.Follow the instructions from the link, which involve running the curl command in your terminal to download the binaries.
  • We can also enable remote access using a TCP socket. For Linux distributions that come with systemd we need to customize the Docker service unit. For other Linux distros, we need to customize the daemon.json usually located /etc/docker.

PhantomJS on Alpine (Docker). GitHub Gist: instantly share code, notes, and snippets.

What are we going to learn?

The goal is to help you achieve practical, working knowledge of Docker so you can build containerized micro-services in C# with relative ease. We are going to use .Net core as it is cross-platform and well supported on Linux-based Docker containers. Having said that, the Windows-based Docker containers exist too, but the haven’t got much traction.

Source code

The source code is available on github

What tools do I need?

Ironically, Windows is not an absolute prerequisite for this exercise. One can build C#-powered containerized micro-services on Mac or Linux too.Windows will work too, of course.

  • First, you need Docker installed. Follow the link and install Docker desktop for your system.
  • Second, you need the .Net core SDK. Install the latest version of .Net core SDK, which is 3.1 at the time of writing.
  • You need a text editor. If you are not settled, consider Visual Studio Code It is cross-platform, fast and it works great with C# and Docker.
  • Many of the activities will be command-line based. Use the shell available to you. To keep things as cross-platform as possible, I am going to use Git Bash on Windows. If you don’t know, Visual Studio Code has great support for working with the command line.

Sanity check

Docker Nginx Alpine Install Curl

In the command line run


If you get some sane answer in both cases, you are all set.

Your first encounter with Docker


You should see current time displayed in the shell

What just happened?

by running this command you instructed docker to start a container based on the alpine image, execute the command date and display its output in the shell. If you did not have the alpine image on your box (most likely), docker downloaded it from the Dockerhub, the public repository of well known Docker images.

Containers, images… I am confused.

You can think about it this way: if an Image is analogous to an ‘.exe’ file, container is the process which runs once you start this .exe file.

Ok, what is this alpine thing, anyway?

Alpine is a Linux distribution which is quite popular in the Docker community, because it is very minimalistic and small.

Your turn

instead of the date try to run other commands. pwd, whoami are good examples

Explore the Alpine container

This time start the container with the following:

the it flag indicates to start the container in the interactive mode and run sh command which is Alpine’s default shell. You will end up inside the container. Look around, explore what is there using ls and and cd commands. cat will display the content of the text file, for example

If you are familiar with the vi editor you can create or edit some files too.By default, alpine does not have any fancy tools to access Internet.This can be mitigated:

apk is Alpine’s package installer, you can install various apps with it. Now, try to do

You should get the Microsoft’s home page, in the text format, of course.

When you done exploring, exit the container:

You should get back to your normal shell

Your turn

The Alpine image is supposed to be small, But how small is it, exactly? Hint: docker images gives the list of images available on your system.

C# console application

Now we are going to replicate the experience we had running date in the docker container, but this time with a C# app.Let’s create the app:

This scaffolds a simple .net core command line application.Go to the Program.cs and replace the line


Sanity check

This should display the current UTC time.

Dockerize C# command line application

Now, let’s put our application to the Docker image. For that, we’ll need a Dockerfile. Dockerfile is essentially a list of instructions which Docker is supposed to follow in order to build your image.create a file Dockerfile` and put the following line into it:

This instructs Docker to build our image on top of predefined Alpine-based image but which has .net core sdk 3.1 installedDocker images are layered, every new layer adds additional functionality. We could have start from the bare Alpine image and install the .net core sdk ourselves, that is pretty straightforward but quite tedious. So we’ll start with the base image Microsoft provides us.Now we can try to build the image

the -t parameter specifies the name of the image, which is going to be csharpdate in this case. The argument .(dot) instructs Docker where to look for the Dockerfile, which is the current directory in our case.After few seconds the image is going to be buit. As mentioned before, it is layered and has Alpine underneath, so we can explore it as we did before:

We end up in the familiar Alpine shell. But this time it has also .net core tools installed, for instance, you can run dotnet.

Anyway, the next task is to copy the sources into the docker image and build themAppend the following lines to the Dockerfile

The WORKDIR directive specifies what is the current directory in the Docker image, /app in our case. It will be created if needed.The COPY directive instructs Docker to copy our C# source file and associated .csproject file into the image’s current directoryAfter that you will have everything you need to build the final binary in image.

Your turn

Rebuild the image, launch the container and verify that the files are indeed there. Also verify that the current directory is /app.

The last step is to actually build the application.Append to the Dockerfile:

This builds the C# project in the release mode and places the output in the build subdirectory of the current directory, that is into /app/build

Once the image is rebuilt we can launch our program inside the container:

this will print again the current UTC time, but this time coming from our C# program!

Specifying the full path to the binary we want to run every time is quite tedious. Docker has a directive which allows to designate some command line as the default.Append to your DOCKERFILE:

Rebuild the image and now you can launch the binary simply running:

Your turn

Experiment: Try to modify the code in the Program.cs, try to specify different build flags and so on

C# microservice

Ok, containerizing a command line application is cool, but it does not look like a microservice, does it?In this part we are going to create a simple service which returns the current UTC time from the REST endpoint.We are going to use Asp.Net Core for that. But don’t be scared! We are not going to use most of the Asp.Net Core machinery to get our service working. The secret is that, in fact, self-hosted Asp.Net Core applications are, in fact, command line applications!So let’s create a directory datems at the same level as the date directory and initialize a .Net Core command line application there:

Docker Alpine Install Curl Tool

We will need to bring Asp.Net Core -specific dependencies into the project. This is hush-hush, but the simplest way to do int is to open the datems.csproj and replace the line


Don’t tell anyone, let them suffer through the official tutorials.

If we run the project

it will still happily run and display the ubiquitous “Hello World!”.

Asp.Net Core is actually a pretty cool and rich framework; if you build Web apps or Rest services on a daily basis, I encourage to study it deeper. But we will use a bare minimum of its features to get our microservice working. So don’t expect best Asp.Net practices here.

We’ll need to use some namespaces. Here is the full list, feel free to add these into the Program.cs

To start a Asp.Net Core app we need to:

  • create an instance of IHostBuilder
  • ask it to Build that host
  • finally to run it.

Install Curl Mac

We are going to do all these steps in our Main method:

There is some Asp.Net Core magic here. Feel free to explore that on your own. For example, we indicate that we are going to listen on all network interfaces on port 5000.The key here is the webBuilder.UseStartup<Program> call.This indicates that our Program class defines the Asp.Net Core pipeline.

In order satisfy this Asp.Net Core requirement, we need to implement two methods, ConfigureServices and ConfigureThe simple ConfigureServices looks like this:

In here we tell that we want to use AspNet Core Controllers which are going to serve as our REST endpoints.the Configure looks like this:

We simply tell Asp.Net Core that we will need to route the requests depending on the URL, and that the default route is just fine.

Sanity Check


If everything is ok, the app should start and listen for the Http request on the default port 5000However, if we try to navigate in the browser to http://localhost:5000 we’ll be greeted with tho “not found” error. What gives?

The answer is that we asked to wire up the default route which, by convention implies the “Home” controller with the “Index” action.

The controller

Let’s implement the required controller. Nothing fancy.Create the class HomeController (you can use the same Program.cs file):

We are saying, return the http Ok response (200) with the payload “42”.

Another sanity check

Restart the app and navigate in the browser to http://localhost:5000You should see the “42” showing up on the page.

return the Json response

The last step is to return an object formatted as JSON, instead of the answer to life, universe and everything.Let’s create a response class:

and return that from the controller:

Restart the app, refresh the browser and you should see the current time formatted as JSON.

Our “real” microservice is pretty much ready.

Build Docker image for the microservice

Ironically our Dockerfile for the command line application should mostly work for the microservice, so you can just copy the Dockerfile from the date directory to the datems the following tweaks in that Dockefile:

  • correct the C# project file name from date.csproj to datems.csproj
  • correct the app dll name in the CMD directive from date.dll to datems.dll

Build the new Docker image in the datems directory, this time calling the image csharpms:

Let’s start the container

The -d flag indicates to start the container in detached mode, so it detaches from the console

The Docker should start the container and print its id, a long hexadecimal numberlet’s dive into the running container.Run

Hint: you don’t need to provide the full container id. A few of initial hexadecimal digits should be enough for the Docker to figure out which container you have in mind.For example:

Your id will be obviously different.You should end up inside the running container!check if your app is running:

You should see the something like

This shows that our service is running.Try to see which ports are open:

you should see the port 5000 is listening. This is our microservice!

Your turn

Use curl inside the container to get the answer from the microservice.Reminder, you can install curl in the Alpine based container by running

Well, the microservice seems to listen on port 5000, but how can we reach it, say from our box? Your machine has very different idea what is the port 5000 than the container.

Port mapping

Exit the container and stop it by executing

If you forgot what is the container id, you can list all running containers with

Port mapping in Docker allows to attach a certain port on the machine to a specific port in the container. Let’s say that we want the port 5000 in the container correspond to the port 9090 on your machine.Start the container this way:

Now all tcp/ip traffic to and from port 9090 on our machine will actually end up in the port 5000 in the container

Sanity check

in the browser, navigate to http://localhost:9090. You should get your nice UTC time back.

Shrinking image size with multi-stage builds

docker images will display all images we have on our box, together with their sizes.

If we check the size of the csharpms image it is quite big. On my machine it takes 422 MB. Large image cause higher resources' usage and slow downloads. Can we shrink our image? Sure we can! One of the most important reasons why the image is so bloated is that it contains whole .Net Core 3.1 SDK. Well, we need SDK to build our microservice binary, but we not necessary need it to run it. The leaner image might suffice.

Docker supports so called multi-stage builds when you copy specific files not from the host machine but from another Docker image, possibly unrelated to one we are building.So here is the plan:

  • In the first (or build) stage we will use the SDK image as before to build the binary.
  • The second (the final) will be based on a leaner baser image into which we will copy the binary produced by the build stage.After the final stage is ready, the build stage will be effectively discarded

Here is the resulting Dockerfile:

Rebuild the image and check its size

On my machine it takes 105 MB, 4 time smaller then original. Not bad!

Sanity check

Start the container from the final image

Docker node alpine install curl

check with the browser or curl that it works as well as before.


Our microservice is containerized and ready. Granted, this is a toy microservice with trivial functionality. But now you have skills to build a Docker image with the C# service as complex as you want.

See also

Comments are closed.