DromiX
DromiX

Reputation: 553

How to download and unzip in Dockerfile

So, I have, it works, but I want to change the way to immediately download the file and unpack it:

Dockerfile
FROM wordpress:fpm

# Copying themes from local  
COPY  ./wordpress/ /var/www/html/wp-content/themes/wordpress/    
RUN chmod -R 777 /var/www/html/    

How can I immediately download the file from the site and unzip it to the appropriate folder?

docker-compose.yml
wordpress:
build: . 
links:
  - db:mysql
nginx:
image: raulr/nginx-wordpress 
links:
  - wordpress
ports:
 - "8080:80"
 volumes_from:
 - wordpress
db:
image: mariadb
environment:
MYSQL_ROOT_PASSWORD: qwerty 

I tried:

#install unzip and wget
RUN \
apt-get update && \
apt-get install unzip wget -y && \
rm -rf /var/lib/apt/lists/*

RUN wget -O /var/www/html/type.zip http://wp-templates.ru/download/2405 \
&& unzip '/var/www/html/type.zip' -d /var/www/html/wp-content/themes/ && rm 
/var/www/html/type.zip || true;

Upvotes: 30

Views: 78018

Answers (3)

krad
krad

Reputation: 1419

Best to use a multistage docker build. You will need the latest version of docker and buildkit enabled. Then do something along these lines

# syntax=docker/dockerfile:1
FROM alpine:latest AS unzipper
RUN apk add unzip wget curl
RUN mkdir /opt/ ; \
  curl <some-url> | tar xvzf - -C /opt

FROM wordpress:fpm
COPY  --from=unzipper /opt/ /var/www/html/wp-content/themes/wordpress/    

Even better is if there is a Docker image built already with the stuff in you want you just need the 'copy --from' line and give it the image name.

Finally dont worry about any mess in the 1st stage as its discarded when the build completes, so the fact its alpine, and not using no-cache is irrelevant, and none of the installed packages end up in the final image

Upvotes: 9

ciclistadan
ciclistadan

Reputation: 105

Found more guidance for remote zipped files in Docker documentation

Because image size matters, using ADD to fetch packages from remote URLs is strongly discouraged; you should use curl or wget instead. That way you can delete the files you no longer need after they’ve been extracted and you don’t have to add another layer in your image. For example, you should avoid doing things like:

ADD https://example.com/big.tar.xz /usr/src/things/
RUN tar -xJf /usr/src/things/big.tar.xz -C /usr/src/things
RUN make -C /usr/src/things all

And instead, do something like:

RUN mkdir -p /usr/src/things \
    && curl -SL https://example.com/big.tar.xz \
    | tar -xJC /usr/src/things \
    && make -C /usr/src/things all

Upvotes: 6

Taron Saribekyan
Taron Saribekyan

Reputation: 1390

Dockerfile has "native command" for copying and extracting .tar.gz files.

So you can change archive type from .zip to .tar.gz (maybe in future versions zip also will be supported, I'm not sure) and use ADD instead of COPY.

Read more about ADD

Upvotes: 5

Related Questions