Joseph Wahba
Joseph Wahba

Reputation: 750

Docker build image size too big for postgresql 9.6

I'm trying to build a PostgreSql 9.6 docker image that has plv8 extension installed. Below is my Dockerfile.

FROM postgres:9.6

RUN apt-get update
RUN apt-get install wget make git python python-pip python-dev build-essential postgresql-server-dev-9.6 libglib2.0-dev libc++abi-dev libc++-dev -y
RUN pip install psycopg2

RUN wget https://github.com/plv8/plv8/archive/v2.3.0.tar.gz && tar -xvzf v2.3.0.tar.gz && cd plv8-2.3.0 && make

RUN cp /plv8-2.3.0/plv8--2.3.0.sql /usr/share/postgresql/9.6/extension/
RUN cp /plv8-2.3.0/plv8.control /usr/share/postgresql/9.6/extension/
RUN cp /plv8-2.3.0/plv8.so /usr/lib/postgresql/9.6/lib/

RUN rm v2* && rm -rf plv8-2.3.0
RUN apt-get remove --purge wget make git python python-pip python-dev build-essential postgresql-server-dev-9.6 libglib2.0-dev libc++abi-dev libc++-dev -y
RUN apt-get autoremove -y && apt-get autoclean -y

The resulting image size is 3.45 GB while the original image size from Docker hub is 235 MB. Any idea why the resulting image size is so big? How can I reduce its size ? I tried to reduce its size using this link but unfortunately docker import/export loses the metadata.

Update:

I tried to consolidate all RUN statements into one.

FROM postgres:9.6

RUN apt-get update ; apt-get install wget make git python python-pip python-dev build-essential postgresql-server-dev-9.6 libglib2.0-dev libc++abi-dev libc++-dev -y ; pip install psycopg2 ; wget https://github.com/plv8/plv8/archive/v2.3.0.tar.gz ; tar -xvzf v2.3.0.tar.gz ; cd plv8-2.3.0 ; make ; cp /plv8-2.3.0/plv8--2.3.0.sql /usr/share/postgresql/9.6/extension/ ; cp /plv8-2.3.0/plv8.control /usr/share/postgresql/9.6/extension/ ; cp /plv8-2.3.0/plv8.so /usr/lib/postgresql/9.6/lib/ ; rm v2* ; rm -rf plv8-2.3.0 ; apt-get remove --purge wget make git python python-pip python-dev build-essential postgresql-server-dev-9.6 libglib2.0-dev libc++abi-dev libc++-dev -y ; apt-get autoremove -y ; apt-get autoclean -y

And the new size is 3.11 GB. Isn't it still big?

Upvotes: 1

Views: 1767

Answers (1)

MiharbKH
MiharbKH

Reputation: 206

Try to run commands using && as possible as you can. Example:

FROM postgres:9.6

RUN apt-get update && \
    apt-get install wget make git python python-pip python-dev build-essential 
    postgresql-server-dev-9.6 libglib2.0-dev libc++abi-dev libc++-dev -y && \
    apt-get install wget make git python python-pip python-dev build-essential 
    postgresql-server-dev-9.6 libglib2.0-dev libc++abi-dev libc++-dev -y && \
    pip install psycopg2

RUN wget https://github.com/plv8/plv8/archive/v2.3.0.tar.gz && tar -xvzf v2.3.0.tar.gz && cd plv8-2.3.0 && make

RUN cp /plv8-2.3.0/plv8--2.3.0.sql /usr/share/postgresql/9.6/extension/ && \
    cp /plv8-2.3.0/plv8.control /usr/share/postgresql/9.6/extension/ && \
    cp /plv8-2.3.0/plv8.so /usr/lib/postgresql/9.6/lib/
.
.
.

And so on... Because each RUN line will generate an image called dangling image with it's own size.

Upvotes: 2

Related Questions