1

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.

2
  • Is 0.12.24 an argument for tfswitch? You have a space between them. Also, you need chmod 777 /usr/local/bin/tfswitch rather than the directory. Commented May 28, 2020 at 10:53
  • yeah, it's an argument to tfswitch. I tried putting the whole command under quotes as well. Doesn't work either way. Tried doing chmod on the executable instead of directory as well Commented May 28, 2020 at 10:56

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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 

Comments

1

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 

2 Comments

So, using the terraform image was what we went initially with. However, our scenario is to dynamically change the version of terraform that we use within the container without having to build multiple image with different versions. That's when we came across tfswitcher and decided to try that out.
I agree a Ruby base image is not required in the sample Dockerfile in the question. We basically be integrating few of the ruby components in this image in addition to terraform. That's when we decided to use a ruby base image

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.