Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Docker as Package Manager for Linux

DZone's Guide to

Docker as Package Manager for Linux

After using the stock Linux package manager, check out how this developer found a way to use Docker as a project management solution.

· Cloud Zone ·
Free Resource

Discover a centralized approach to monitor your virtual infrastructure, on-premise IT environment, and cloud infrastructure – all on a single platform.

I first tried Linux in 1995 and it was Red Hat Linux distribution version 2.1. It was quite a challenge to set it up to work with Intel 80486DX4. In the 90’s, installing something into Linux took some time to get used to after Windows “anybody *.tar.gz + dependencies."

Red Hat was one of the first to come with integrated package management system RPM that was suppose to solve dependencies. Once you start using it, more and more of the rpm installation's limitations start to creep in, a phenomenon called Dependency hell. Debian was a good alternative with DEB packages.

During the years I played with major Linux distributions Debian, Slackware and derivatives, and Unix FreeBSD, OpenBSD and NetBSD, settling down with Gentoo for a few years, as documentation at the beginning of 2000’s was awesome. In my last 10 years, I am on Arch Linux as my primary desktop and the first and last time I reinstalled Arch on desktop was 5 years ago.

As you start experimenting with new software or never-ending updates, your system will start get cluttered even with the best package managers.

In the last 4 years, I have been using Docker as a container deployment tool and I was wondering whether Docker can run my basic application for the shell. Instead of installing a new application, I will build Docker images and will run it as a standard Linux application.

It turns out you can comfortably run most of the Linux tools as Docker containers. The only problem is that you will need to maintain it yourself.

In this article, we'll cover how to run Amazon AWSCLI, Google Gcloud CLI, Terraform and Packer from Docker images in Linux shell.

The first step is to create a Dockerfile to build the needed images.

Let’s start with AWSCLI Dockerfile.

FROM alpine:latest

# Version https://github.com/aws/aws-cli/releases
ENV AWS_CLI_VERSION 1.15.55

RUN apk --no-cache update && \
    apk --no-cache add \
    python \
    py-pip \
    groff \
    less \
    mailcap \
    jq && \
    pip --no-cache-dir install --upgrade awscli==${AWS_CLI_VERSION} && \
    apk --purge del py-pip && \
    rm /var/cache/apk/*

VOLUME /root/.aws
VOLUME /aws

WORKDIR /aws


Build the Docker image:

docker build -t used_name/aws-cli .


Note: change user_name to your Docker Hub login.

Run a container from the newly-created image.

docker run --rm -tiv $HOME/.aws:/root/.aws -v $(pwd):/aws used_name/aws-cli aws


To make it easy, create an alias in ~/.bashrc file if you use bash.

alias aws = "docker run --rm -tiv $HOME/.aws:/root/.aws -v $(pwd):/aws used_name/aws-cli aws"


Just to be aware of few potential issues.

Running a command inside a container is running as the root and new file created on local system mapped to local file system. For example, $HOME/.aws   during the AWS configuration command will be owned by the root user. The workaround is to create new used during container image build in Dockerfile and assign the GUID and UID of your current Linux user and run the command with created used inside Docker image.

Let’s see the rest of the Dockerfiles and associated aliases.

Gcloud

FROM alpine:latest

# Gcloud version https://github.com/google-cloud-sdk/google-cloud-sdk/releases

ENV CLOUD_SDK_VERSION 202.0.0
ENV PATH /google-cloud-sdk/bin:$PATH

RUN apk --no-cache update && \
    apk --no-cache add \
    curl \
    python \
    py-crcmod \
    libc6-compat \
    openssh-client \
    jq && \
    curl -O https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-${CLOUD_SDK_VERSION}-linux-x86_64.tar.gz && \
    tar xzf google-cloud-sdk-${CLOUD_SDK_VERSION}-linux-x86_64.tar.gz && \
    rm google-cloud-sdk-${CLOUD_SDK_VERSION}-linux-x86_64.tar.gz && \
    ln -s /lib /lib64 && \
    gcloud config set core/disable_usage_reporting true && \
    gcloud config set component_manager/disable_update_check true && \
    gcloud config set metrics/environment github_docker_image && \
    apk --purge del curl && \
    rm /var/cache/apk/*


Terraform

FROM alpine:latest

# Terraform versioin https://releases.hashicorp.com/terraform/

ENV TERRAFORM_VERSION=0.11.7
ENV TERRAFORM_SHA256SUM=6b8ce67647a59b2a3f70199c304abca0ddec0e49fd060944c26f666298e23418

RUN apk --no-cache update && \
    apk --no-cache add \
    ca-certificates \
    curl && \
    curl https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip > terraform_${T
ERRAFORM_VERSION}_linux_amd64.zip && \
    echo "${TERRAFORM_SHA256SUM}  terraform_${TERRAFORM_VERSION}_linux_amd64.zip" > terraform_${TERRAFORM_VERSION}_SHA256SUMS && \
    sha256sum -cs terraform_${TERRAFORM_VERSION}_SHA256SUMS && \
    unzip terraform_${TERRAFORM_VERSION}_linux_amd64.zip -d /bin && \
    rm -f terraform_${TERRAFORM_VERSION}_linux_amd64.zip && \
    apk --purge del curl && \
    rm /var/cache/apk/*


Packer

FROM alpine:latest

# Helm version https://releases.hashicorp.com/packer/

ENV PACKER_VERSION=1.2.4
ENV PACKER_SHA256SUM=258d1baa23498932baede9b40f2eca4ac363b86b32487b36f48f5102630e9fbb

RUN apk --no-cache update && \
    apk --no-cache add \
    ca-certificates \
    curl && \
    curl https://releases.hashicorp.com/packer/${PACKER_VERSION}/packer_${PACKER_VERSION}_linux_amd64.zip > packer_${PACKER_VERSION}_linux_amd64.zip && \
    echo "${PACKER_SHA256SUM}  packer_${PACKER_VERSION}_linux_amd64.zip" > packer_${PACKER_VERSION}_SHA256SUMS && \
    sha256sum -cs packer_${PACKER_VERSION}_SHA256SUMS && \
    unzip packer_${PACKER_VERSION}_linux_amd64.zip -d /bin && \
    rm -f packer_${PACKER_VERSION}_linux_amd64.zip && \
    apk --purge del curl && \
    rm /var/cache/apk/*


Aliases

alias gcloud='docker run -ti --rm  -v ~/:/root/ used_name/gcloud-cli gcloud'
alias gsutil='docker run -ti --rm  -v ~/:/root/ used_name/gcloud-cli gsutil'
alias packer='docker run -it --rm -v $(pwd):/app/ -w /app/ used_name/packer-cli packer'
alias terraform='docker run -it --rm -v ${HOME}:/root -v $(pwd):/app/ -w /app/ used_name/terraform-cli terraform'

You can find all above and additional sources for Kubectl, Helm etc. on my GitHub account.

Or pull Docker images directly from Docker Hub:

docker pull mudrii/aws-cli 
docker pull mudrii/gcloud-beta-cli 
docker pull mudrii/gcloud-cli 
docker pull mudrii/gkubectl-cli 
docker pull mudrii/helm-cli 
docker pull mudrii/kubectl-cli
docker pull mudrii/packer-cli 
docker pull mudrii/terraform-cli 
docker pull mudrii/tflint-cli

Can you run Docker tools as an alias in Windows 10 Power Shell?Yes you can! We need Docker for Windows to be installed.

  • Create anew folder in your %HOME%\Documents\WindowsPowerShell\

  • Create new file in newly created folder Microsoft.PowerShell_profile.ps1

  • Add the below entries:

#docker rm $(docker ps -a -q)
function Remove-StoppedContainers {
 foreach ($id in & docker ps -a -q) { 
 & docker rm $id }
}
# docker inspect — format ‘{{ .NetworkSettings.Networks.nat.IPAddress }}’ <id>
function Get-ContainerIPAddress {
 param (
 [string] $id
 )
 & docker inspect — format ‘{{ .NetworkSettings.Networks.nat.IPAddress }}’ $id
}
#docker run gcloud
function Docker-Run-Gcloud {
 docker run -ti — rm -v ~./.kube:/root/.kube -v ~./.config:/root/.config mudrii/gcloud-cli gcloud $args
}
#docker run terraform
function Docker-Run-Terraform {
 docker run -ti — rm mudrii/terraform-cli $args
}
#docker run packer
function Docker-Run-Packer {
 docker run -ti — rm mudrii/packer-cli $args
}
#docker run packer
function Docker-Run-Azure {
 docker run -v ${HOME}:/root -it — rm azuresdk/azure-cli-python az $args
}
New-Alias drm Remove-StoppedContainers
New-Alias dip Get-ContainerIPAddress
New-Alias gcloud Docker-Run-Gcloud
New-Alias terraform Docker-Run-Terraform
New-Alias packer Docker-Run-Packer
New-Alias az Docker-Run-Azure

Learn how to auto-discover your containers and monitor their performance, capture Docker host and container metrics to allocate host resources, and provision containers.

Topics:
docker ,linux ,powershell ,tutorial ,project management ,cloud

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}