Protect your web server with HTTP authentication

Go to Project 7 from the git repository root:

cd projects/p07

The first step is the same as it was in Run more Docker Compose project on the same port using nginx-proxy. Start the proxy server:

cd nginxproxy
docker-compose up -d

Go to the web folder:

cd ../web

You can simply start a web server protected by HTTP authentication. The name and the password will come from environment variables. I recommend you to use a more secure way in production. Create the .htpasswd file manually and mount it inside the container.

The htpasswd container will create .htpasswd automatically and exit.

In the “.env” file you can find two variables. HTTPD_USER and HTTPD_PASS will be used in “docker-compose.yml” by the “htpasswd” service to generate the password file and then the “httpd” service will read it from the common volume.

The “fixperm” service runs and exits similar to “htpasswd”. It sets the permission of the files after the web server starts.

Use the “depends_on” option to control which service starts first.

At this point you need to have the XIP variable set as the Welcome to Learn Docker’s documentation! refers to it.

Alternative option: set the XIP variable in the “.env” file:

## Start the web server

docker-compose up -d

Open the web page in your browser (Ex.: p07.192.168.1.6.xip.io). You will get a password prompt.

Clean the project:

docker-compose down --volume
cd ../nginxproxy
docker-compose down --volume