Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit f38d9dc

Browse files
authored
Feature dockerfile for cortex (#1014)
Co-authored-by: Hien To <tominhhien97@gmail.com>
1 parent 2562ac8 commit f38d9dc

File tree

12 files changed

+234
-50
lines changed

12 files changed

+234
-50
lines changed

Dockerfile

Lines changed: 0 additions & 15 deletions
This file was deleted.

docker-compose.yml

Lines changed: 0 additions & 35 deletions
This file was deleted.

docker/Dockerfile

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Please change the base image to the appropriate CUDA version base on NVIDIA Driver Compatibility
2+
# Run nvidia-smi to check the CUDA version and the corresponding driver version
3+
# Then update the base image to the appropriate CUDA version refer https://catalog.ngc.nvidia.com/orgs/nvidia/containers/cuda/tags
4+
5+
FROM nvidia/cuda:12.4.1-runtime-ubuntu22.04 AS base
6+
7+
# 1. Install dependencies only when needed
8+
FROM base AS devel
9+
10+
# Install g++ 11
11+
RUN apt update && apt install -y gcc-11 g++-11 cpp-11 jq xsel curl gnupg make python3-dev && curl -sL https://deb.nodesource.com/setup_20.x | bash - && apt install nodejs -y && rm -rf /var/lib/apt/lists/*
12+
13+
# Update alternatives for GCC and related tools
14+
RUN update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 110 \
15+
--slave /usr/bin/g++ g++ /usr/bin/g++-11 \
16+
--slave /usr/bin/gcov gcov /usr/bin/gcov-11 \
17+
--slave /usr/bin/gcc-ar gcc-ar /usr/bin/gcc-ar-11 \
18+
--slave /usr/bin/gcc-ranlib gcc-ranlib /usr/bin/gcc-ranlib-11 && \
19+
update-alternatives --install /usr/bin/cpp cpp /usr/bin/cpp-11 110
20+
21+
RUN npm install -g yarn
22+
23+
WORKDIR /app
24+
25+
FROM devel AS release
26+
27+
EXPOSE 1337
28+
29+
COPY ./common/entrypoint.sh /usr/local/bin/entrypoint.sh
30+
31+
RUN chmod +x /usr/local/bin/entrypoint.sh
32+
33+
HEALTHCHECK --interval=300s --timeout=30s --start-period=10s --retries=3 \
34+
CMD curl -f http://127.0.0.1:1337/api/system || exit 1
35+
36+
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]

docker/Dockerfile.firewall

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Please change the base image to the appropriate CUDA version base on NVIDIA Driver Compatibility
2+
# Run nvidia-smi to check the CUDA version and the corresponding driver version
3+
# Then update the base image to the appropriate CUDA version refer https://catalog.ngc.nvidia.com/orgs/nvidia/containers/cuda/tags
4+
5+
FROM nvidia/cuda:12.4.1-runtime-ubuntu22.04 AS base
6+
7+
# 1. Install dependencies only when needed
8+
FROM base AS devel
9+
10+
# Install g++ 11
11+
RUN apt update && apt install -y gcc-11 g++-11 cpp-11 jq xsel curl gnupg make python3-dev dnsmasq nginx iproute2 && curl -sL https://deb.nodesource.com/setup_20.x | bash - && apt install nodejs -y && rm -rf /var/lib/apt/lists/*
12+
13+
# Update alternatives for GCC and related tools
14+
RUN update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 110 \
15+
--slave /usr/bin/g++ g++ /usr/bin/g++-11 \
16+
--slave /usr/bin/gcov gcov /usr/bin/gcov-11 \
17+
--slave /usr/bin/gcc-ar gcc-ar /usr/bin/gcc-ar-11 \
18+
--slave /usr/bin/gcc-ranlib gcc-ranlib /usr/bin/gcc-ranlib-11 && \
19+
update-alternatives --install /usr/bin/cpp cpp /usr/bin/cpp-11 110
20+
21+
RUN npm install -g yarn
22+
23+
RUN mkdir -p /etc/dnsmasq.d/
24+
25+
WORKDIR /app
26+
27+
FROM devel AS release
28+
29+
EXPOSE 80
30+
31+
COPY ./common/dnsmasq.conf /etc/dnsmasq.conf
32+
COPY ./common/blocked-domains.txt /etc/dnsmasq.d/blocked-domains.txt
33+
34+
COPY ./common/entrypoint-firewall.sh /usr/local/bin/entrypoint.sh
35+
36+
COPY ./common/routes.txt /app/routes.txt
37+
COPY ./common/generate_nginx_conf.sh /usr/local/bin/generate_nginx_conf.sh
38+
COPY ./common/nginx.conf /etc/nginx/nginx.conf
39+
40+
RUN chmod +x /usr/local/bin/entrypoint.sh
41+
42+
HEALTHCHECK --interval=300s --timeout=30s --start-period=10s --retries=3 \
43+
CMD curl -f http://127.0.0.1/api/system || exit 1
44+
45+
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]

docker/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Docker with cortex
2+
3+
We offer two methods for deploying the Cortex environment on Docker.
4+
5+
## Method 1: Use the default Dockerfile with Cortex.
6+
7+
To use this method, you need to follow these steps:
8+
```bash
9+
git clone https://github.com/janhq/cortex.git
10+
cd cortex/docker
11+
docker build -t cortex:latest .
12+
13+
# Run the container with GPU support
14+
docker run -it --gpus all -d -p 1337:1337 cortex:latest
15+
16+
# Run the container with CPU support
17+
docker run -it -d -p 1337:1337 cortex:latest
18+
19+
# After starting, you can access Swagger at http://localhost:1337/api and the API server at http://localhost:1337.
20+
# Additionally, you can exec into the container and use cortex-cli to perform other operations.
21+
```
22+
23+
## Method 2: Use Dockerfile.firewall with the feature to block outbound connections by domain and block inbound connections by API path.
24+
25+
The use case for this method is when you want to host the Cortex API 100% offline, preventing access to remote models like the OpenAI API. Alternatively, you might want to block inbound connections by restricting clients from calling the API to load models `/v1/models/start`.
26+
27+
To use this method, you need to follow these steps:
28+
29+
- Step 1: Edit the contents of the [blocked-domains.txt](./docker/common/blocked-domains.txt) file according to your requirements. Refer to the provided examples in the file. The goal is to block outbound connections to the domains you do not want to allow.
30+
- Step 2: Edit the contents of the [blocked-paths.txt](./docker/common/blocked-paths.txt) file according to your requirements. Refer to the provided examples in the file. The goal is to block inbound connections to the paths you do not want to allow.
31+
- Step 3: Build the image with Dockerfile.firewall following the instructions below:
32+
33+
```bash
34+
git clone https://github.com/janhq/cortex.git
35+
cd cortex/docker
36+
docker build -f Dockerfile.firewall -t cortex-with-firewall:latest .
37+
38+
# Run the container with GPU support
39+
docker run -it --gpus all -d -p 1337:1337 cortex:latest
40+
41+
# Run the container with CPU support
42+
docker run -it -d -p 1337:1337 cortex:latest
43+
44+
# After starting, you can access Swagger at http://localhost:1337/api and the API server at http://localhost:1337.
45+
# Additionally, you can exec into the container and use cortex-cli to perform other operations.
46+
```

docker/common/blocked-domains.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Block IPv4 of domain openai.com and all subdomains *.openai.com
2+
address=/openai.com/0.0.0.0
3+
4+
# Block IPv6 of domain openai.com and all subdomains *.openai.com
5+
address=/openai.com/::

docker/common/dnsmasq.conf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
server=8.8.8.8
2+
3+
no-resolv
4+
5+
conf-file=/etc/dnsmasq.d/blocked-domains.txt
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/sh
2+
3+
# Setup DNS resolution with dnsmasq
4+
echo "nameserver 127.0.0.1" > /etc/resolv.conf
5+
dnsmasq -k &
6+
7+
# Generate Nginx configuration from routes.txt
8+
/usr/local/bin/generate_nginx_conf.sh
9+
10+
# Install cortex
11+
npm install -g cortexso
12+
13+
# Start cortex
14+
cortex -a 127.0.0.1
15+
16+
cortex engines llamacpp init
17+
cortex engines tensorrt-llm init
18+
19+
# Start nginx
20+
nginx -g 'daemon off;' &
21+
22+
# Keep the container running by tailing the log file
23+
tail -f /root/cortex/cortex.log

docker/common/entrypoint.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/sh
2+
3+
npm install -g cortexso
4+
# Run cortex
5+
cortex -a 0.0.0.0
6+
7+
cortex engines llamacpp init
8+
cortex engines tensorrt-llm init
9+
10+
# Keep the container running by tailing the log file
11+
tail -f /root/cortex/cortex.log
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/sh
2+
3+
NGINX_CONF="/etc/nginx/conf.d/generated_routes.conf"
4+
5+
rm -f $NGINX_CONF
6+
7+
touch $NGINX_CONF
8+
9+
while IFS= read -r line || [ -n "$line" ]
10+
do
11+
route=$(echo $line | awk '{print $1}')
12+
action=$(echo $line | awk '{print $2}')
13+
14+
echo "location $route {" >> $NGINX_CONF
15+
if [ "$action" = "allow" ]; then
16+
echo " allow all;" >> $NGINX_CONF
17+
else
18+
echo " deny all;" >> $NGINX_CONF
19+
fi
20+
echo "}" >> $NGINX_CONF
21+
done < /app/routes.txt

0 commit comments

Comments
 (0)