Den Contre
Den Contre

Reputation: 245

Using enviroment variables in a dockerfile RUN command

Im trying to create an image through a dockerfile. But it returns an error.

Nginx Dockerfile

RUN sudo apt-get install software-properties-common -y
RUN nginx=stable
RUN echo "deb http://ppa.launchpad.net/nginx/$nginx/ubuntu lucid main" > /etc/apt/sources.list.d/nginx-$nginx-lucid.list
RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys C300EE8C
RUN apt-get update
RUN apt-get install nginx 

Error message:

W: Failed to fetch http://ppa.launchpad.net/nginx//ubuntu/dists/lucid/main/binary-amd64/Packages  404  Not Found

E: Some index files failed to download. They have been ignored, or old ones used instead.
Fetched 21.3 MB in 59s (355 kB/s)
The command '/bin/sh -c apt-get update' returned a non-zero code: 100

Upvotes: 1

Views: 151

Answers (1)

Dimitris Fasarakis Hilliard
Dimitris Fasarakis Hilliard

Reputation: 160447

You are making the mistake of assuming this:

RUN nginx=stable

Will set an environment variable which you can then access. This is unfortunately not the case.


To create enviroment variables in a dockerfile, you use the ENV command; in its most simplistic form, you can define your env. variable as:

ENV nginx stable # define it

Then the substitution of $nginx with stable will be performed correctly in the consequent RUN commands.

Additional notes:

First, you should add -y to your last RUN command so it doesn't fail:

RUN apt-get install nginx -y     

Second, you should consider chaining the RUN commands together with && to reduce the number of layers created.

Upvotes: 2

Related Questions