Search code examples
lambdapython-3.8alpine-linuxsam

How do I install a specific version of python on alpine linux (python3.8)?


Currently I'm using a dind configuration in gitlab
This dind works for me to deploy a dockerized lambda function through SAM.

This is my before script

   - apk add --no-cache curl jq
   - apk add --no-cache python3 python3-dev py3-setuptools
   - apk add py3-pip
   - apk add --no-cache build-base g++ make cmake unzip curl-dev
   - apk add --no-cache autoconf automake libtool libexecinfo-dev
   - apk add --no-cache git
   - pip3 install --no-cache --upgrade wheel
   - pip3 install awscli --upgrade
   - pip3 install aws-sam-cli --upgrade

I'm having troubles because the lambda function python version is 3.8 an sam builds complains that the version the docker gitlab setup is different and doesn't find 3.8


Solution

  • Finally I solve it Probably I lacked context I'm doing a gitlab.yaml file to build, test and deploy my application in AWS thorugh SAM.

    Since one of my lambda functions is a dockerized lambda function I nee sam to be able to access docker as a command so it can run docker pull and docker build
    The other lambda functions that I have use python3.8 as runtime so the docker version I was using as a base image pointed to https://dl-cdn.alpinelinux.org/alpine/v3.15/main/x86_64/ so everyt time something was installed with apk the version was 3.15 which has python3.10. A solution to this is use: image: docker:19.03.15-alpine3.13 as a base image with the service dind like this:

    image: docker:19.03.15-alpine3.13
    ## This will run a Docker daemon in a container (Docker-In-Docker), which will
    ## be available at thedockerhost:2375. If you make e.g. port 5000 public in Docker
    ## (`docker run -p 5000:5000 yourimage`) it will be exposed at thedockerhost:5000.
    services:
      - name: docker:dind
        alias: thedockerhost
    
    variables:
      # Tell docker CLI how to talk to Docker daemon; see
      # https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#use-docker-in-docker-executor
      DOCKER_HOST: tcp://thedockerhost:2375/
      # DOCKER_HOST: tcp://docker:2375/
      # Use the overlays driver for improved performance:
      DOCKER_DRIVER: overlay2
      DOCKER_TLS_CERTDIR: ""
      REPOSITORY_URL: ######.dkr.ecr.region.amazonaws.com/
      REGION: us-east-2
    
    deploy:
      stage: deploy
      before_script:
        - apk add --no-cache python3
        - apk add --no-cache curl jq
        - apk add --no-cache python3-dev py3-setuptools
        - apk add --no-cache py3-pip
        - apk add --no-cache py-pip
        - apk add --no-cache build-base g++ make cmake unzip curl-dev
        - apk add --no-cache autoconf automake libtool libexecinfo-dev
        - apk add --no-cache git
        - pip3 install --no-cache --upgrade wheel
        - pip3 install --no-cache awscli --upgrade
        - pip3 install --no-cache aws-sam-cli --upgrade
      script:
        - aws ecr get-login-password --region ${REGION} | docker login --username AWS --password-stdin ${REPOSITORY_URL}
        - sam build
        - sam deploy --no-confirm-changeset --no-fail-on-empty-changeset
      only:
        - master
    
    

    This alphine version points to:
    https://dl-cdn.alpinelinux.org/alpine/v3.13/main/x86_64/APKINDEX.tar.gz
    which installs python3.8 and now sam is able to package/build the rest of the lambda functions.