Docker command fails during build, but succeeds while executed within running container
Asked Answered
B

4

32

the command :

docker build -t nginx-ubuntu . 

whith the Dockerfile below :

FROM ubuntu:12.10
RUN apt-get update
RUN apt-get -y install libpcre3 libssl-dev
RUN apt-get -y install libpcre3-dev
RUN apt-get -y install wget zip gcc
RUN wget http://nginx.org/download/nginx-1.4.1.tar.gz
RUN gunzip nginx-1.4.1.tar.gz
RUN tar -xf nginx-1.4.1.tar
RUN wget --no-check-certificate https://github.com/max-l/nginx_accept_language_module/archive/master.zip
RUN unzip master
RUN cd nginx-1.4.1
RUN ./configure --add-module=../nginx_accept_language_module-master --with-http_ssl_module --with-pcre=/lib/x86_64-linux-gnu --with-openssl=/usr/lib/x86_64-linux-gnu

Fails at the last line (./configure ...)

If I remove the last line and run a bash in the container, and execute the last line manually, it works.

I would expect that whatever command runs successfully within a container should work when the command is appended in the Dockerfile (prefixed by RUN)

am I missing something ?

Barnette answered 26/7, 2013 at 21:57 Comment(0)
J
53

The pwd is not persistent across RUN commands. You need to cd and configure within the same RUN.

This Dockerfile works fine:

FROM ubuntu:12.10
RUN apt-get update
RUN apt-get -y install libpcre3 libssl-dev
RUN apt-get -y install libpcre3-dev
RUN apt-get -y install wget zip gcc
RUN wget http://nginx.org/download/nginx-1.4.1.tar.gz
RUN gunzip nginx-1.4.1.tar.gz
RUN tar -xf nginx-1.4.1.tar
RUN wget --no-check-certificate https://github.com/max-l/nginx_accept_language_module/archive/master.zip
RUN unzip master
RUN cd nginx-1.4.1 && ./configure --add-module=../nginx_accept_language_module-master --with-http_ssl_module --with-pcre=/lib/x86_64-linux-gnu --with-openssl=/usr/lib/x86_64-linux-gnu
Joy answered 26/7, 2013 at 22:24 Comment(2)
I see, so I guess every RUN command has a new context, as in nothing is preserved. Thanks for the quick response !Barnette
Use WORKDIR instead of cd. See @alanfalloon's answer.Bestiality
P
31

As an alternative to @creak's answer, you can change the default working directory in a Dockerfile with the WORKDIR command:

FROM ubuntu:12.10
# Run update & install together so that the docker cache doesn't
# contain an out-of-date APT cache (leading to 404's when installing
# packages)
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get -y install libpcre3 libssl-dev libpcre3-dev wget zip gcc
ADD http://nginx.org/download/nginx-1.4.1.tar.gz nginx-1.4.1.tar.gz
RUN tar -zxf nginx-1.4.1.tar.gz
RUN wget --no-check-certificate https://github.com/max-l/nginx_accept_language_module/archive/master.zip
RUN unzip master
WORKDIR nginx-1.4.1
RUN ./configure --add-module=../nginx_accept_language_module-master --with-http_ssl_module --with-pcre=/lib/x86_64-linux-gnu --with-openssl=/usr/lib/x86_64-linux-gnu

This also affects the default directory when you use docker run (overridden by the -w switch).

Palm answered 4/4, 2014 at 18:43 Comment(2)
+1 WORKDIR is the correct way to set the working directory during a container build, as it applies to all the 'RUN, CMD, ENTRYPOINT, COPY, and ADD instructions that follow it'.Ruhr
this does not work for me. The file isn't available when I change WORKDIRWakeful
W
2

When I called wget or tar with RUN I needed to specify a path, it looks like ADD is the correct approach if you want to use WORKDIR as it's path instead. So either of the below resolved my issue.

RUN

RUN wget http://nginx.org/download/nginx-1.4.1.tar.gz -P ~/directory
RUN tar -zxf ~/directory/nginx-1.4.1.tar.gz -C ~/directory

or

ADD

WORKDIR ~/directory
ADD http://nginx.org/download/nginx-1.4.1.tar.gz nginx-1.4.1.tar.gz
RUN tar -zxf nginx-1.4.1.tar.gz

The second approach prevented me from needing to install wget in the container.

Wakeful answered 8/6, 2016 at 3:10 Comment(0)
F
1

Another way of doing this using the \ operator to start a new line and proceed with an additional command

Example

RUN cd /Desktop \ 
    cd Work \   
    pwd
Fourwheeler answered 16/9, 2022 at 19:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.