Siddhant
Siddhant

Reputation: 581

Failure to run an executable inside a Docker image: Not Found error

I'm trying to build a simple docker image with tfswitch installed. I tried it running locally on my Mac OSX by running curl and executing tfswitch 0.12.24 to install v0.12.24 of terraform. It works perfectly fine there. However, I need to create a Docker image out of it and it keeps failing. The tfswitch seems to be added to the image but the RUN /usr/local/bin/tfswitch 0.12.24 fails with an error No such file or directory. I went through various posts online, which recommended to change permissions and adding usr/local/bin to the Path. I tried all that. The PATH also has usr/local/bin added to it by default. Not sure what's going wrong. Below is the docker image which will change permissions as well, echo the path and also run which tfswitch to identify the location of tfswitch. Everything seems to alright to me. Not sure what's wrong. Any idea what could be wrong?

FROM ruby:2.4.1-alpine AS Dummy_Image
RUN apk add --update --no-cache curl
RUN sh -c "$(curl -L https://raw.githubusercontent.com/warrensbox/terraform-switcher/release/install.sh)" 
RUN echo "${PATH}"
RUN which tfswitch
RUN chmod 777 /usr/local/bin
RUN ls -l /usr/local/bin
RUN /usr/local/bin/tfswitch 0.12.24

I tried with RUN "/usr/local/bin/tfswitch 0.12.24", RUN "tfswitch 0.12.24" as well as RUN tfswitch 0.12.24, but doesn't work either way. Tried with chmod on the executable only as well.

Upvotes: 1

Views: 1698

Answers (3)

Cognitiaclaeves
Cognitiaclaeves

Reputation: 878

This might be reviving an old thread, but I wanted to point out there might be a more succinct way to do this now. I solved this apparent issue by adding the libc6-compat package to the node:alpine container.

(It's actually meant to be a build container that includes terragrunt, terraform, cdktf, and tfswitch), but here's the relevant info:

FROM node:alpine
RUN apk add --no-cache git curl docker-cli unzip libc6-compat
# install cdktf
# install terraform
# install terragrunt
# install tfswitch:
curl -L https://raw.githubusercontent.com/warrensbox/terraform-switcher/release/install.sh | sh

Upvotes: 2

reubit
reubit

Reputation: 31

I just ran into the same issue whilst trying to achieve the same thing with tfswitch inside docker. We're also making the switch from using a statically defined version of terraform in our CI/CD base image, to allowing dynamic version selection at deploy time.

This issue is specific to alpine based images. The tfswitch binary is dynamically linked to glibc, which doesn't come packaged with alpine (uses uclibc as a light-weight alternative).

I ended up adding this glibc alpine package to the image: https://github.com/sgerrand/alpine-pkg-glibc/

Specifically, I added this to my Dockerfile before installing tfswitch:

# Install glibc (dependency for tfswitch)
RUN wget -q -O /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub \
  && wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.32-r0/glibc-2.32-r0.apk \
  && apk add glibc-2.32-r0.apk

Hope this helps someone! Was tearing my hair out trying to work out what was going on.

Upvotes: 3

David Maze
David Maze

Reputation: 158908

Since a Docker container is an isolated environment, you don't need "switcher" or "version manager" type tools. Hashicorp distributes Terraform as a compiled (Go) binary, so you can just download it and run it; you do not need it to be in a Ruby base image.

There is an official hashicorp/terraform image and you might consider just using that, instead of building your own. Since it's a single statically-linked binary, you can also just download and run it without Docker (and given the user-provided configuration and local state files, and some cases of implicitly-provided credentials from $HOME, this might be much easier to do).

If you do want to build your own, you can just download the binary:

ARG terraform_version=0.12.26
RUN cd /tmp \
 && curl -LO https://releases.hashicorp.com/terraform/${terraform_version}/terraform_${terraform_version}_linux_amd64.zip \
 && unzip terraform_${terraform_version}_linux_amd64.zip \
 && mv terraform /usr/local/bin \
 && rm terraform_${terraform_version}_linux_amd64.zip

Upvotes: 1

Related Questions