Which Python variant to use as a base image in doc

2019-08-20 08:24发布

问题:

I am using Python_onbuild as a base image in the dockerfile as below. However, it is causing my commands to be repeated by invalidating the cache whenever I make a change in the source file.

FROM python:2.7.13-onbuild
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

RUN echo "Test Cache"
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install --assume-yes apt-utils
RUN apt-get update && apt-get install -y curl
RUN apt-get update && apt-get install -y unzip
RUN curl -o - url

Build logs:

Sending build context to Docker daemon  239.1kB
Step 1/6 : FROM python:2.7.13-onbuild
# Executing 3 build triggers...
Step 1/1 : COPY requirements.txt /usr/src/app/
 ---> Using cache
Step 1/1 : RUN pip install --no-cache-dir -r requirements.txt
 ---> Using cache
Step 1/1 : COPY . /usr/src/app
 ---> 13e927036649

This 3rd step of copying (Step 1/1 : COPY . /usr/src/app) is making the rest of the commands repeated whenever I make a change in any files in the working directory. I think this an ONBUILD command from the base image. If that true then what is the alternative base image in such situation? Should I use Python:?

I want more control on the requirement installation as as well as the copying process because I have to download a 3.6GB file which I do not want to repeat every time I build the docker.

Note: This specific base image was chose by someone else and I am building on top of some existing work.

回答1:

The base image you're using is described in this Dockerfile.
As you can see, it's based on python:2.7. So if you don't need the ONBUILD instructions, just use this image directly or the one that can fit your needs: you can find a list on the python image on Docker Hub with links to all the corresponding Dockerfiles.



回答2:

You can use base ubuntu image and install python or you can use other images with python installed. you need move the source copying part after the requirement installation part so that you don't have to do the installation if you change the code. This is what my docker file looks like.

############################################################
# Dockerfile to run a Django-based web application
# Based on an Ubuntu Image
############################################################

# Set the base image to use to Ubuntu
FROM ubuntu:14.04

ENV DOCKYARD_SRC=app
ENV DOCKYARD_SRVHOME=/app

# Update the default application repository sources list
RUN apt-get update 
RUN apt-get install -y vim
RUN apt-get install -y python-dev
RUN apt-get install -y python-pip
RUN apt-get install -y build-essential
RUN apt-get -y install wget
RUN apt-get -y install php5 libapache2-mod-php5 php5-mcrypt
RUN apt-get -y install curl libcurl3 libcurl3-dev php5-curl
RUN pip install uwsgi

RUN mkdir $DOCKYARD_SRC

COPY ./requirements.txt $DOCKYARD_SRVHOME/requirements.txt
RUN pip install -r $DOCKYARD_SRVHOME/requirements.txt
COPY . $DOCKYARD_SRVHOME

EXPOSE 8001 8000

WORKDIR $DOCKYARD_SRVHOME

RUN chmod +x entrypoint.sh

CMD ["./entrypoint.sh"]