Reputation: 18670
Let's say that I am logged in as reynierpm
in my Fedora and I will build a Docker image from a Dockerfile. That image will contain a LAMP environment. I have a Apache Virtual Host(VH) default file that looks as follow:
<VirtualHost *:80>
#ServerName www.example.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www
<Directory /var/www>
AllowOverride All
Require all granted
</Directory>
ErrorLog /dev/stdout
CustomLog /dev/stdout combined
</VirtualHost>
As part of the built process this file is copied to the proper location on the image.
Can I get the logged in username from the host and set dynamically into this VH? At the end I would like to get the following result:
<VirtualHost *:80>
ServerName reynierpm.dev
ServerAdmin webmaster@localhost
DocumentRoot /var/www
<Directory /var/www>
AllowOverride All
Require all granted
</Directory>
ErrorLog /dev/stdout
CustomLog /dev/stdout combined
</VirtualHost>
I know that I can get the value for the current user using $(whoami)
from bash but how I can insert/set it to the VH file on Docker build?
This is the content of the Dockerfile
:
FROM ubuntu:14.04.5
MAINTAINER Me <[email protected]>
ARG USER_NAME=unknown
VOLUME ["/var/www"]
RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive \
apt-get install -y software-properties-common && \
apt-get update && \
apt-get install -y \
apache2 \
php5 \
php5-cli \
libapache2-mod-php5 \
php5-gd \
php5-json \
php5-mcrypt \
php5-mysql \
php5-xdebug \
php5-curl \
php5-memcached \
php5-mongo \
zend-framework \
mc \
nano
# Copy default virtual host file.
COPY /config/apache2_vhost/apache_default /etc/apache2/sites-available/000-default.conf
COPY run /usr/local/bin/run
RUN chmod +x /usr/local/bin/run
RUN a2enmod rewrite
EXPOSE 80
EXPOSE 9001
CMD ["/usr/local/bin/run"]
UPDATE: build fails while try to use the args
Following @Elton suggestion I added this line to the run
file:
sed -i "s/#ServerName www.example.com/$USER_NAME.dev/g" /etc/apache2/sites-available/000-default.conf
And then try to build the image as:
docker build --build-arg USER_NAME=$(whoami) -t dev-php55 .
But is failing with the following message:
One or more build-args [USER_NAME] were not consumed, failing build.
What's wrong?
Upvotes: 1
Views: 943
Reputation: 19184
If you want that value to be fixed in the image, the best option is to use a build argument, and do some text substitution in your Dockerfile:
FROM ubuntu
ARG USER_NAME=unknown
RUN echo Built by: $USER_NAME > /builder.txt
Then pass your whoami
when you build:
docker build --build-arg USER_NAME=$(whoami) -t temp .
The build argument overrides the default in the Dockerfile so your image has the real builder's username:
docker run temp cat /builder.txt
Built by: ubuntu
EDIT: build arguments are only available as environment variables during the build process. Neither the variable or the value are present in the final built image. So you can only make use of the argument value in build instructions like RUN
.
Upvotes: 3