Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize Dockerfile to reduce image size #136

Merged
merged 2 commits into from
Sep 16, 2023
Merged

Optimize Dockerfile to reduce image size #136

merged 2 commits into from
Sep 16, 2023

Conversation

jaehee329
Copy link
Contributor

@jaehee329 jaehee329 commented Aug 27, 2023

Description

ref #135

Changed docker base image from ubuntu:20.04 to python:3.8-slim-buster
Reduced the number of layers

Reduced 0.31GB of the image size

image

build time also decreased from 5.5 min to 2 min in my environment (Win10, i5-10400F, 16GB RAM)

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Documentation update
  • Refactoring, Maintenance
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Dockerfile Outdated
@@ -1,26 +1,24 @@
# Copyright (c) 2021 LG Electronics Inc.
# SPDX-License-Identifier: Apache-2.0
FROM ubuntu:20.04
FROM --platform=linux/amd64 python:3.8-slim-buster
Copy link

@jongwooo jongwooo Aug 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To provide for multiple platforms instead of just one, the setting must be excluded from the FROM statement. For more information, see https://docs.docker.com/build/building/multi-platform/

Suggested change
FROM --platform=linux/amd64 python:3.8-slim-buster
FROM python:3.8-slim-buster

Dockerfile Outdated
RUN apt-get clean
RUN apt-get update && apt-get install sudo -y
RUN ln -sf /bin/bash /bin/sh
ENV DEBIAN_FRONTEND=noninteractive

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noninteractive mode is useful when we need zero interaction while installing or upgrading the system via apt. However, setting this environment variable to noninteractive has an adverse effect. Because it is inherited by all images and containers built from the image, effectively changing their behavior.

Suggested change
ENV DEBIAN_FRONTEND=noninteractive

Let's look for an alternative.

Dockerfile Outdated
Comment on lines 8 to 9
apt-get update && \
apt-get install -y sudo build-essential \
Copy link

@jongwooo jongwooo Aug 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
apt-get update && \
apt-get install -y sudo build-essential \
apt-get update && \
apt-get install --no-install-recommends -y \
build-essential \

@jongwooo
Copy link

Consider changing the pull request title and commit message to Optimize Dockerfile to reduce image size.

@jaehee329 jaehee329 changed the title Optimize Dockerfile Optimize Dockerfile to reduce image size Sep 1, 2023
@jaehee329 jaehee329 closed this Sep 11, 2023
@jaehee329 jaehee329 reopened this Sep 11, 2023
@jaehee329
Copy link
Contributor Author

@jongwooo
Thanks for your kind review. Followed your suggestions.

I have a question about using multi-stage build now.
Can you please check this Dockerfile appropriate?
Since it's my first time to use multi-stage build, I'm not sure is it okay to copy only the executable file from builder.

# Copyright (c) 2021 LG Electronics Inc.
# SPDX-License-Identifier: Apache-2.0
FROM python:3.8-slim-buster AS builder

RUN ln -sf /bin/bash /bin/sh && \
    apt-get update && \
    apt-get install --no-install-recommends -y \
    build-essential \
    python3 python3-distutils python3-pip python3-dev python3-magic \
    libxml2-dev libxslt1-dev libhdf5-dev bzip2 xz-utils zlib1g libpopt0 && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

WORKDIR	/app

COPY . /app

RUN pip3 install --upgrade pip && \
    pip3 install . && \
    pip3 install dparse && \
    rm -rf ~/.cache/pip /root/.cache/pipe

FROM python:3.8-slim-buster AS production

COPY --from=builder /usr/local/bin/fosslight_source /usr/local/bin/fosslight_source

ENTRYPOINT ["/usr/local/bin/fosslight_source"]

@soimkim soimkim merged commit 4133d5e into fosslight:main Sep 16, 2023
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants