Skip to content

Dockerfile

Itao-o-bong✨ edited this page Oct 11, 2023 · 1 revision

This is a sample repo that shows how to build a simple profile page and the Docker file to deploy same to nginx

  1. 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.
  2. 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.
  3. 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/html directory inside the container. This is where Nginx will serve the static content from.
  4. # COPY nginx.conf /etc/nginx/conf.d/default.conf

    • This line is commented out (prefixed with #). If you have a custom Nginx configuration file (named nginx.conf), you can uncomment this line and replace nginx.conf with the actual filename. It would then copy your custom configuration to replace the default one.
  5. 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.
  6. 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:

  1. Base Image: The image starts with the official Nginx image, which is widely used and well-maintained.

  2. Remove Default Configurations: The default Nginx configurations are removed to ensure that you're starting with a clean slate.

  3. 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.

  4. (Optional) Custom Nginx Configuration: If you have a custom Nginx configuration, uncomment and modify the line to copy it into the appropriate directory.

  5. Expose Port: Port 80 is exposed, indicating that the container will listen for incoming HTTP traffic on this port.

  6. 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.

Clone this wiki locally