Adding Custom HTML in Nginx using Docker

How to Use the NGINX Docker Official Image | Docker

I am using the above link to follow steps to create custom HTML using NGINX. As mentioned in the tutorial I have created a index.html file in site-content folder in my local machine. After that when I execute the

$ docker run -it --rm -d -p 8080:80 --name web -v ~/site-content:/usr/share/nginx/html nginx

command and check http://localhost:8080 I still get old message of Welcome to nginx. Could I please get help on this?

The container must be stopped once.

$ docker stop web

Make sure the files are arranged as follows

$ ls ~/site-content
index.html

Yes I have stopped the container once. Also i checked the files. I am using docker desktop and command prompt to execute the commands. After I stopped the container and run next command

docker run -it --rm -d -p 8080:80 --name web -v ~/site-content:/usr/share/nginx/html nginx

I still get old output in the page

The second half of the sentence is missing and not visible, so it would be helpful if you could edit it.

Login to the container and check if the file exists in the container

$ docker exec -it web bash
$ ls /usr/share/nginx/html
index.html

And make sure the contents of index.html are correct

# cat /usr/share/nginx/html/index.html 
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Docker Nginx</title>
</head>
<body>
  <h2>Hello from Nginx container</h2>
</body>
</html>
1 Like

Apologies for the confusion. I am using docker desktop in windows. Could you let me know commands in it

I don’t have “docker desktop for windows” available right now, so please wait for more details.
You can login to the container by typing the command “docker exec” in a terminal (powershell etc
) with the command “docker run”.

Sample:

ubuntu@ubuntu:~$ docker run -it --rm -d -p 8080:80 --name web -v ~/site-content:/usr/share/nginx/html nginx
3f1ba1408e1140c153a3f2fc19c815ccb4ee0437a064beb0dc9afc768b411a26
ubuntu@ubuntu:~$ docker exec -it web bash
root@3f1ba1408e11:/# ls /usr/share/nginx/html
index.html
root@3f1ba1408e11:/# cat /usr/share/nginx/html/index.html 
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Docker Nginx</title>
</head>
<body>
  <h2>Hello from Nginx container</h2>
</body>
</html>

Unless you are running the command in a WSL distribution for which you enabled the WSL integration in Docker Desktop, ~ will not work. You can replace it with %USERPROFILE% if you run it from command line. I am not sure about the syntax in PowerShell, but I wouldn’t store the site-content in the user profile folder anyway. I would create a project folder and run the commands there so I could use $(pwd) which works on Linux, macOS and in PowerShell.

Or the best would be to create a compose file and just using relative path.

services:
  nginx:
    image: nginx
    ports:
      - "8080:80"
    volumes:
      - ./site-content:/usr/share/nginx/html
docker compose up -d

By the way I don’t recommend using -it and -d at the same time. Asking for an interactive terminal in a detached container doesn’t make sense and some processes like Apache HTTPD could stop just because you use docker attach. Or without -d, resizing an terminal window could stop the container. --rm is usually not used either with detached containers but in some cases it can still be useful. I know that all these flags were in the blog post, but that was probably a mistake . Sometimes I make that mistake too when I run multiple bash containers and suddenly I want to run a server.

Note: I fixed your posts. Please, always check your posts after sending them and make sure everything looks like as expected. Otherwise some parts of the message could disappear and nobody understands your question. The

```

characters always has to be in a new line.

@jayanthcr7 did you rebuild it. for any changes you will need to rebuild and the run the “docker run ,” command