In this example, we are going to install apache web server on Ubuntu machine and access it from the host OS. The point here is that the apache server will always be running as a foreground process.


Dockerfile


FROM ubuntu:16.04

# Apache ENVs
ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOCK_DIR /var/lock/apache2
ENV APACHE_LOG_DIR /var/log/apache2
ENV APACHE_PID_FILE /var/run/apache2/apache2.pid
ENV APACHE_SERVER_NAME localhost

# Install services, packages and do cleanup
RUN apt-get update \
&& apt-get install -y \
apache2 \
&& rm -rf /var/lib/apt/lists/*

# Copy files
COPY apache-conf /etc/apache2/apache2.conf

# Expose Apache
EXPOSE 80

# Launch Apache
CMD ["/usr/sbin/apache2ctl", "-DFOREGROUND"]

apache-conf


Mutex file:${APACHE_LOCK_DIR} default
PidFile ${APACHE_PID_FILE}
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}
HostnameLookups Off
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf
Include ports.conf

<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
<Directory /usr/share>
AllowOverride None
Require all granted
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>

AccessFileName .htaccess

<FilesMatch "^\.ht">
Require all denied
</FilesMatch>

LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent

IncludeOptional conf-enabled/*.conf
IncludeOptional sites-enabled/*

Create image


ubuntu@xenial64:~$ docker build -t webserver_img .

Create container


The container is accessible from outside over port 5000.


ubuntu@xenial64:~$ docker run -i -t -d -p 5000:80 --name=webserver_con webserver_img

Check container status


ubuntu@xenial64:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7910cd39b101 webserver_img "/usr/sbin/apache2..." 4 seconds ago Up 4 seconds 0.0.0.0:5000->80/tcp webserver_con

Test


ubuntu@xenial64:~$ curl -I 127.0.0.1:5000 # or localhost:5000
HTTP/1.1 200 OK
Date: Wed, 24 Jan 2018 13:12:38 GMT
Server: Apache/2.4.18 (Ubuntu)
Last-Modified: Wed, 24 Jan 2018 13:08:16 GMT
ETag: "2c39-563855b3bd000"
Accept-Ranges: bytes
Content-Length: 11321
Vary: Accept-Encoding
Content-Type: text/html