Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change nginx's default homepage via a Dockerfile and then launch it by running a container

Tags:

I have a very simple HTML file which outputs 'Container with HTML file'.

I have this Dockerfile where I copy my welcome.html (my simple HTML page):

FROM nginx:latest
WORKDIR /usr/share/nginx/html
COPY welcome.html welcome.html

Then I create an image in the directory containing this HTML page:

docker image build -t html_nginx .

and run a container using:

docker container run -p 80:80 --rm html_nginx

But when the container is run on port 80, I get the default 'Welcome Page' of nginx and not my desired output from the HTML file ('Container with HTML file').

How much ever I try, I have never been able to get my message printed on the browser.

Can someone please point me in the right direction?

like image 758
Venkata Ramana Avatar asked Apr 16 '18 19:04

Venkata Ramana


People also ask

What command do you use to launch an nginx container on port 80 of the host file system?

Let's start our Nginx Docker container with this command: sudo docker run --name docker-nginx -p 80:80 nginx.

Which Dockerfile command define the default working directory?

The WORKDIR command is used to define the working directory of a Docker container at any given time. The command is specified in the Dockerfile. Any RUN , CMD , ADD , COPY , or ENTRYPOINT command will be executed in the specified working directory.

Should nginx be in a container?

nginx:<version>It is designed to be used both as a throw away container (mount your source code and start the container to start your app), as well as the base to build other images off of.


1 Answers

The default nginx config will load the index.html file from the /usr/share/nginx/html directory.

This default is set in /etc/nginx/conf.d/default.conf with the lines

location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
}

So to fix this you can copy welcome.html into the /usr/share/nginx/html directory as index.html, or change the above index line to reference welcome.html and reload. To make this change more permanent you would likely want to create a dockerfile that defines a docker image overriding the config copied in where you have already set this value.

Alternatively you should just be able to access /welcome.html when hitting your server e.g. http://localhost:80/welcome.html

like image 59
Peter Reid Avatar answered Oct 26 '22 02:10

Peter Reid