CyberPunk
CyberPunk

Reputation: 1447

Error trying to install Python inside a Docker container

I am relatively new to docker. I have an application which I want to containerize.

Below is is my docker file:

FROM ubuntu:16.04

## ENV Variables
ENV PYTHON_VERSION="3.6.5"

# Update and Install packages
RUN apt-get update -y \
 && apt-get install -y \
 curl \
 wget \
 tar

# Install Python 3.6.5
RUN wget https://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tar.xz \
    && tar -xvf Python-${PYTHON_VERSION}.tar.xz \
    && cd Python-${PYTHON_VERSION} \
    && ./configure \
    && make altinstall \
    && cd / \
    && rm -rf Python-${PYTHON_VERSION}

# Install Google Cloud SDK

# Downloading gcloud package
RUN curl https://dl.google.com/dl/cloudsdk/release/google-cloud-sdk.tar.gz > /tmp/google-cloud-sdk.tar.gz

# Installing the package
RUN mkdir -p /usr/local/gcloud \
  && tar -C /usr/local/gcloud -xvf /tmp/google-cloud-sdk.tar.gz \
  && /usr/local/gcloud/google-cloud-sdk/install.sh

# Adding the package path to local
ENV PATH $PATH:/usr/local/gcloud/google-cloud-sdk/bin

I am trying to install python3.6.5 version but I am receiving the following error.

020-01-09 17:26:13 (107 KB/s) - 'Python-3.6.5.tar.xz' saved [17049912/17049912]

tar (child): xz: Cannot exec: No such file or directory

tar (child): Error is not recoverable: exiting now

tar: Child returned status 2

tar: Error is not recoverable: exiting now

The command '/bin/sh -c wget https://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tar.xz && tar -xvf Python-${PYTHON_VERSION}.tar.xz && cd Python-${PYTHON_VERSION} && ./configure && make altinstall && cd / && rm -rf Python-${PYTHON_VERSION}' returned a non-zero code: 2

Upvotes: 1

Views: 1048

Answers (2)

Zeitounator
Zeitounator

Reputation: 44799

Decompressing an .xz file requires the xz binary which under ubuntu is provided by the package xz-utils So You have to instal xz-utils on your image prior to decompressing an .xz file.

You can add this to your previous apt-get install run:

# Update and Install packages
RUN apt-get update -y \
 && apt-get install -y \
 curl \
 wget \
 tar \
 xz-utils

This should fix the following call to tar in the next RUN expression

Upvotes: 3

Itamar Turner-Trauring
Itamar Turner-Trauring

Reputation: 3900

Instead of trying to install Python, just start with a base image that has Python preinstalled, e.g. python:3.6-buster. This image is based on Debian Buster, which was released in 2019. Since Ubuntu is based on Debian, everything will be pretty similar, and since it's from 2019 (as opposed to Ubuntu 16.04, which is from 2016) you'll get more up-to-date software.

See https://pythonspeed.com/articles/base-image-python-docker-images/ for longer discussion.

Upvotes: 0

Related Questions