-
Notifications
You must be signed in to change notification settings - Fork 0
Dockerfile
This is a sample repo that shows how to build a simple profile page and the Docker file to deploy same to nginx
-
FROM nginx:latest- This line sets the base image for the Docker image. It uses the latest version of the official Nginx image available on Docker Hub.
-
RUN rm -rf /usr/share/nginx/html/*- This line removes any existing files in the default Nginx HTML directory. It ensures that the container starts with an empty directory, allowing you to add your own HTML files.
-
COPY . /usr/share/nginx/html- This line copies all the files and directories from your local directory (the context where the Docker build is initiated) to the
/usr/share/nginx/htmldirectory inside the container. This is where Nginx will serve the static content from.
- This line copies all the files and directories from your local directory (the context where the Docker build is initiated) to the
-
# COPY nginx.conf /etc/nginx/conf.d/default.conf- This line is commented out (prefixed with
#). If you have a custom Nginx configuration file (namednginx.conf), you can uncomment this line and replacenginx.confwith the actual filename. It would then copy your custom configuration to replace the default one.
- This line is commented out (prefixed with
-
EXPOSE 80- This line informs Docker that the container will listen on port 80 at runtime. It's a form of documentation to indicate which ports are intended to be published.
-
CMD ["nginx", "-g", "daemon off;"]- This line sets the default command to run when the container starts. It starts the Nginx server in the foreground and keeps the process running. This is necessary because Docker containers typically expect a foreground process to be running.
Now, here's how you can use this Dockerfile:
-
Base Image: The image starts with the official Nginx image, which is widely used and well-maintained.
-
Remove Default Configurations: The default Nginx configurations are removed to ensure that you're starting with a clean slate.
-
Copy HTML Files: All files and directories from your local directory are copied into the container's Nginx HTML folder. This is where your static content will be served from.
-
(Optional) Custom Nginx Configuration: If you have a custom Nginx configuration, uncomment and modify the line to copy it into the appropriate directory.
-
Expose Port: Port 80 is exposed, indicating that the container will listen for incoming HTTP traffic on this port.
-
Start Nginx: The container starts by running Nginx in the foreground. This keeps the container running and makes it ready to serve your static content.
Please note that to build the Docker image from this Dockerfile, you would use the docker build command in your terminal. For example:
docker build -t my-nginx-image .This assumes the Dockerfile is in the current directory, and it tags the resulting image with the name my-nginx-image.