summaryrefslogtreecommitdiff
path: root/Dockerfile
blob: 3e44c717f304e57f76c80b3deaf7c557025a859c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# ===== BUILD STAGE =====
FROM ubuntu:22.04 AS builder

# Set environment variables for build
ENV DEBIAN_FRONTEND=noninteractive
ENV CGO_ENABLED=1
ENV GO_VERSION=1.22.10

# Install basic dependencies first
RUN apt-get update && apt-get install -y \
    wget \
    curl \
    git \
    build-essential \
    gcc \
    g++ \
    pkg-config \
    ca-certificates \
    gnupg \
    && rm -rf /var/lib/apt/lists/*

# Install OpenGL and X11 dependencies (required for Go OpenGL bindings)
RUN apt-get update && apt-get install -y --no-install-recommends \
    libgl1-mesa-dev \
    libglu1-mesa-dev \
    libglfw3-dev \
    libglew-dev \
    libx11-dev \
    libxrandr-dev \
    libxinerama-dev \
    libxcursor-dev \
    libxi-dev \
    libxxf86vm-dev \
    && rm -rf /var/lib/apt/lists/*

# Add NVIDIA CUDA repository and install CUDA (optional)
RUN wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-keyring_1.0-1_all.deb && \
    dpkg -i cuda-keyring_1.0-1_all.deb && \
    apt-get update && \
    (apt-get install -y cuda-toolkit-11-8 || echo "CUDA installation failed, continuing without CUDA support") && \
    rm -rf /var/lib/apt/lists/* && \
    rm -f cuda-keyring_1.0-1_all.deb

# Set CUDA environment if available
ENV PATH=$PATH:/usr/local/cuda/bin
ENV LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/lib64

# Install Go
RUN wget https://go.dev/dl/go${GO_VERSION}.linux-amd64.tar.gz && \
    tar -C /usr/local -xzf go${GO_VERSION}.linux-amd64.tar.gz && \
    rm go${GO_VERSION}.linux-amd64.tar.gz

# Set Go environment
ENV PATH=$PATH:/usr/local/go/bin
ENV GOPATH=/go
ENV PATH=$PATH:$GOPATH/bin

# Create workspace directory
WORKDIR /workspace

# Copy go.mod and go.sum first for better caching
COPY go.mod go.sum ./

# Download Go dependencies
RUN go mod download

# Copy source code
COPY . .

# Try to compile CUDA kernel if CUDA is available, otherwise create dummy library
RUN if command -v nvcc >/dev/null 2>&1; then \
        echo "CUDA found, compiling kernel..." && \
        nvcc -c torus_kernel.cu -o torus_kernel.o -Xcompiler -fPIC && \
        ar rcs libtorus_kernel.a torus_kernel.o; \
    else \
        echo "CUDA not found, creating dummy library..." && \
        echo "void updateTorusVerticesCUDA() {}" > dummy.c && \
        echo "void updateTorusColorsCUDA() {}" >> dummy.c && \
        echo "int initCUDA() { return 0; }" >> dummy.c && \
        echo "void cleanupCUDA() {}" >> dummy.c && \
        gcc -c dummy.c -o torus_kernel.o && \
        ar rcs libtorus_kernel.a torus_kernel.o; \
    fi

# Build the application
RUN go build -o torus-app .

# ===== RUNTIME STAGE =====
FROM ubuntu:22.04 AS runtime

# Set environment variables for runtime
ENV DEBIAN_FRONTEND=noninteractive
ENV DISPLAY=:0

# Install only runtime dependencies
RUN apt-get update && apt-get install -y \
    libgl1-mesa-glx \
    libglu1-mesa \
    libglfw3 \
    libglew2.2 \
    libx11-6 \
    libxrandr2 \
    libxinerama1 \
    libxcursor1 \
    libxi6 \
    xvfb \
    x11-apps \
    mesa-utils \
    && rm -rf /var/lib/apt/lists/*

# Create workspace directory
WORKDIR /workspace

# Copy the built application from builder stage
COPY --from=builder /workspace/torus-app .

# Create a script to run with virtual display
RUN echo '#!/bin/bash\n\
Xvfb :0 -screen 0 1024x768x24 -ac +extension GLX +render -noreset &\n\
export DISPLAY=:0\n\
sleep 2\n\
exec "$@"' > /usr/local/bin/run-with-display.sh && \
    chmod +x /usr/local/bin/run-with-display.sh

# Expose any ports if needed (for future web interface)
EXPOSE 8080

# Default command
CMD ["/usr/local/bin/run-with-display.sh", "./torus-app"]

# ===== DEVELOPMENT STAGE =====
FROM builder AS development

# Install additional development tools
RUN apt-get update && apt-get install -y \
    vim \
    nano \
    htop \
    gdb \
    valgrind \
    strace \
    && rm -rf /var/lib/apt/lists/*

# Create the display script in development too
RUN echo '#!/bin/bash\n\
Xvfb :0 -screen 0 1024x768x24 -ac +extension GLX +render -noreset &\n\
export DISPLAY=:0\n\
sleep 2\n\
exec "$@"' > /usr/local/bin/run-with-display.sh && \
    chmod +x /usr/local/bin/run-with-display.sh

# Expose ports
EXPOSE 8080

# Default to bash for development
CMD ["/bin/bash"]