Extracting gz in Dockerfile

Viewed 47003

I have a directory structure as follows.

- app
  Dockerfile
   - target
     - test_app.tar.gz

The extraction of the tar.gz will have the following,

- lib
- conf
- bin

I would like to extract and add lib/* folder to the docker image.

FROM docker.hub.com/alpine/jdk1.8:latest  

RUN mkdir -p /service \
            /service/app_lib \
            /service/lib

COPY target/test_app.tar.gz /service/app_lib/
RUN cd  /service/app_lib/ 
RUN tar -xzf test_app.tar.gz
RUN rm test_app.tar.gz
RUN cd lib
COPY * /service/lib/
RUN rm app_lib

Getting the below error with this.

Step 5/11 : RUN tar -xzf test_app.tar.gz
 ---> Running in edde27a1cc60
tar (child): test_app.tar.gz: Cannot open: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now

Not sure what is wrong with this.

2 Answers

The ADD directive will unpack the .gz automatically on build

ADD target/test_app.tar.gz /service/app_lib
# Clean up
RUN rm -rf /service/app_lib/conf \
           /service/app_lib/bin

The effect of the RUN cd command only persists for that single line, at the next line, you're back in /, so the file can't be found. Use WORKDIR instead of RUN cd ... and it should work as you expect. Or you can put the whole thing into a single RUN command like

RUN cd /service/app_lib/ && tar -xzf test_app.tar.gz && rm test_app.tar.gz

Same thing applies for the next RUN cd lib part.

https://docs.docker.com/engine/reference/builder/#workdir

Related