27

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;
DromiX
  • 523
  • 1
  • 4
  • 15
  • 1
    What happens when you run that command? I'd remove the `|| true` part since that will suppress any failure, which you don't really want. – David Maze Dec 19 '18 at 01:53
  • 1
    Try using `CMD` instead of `RUN`, CMD will occur once the Docker was loaded and entrypoint was called. – H. Bloch Jan 23 '20 at 09:02

3 Answers3

5

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
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

krad
  • 1,321
  • 1
  • 15
  • 12
  • To avoid confusion for others: According to the docker docs, the `--from` syntax requires an `=` sign, i.e. `--from=unzipper`. I was required to add it to make the snippet from above run. – Fgop Mar 06 '23 at 10:27
4

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
ciclistadan
  • 85
  • 1
  • 1
  • 8
3

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

Taron Saribekyan
  • 1,360
  • 7
  • 13
  • 5
    The question is, how to combine ADD with wget, because ADD decompresses only __local__ files. – David Dale Jul 10 '21 at 08:54
  • 4
    It's enormously silly that `ADD` does copy, or extract, or download… but never a mix. Personally I'd have created 3 different verbs (with a single meaning). (sorry about the rant) – lapo Nov 16 '21 at 13:11