Skip to content

Commit

Permalink
Merge pull request #289 from dantol29/slombard/web-199-add-ngnix-in-a…
Browse files Browse the repository at this point in the history
…-docker

Slombard/web 199 add ngnix in a docker
  • Loading branch information
dantol29 authored May 17, 2024
2 parents c494d30 + 61fb3f0 commit 7f0d179
Show file tree
Hide file tree
Showing 455 changed files with 239,135 additions and 1 deletion.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@

# Specific files and directories to include
!Makefile
!Dockerfile
!.github/
!.clang-format
!.vscode/
!.cgi-bin/
!docs/
!tests/
!/ngnix/**/**/*
!webserv.conf
!webserv_default.conf
!tests/
Expand All @@ -46,7 +48,8 @@
/secretus/
/a.out
**/*.o
largefile # so we don't pull and push 100mb file
# so we don't pull and push 100mb file
largefile
testers/myenv/
testers/testsvenv/
tests/my_venv/
Expand Down
Binary file added ngnix/.DS_Store
Binary file not shown.
28 changes: 28 additions & 0 deletions ngnix/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Use a base image
FROM ubuntu:latest

# Install dependencies
RUN apt-get update && \
apt-get install -y build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev

# Copy the Nginx source code from your repository to the image
COPY nginx-1.24.0 /usr/src/nginx-1.24.0

# Compile Nginx
WORKDIR /usr/src/nginx-1.24.0
RUN ./configure --without-http_gzip_module && \
make && \
make install

# Set the PATH
ENV PATH="/usr/local/nginx/sbin:$PATH"

# Forward request logs to Docker log collector
RUN ln -sf /dev/stdout /usr/local/nginx/logs/access.log \
&& ln -sf /dev/stderr /usr/local/nginx/logs/error.log

# Expose port 80
EXPOSE 80

# Start Nginx in the foreground
CMD ["nginx", "-g", "daemon off;"]
69 changes: 69 additions & 0 deletions ngnix/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Nginx Server Docker Container

The Nginx source code has been downloaded from the [Ngnix website](https://nginx.org/en/download.html).

The Dockerfie in this directory is building an Ubunut environemnt and building Ngnix from source. The advantage is to have the source code, directly in the directory and be able to test Ngnix in a Linux environemnte. Ngnix is the webserver we take as reference in our webserv project.

## Prerequisites

- Docker installed on your machine. For installation instructions, visit the [Docker website](https://docs.docker.com/get-docker/).

## Building the Docker Image

To build the Docker image:

1. Open a terminal and navigate to the directory containing the Dockerfile.
2. Execute the following command:

```
docker build -t nginx-test .
```

This command builds a Docker image named `nginx-test` based on the instructions in the Dockerfile.

## Running the Docker Container

Run the Nginx server inside the Docker container on a custom port (e.g., 8888) with the following command:

```
docker run -d -p 8888:80 nginx-test
```

This command maps port 8888 on your host machine to port 80 inside the container, where Nginx is configured to listen by default. We choose to use port 8888 on the host to avoid conflicts with other services, including our development web server, which might already be using another port. See the section `Understanding port mapping` for a more detailed explanation of what is going on.

## Verifying the Installation

To verify that the Nginx server is running:

1. Open a web browser.
2. Navigate to `http://localhost:8888`.

If the setup is correct, the default Nginx welcome page will be displayed, indicating that the server is running and accessible.

## Understanding port mapping

When you run a Docker container and specify port mappings using `-p <host_port>:<container_port>`, Docker creates a bridge between a specified port on your host machine and a port inside the Docker container. This mechanism is crucial for containerized applications that need to be accessible from outside the Docker environment, such as web servers.

- **Host Port (`<host_port>`):** This is the port on your host machine. When you access this port, Docker forwards the traffic to a corresponding port inside the container. This allows you to interact with the Dockerized application using your host machine's IP address or localhost.

- **Container Port (`<container_port>`):** This port is within the Docker container, where the application (in this case, Nginx) listens for incoming connections or requests. Applications inside containers are configured to listen on specific ports, just as they would on a physical or virtual server.

### The Command Explained

```bash
docker run -d -p 8888:80 nginx-test
```

This command tells Docker to run the `nginx-test` container in detached mode (`-d`), creating a pathway for traffic between the host's port 8888 and the container's port 80. Here's why each part matters:

- **`-d`**: This flag runs the container in the background (detached mode), allowing you to continue using the terminal session.

- **`-p 8888:80`**: This port mapping is critical for making Nginx accessible:

- **`8888` (Host Port):** We've chosen this non-standard web port to avoid conflicts with any existing services on the host, such as another web server or development tools that might be using more common ports like 80 or 8080. Using port 8888 minimizes the chance of port collisions, which can cause errors or prevent services from starting.

- **`80` (Container Port):** By default, Nginx is configured to listen on port 80 for HTTP requests. When we map host port 8888 to this port, any traffic sent to `http://localhost:8888` is forwarded internally by Docker to port 80 of the running `nginx-test` container. This setup ensures that Nginx can handle requests without requiring any changes to its default configuration.

### Practical Implication

By mapping port 8888 on your host to port 80 inside the Nginx container, you're effectively exposing Nginx to users and systems on your network (or the internet, if your host is publicly accessible) through a non-standard port. This approach allows you to run multiple web servers or services simultaneously without port conflicts, facilitating a smoother development and testing process.
Binary file added ngnix/nginx-1.24.0/.DS_Store
Binary file not shown.
Loading

0 comments on commit 7f0d179

Please sign in to comment.