How to download and unzip in Dockerfile
Asked Answered
C

3

29

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;
Cicelycicenia answered 18/12, 2018 at 20:48 Comment(2)
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.Watchband
Try using CMD instead of RUN, CMD will occur once the Docker was loaded and entrypoint was called.Falchion
H
8

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

Hildebrandt answered 19/8, 2022 at 11:32 Comment(1)
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.Mayamayakovski
B
6

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
Barbaresi answered 14/6, 2022 at 20:50 Comment(1)
Seems like they changed their mind. The cited paragraph has been removed, instead you find this now at docs.docker.com/develop/develop-images/instructions/…: ADD is better than manually adding files using something like wget and tar, because it ensures a more precise build cache.Geometric
S
5

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

Sequestration answered 7/2, 2020 at 5:22 Comment(2)
The question is, how to combine ADD with wget, because ADD decompresses only local files.Headway
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)Batchelor

© 2022 - 2024 — McMap. All rights reserved.