Search code examples
macosubuntudockerdockerfiledocker-build

`docker build` fails trying to install ubuntu packages


Trying to build a simple ubuntu apache web server docker image, I get an error while the docker build command tries installing packages. I am using the Ubuntu base image in Docker to do this. Below is the code in my Dockerfile;

FROM ubuntu
RUN apt-get update
RUN apt-get install apache2
RUN apt-get install apache2-utils
RUN apt-get clean
EXPOSE 80
CMD ["apache2ctl", "-D", "FOREGROUND"]

My Host OS is Mac OSX El Capitan, and the error I get when the build fails is;

The command '/bin/sh -c apt-get install apache2' returned a non-zero code: 1

and my docker build command is;

docker build -t="webserver" .

Any help please. Thanks in Advance.


Solution

  • You should use the '-y' apt-get flag when building an image.

    apt-get will ask you permission to continue with apache installation, and since you can't interact with the apt-get while building the image, you must pass the '-y' flag that means 'yes' to apt-get prompt.

    Try changing it to:

    FROM ubuntu
    RUN apt-get update
    RUN apt-get install apache2 -y
    RUN apt-get install apache2-utils -y
    RUN apt-get clean
    EXPOSE 80
    CMD ["apache2ctl", "-D", "FOREGROUND"]
    

    or even:

    FROM ubuntu
    RUN apt-get update && apt-get install apache2 apache2-utils -y
    RUN apt-get clean
    EXPOSE 80
    CMD ["apache2ctl", "-D", "FOREGROUND"]