Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Install build-essential in Docker image without having to do `apt-get update`?

I have a Dockerfile which starts with the following:

FROM python:3.7-slim

RUN apt-get update && apt-get install build-essential -y

Problem is, this layer is always changing, so when I run docker build -t <mytag> ., this layer (and subsequent ones) run again, which takes up significant time.

Is there a way to install build-essential in my Dockerfile in a layer which doesn't constantly change?


EDIT: I had a COPY line before RUN, which I removed from the question as I didn't want to include the names of private files, but it didn't occur to me that that was what was making the build re-run from this step.

like image 504
EuRBamarth Avatar asked Sep 30 '19 09:09

EuRBamarth


People also ask

Should you run apt get update in docker?

But previously this advice was given by the official Docker docs' best practices page: Avoid RUN apt-get upgrade …, as many of the “essential” packages from the parent images cannot upgrade inside an unprivileged container. To be clear: RUN commands happen during image build, not during container startup.

Can I use apt get in Dockerfile?

Key takeaways: Set DEBIAN_FRONTEND=noninteractive to prevent some packages from prompting interactive input ( tzdata for example), which leads to indefinite waiting for an user input. Run apt update before the install command to fetch the current package lists.

Should Docker images run as root when installing security updates?

In order to install security updates, you need to be running as root or some other privileged user. And it’s true, you don’t want your Docker image to run as root. But just because you’re installing security updates doesn’t mean your image needs to run as root.

How do I build a docker image without a dockerfile?

docker build [OPTIONS] -f- PATH This syntax can be useful in situations where you want to build an image from a repository that does not contain a Dockerfile, or if you want to build with a custom Dockerfile, without maintaining your own fork of the repository.

How do I use Git with Docker build context?

When building an image using a remote Git repository as build context, Docker performs a git clone of the repository on the local machine, and sends those files as build context to the daemon. This feature requires git to be installed on the host where you run the docker build command.

What is onbuild in dockerfile?

A Docker build executes ONBUILD commands before any command in a child Dockerfile. ONBUILD is useful for images that are going to be built FROM a given image. For example, you would use ONBUILD for a language stack image that builds arbitrary user software written in that language within the Dockerfile, as you can see in Ruby’s ONBUILD variants.


2 Answers

Create a base image which contains:

FROM python:3.7-slim

RUN apt-get update && apt-get install build-essential -y

Build it:

docker build -t mybase .

Then use it for new images:

FROM mybase
like image 126
LinPy Avatar answered Oct 12 '22 17:10

LinPy


"Is there a way to install build-essential in my Dockerfile in a layer which doesn't constantly change?"

Even the question "having some age", is a case in which the construction of the image can be used in multiple stages. The code below uses an example with a Python App.

# first stage
FROM python:3.8 AS builder
COPY requirements.txt .

# install dependencies to the local user directory (eg. /root/.local)
RUN pip install --user -r requirements.txt

# second unnamed stage
FROM python:3.8-slim
WORKDIR /code

# copy only the dependencies installation from the 1st stage image
COPY --from=builder /root/.local /root/.local
COPY ./src .

# update PATH environment variable
ENV PATH=/root/.local:$PATH

CMD [ "python", "./server.py" ]

I hope it will be useful.

src: Containerized Python Development – Part 1

like image 33
Fernando L Couto Avatar answered Oct 12 '22 17:10

Fernando L Couto