File size: 3,105 Bytes
64bc6d4
936da19
 
64bc6d4
 
 
936da19
 
 
64bc6d4
 
 
 
936da19
64bc6d4
 
 
 
 
936da19
64bc6d4
 
 
5d5bc3a
64bc6d4
 
 
2c46e17
64bc6d4
 
936da19
 
64bc6d4
 
 
 
 
3a282ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64bc6d4
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
# 1. Base Image: Start with Miniconda as your project requires it.
FROM continuumio/miniconda3

# 2. Create Conda Environment:
# First, copy only the environment file and create the environment.
# This is done as root and caches this layer, so it only re-runs if environment.yaml changes.
COPY environment.yaml /tmp/environment.yaml
RUN conda env create -f /tmp/environment.yaml

# 3. Create a Non-Root User:
# As shown in your example, we create a dedicated, non-root user to run the application.
# This is a critical security and permissions best practice.
RUN useradd -m -u 1000 user

# 4. Copy Application Code:
# Copy the rest of your application code into the user's home directory.
# The `--chown=user:user` flag sets the correct ownership at the same time,
# which is more efficient and cleaner than a separate `chown` command.
COPY --chown=user:user . /home/user/app

# 5. Switch to Non-Root User:
# From this point on, all commands will be run as 'user'.
USER user

# 6. Set Working Directory:
# Set the working directory to where the code was copied.
WORKDIR /home/user/app

# 7. Expose Port:
# Expose the port your Gradio app will run on.
EXPOSE 7860

# 8. Run the Application:
# Use the `conda run` command to execute your app within the 'tirex' environment.
# Because we are now running as 'user', any libraries that need to write to a cache
# (like Hugging Face, Matplotlib, or PyTorch) will do so inside `/home/user/.cache`,
# which is writable by 'user', completely solving all previous permission errors.
CMD ["conda", "run", "--no-capture-output", "-n", "tirex", "python", "app.py"]








# 1. Base Image: Start with Miniconda as your project requires it.
FROM continuumio/miniconda3

# 2. Create Conda Environment:
# First, copy only the environment file and create the environment.
# This is done as root and caches this layer, so it only re-runs if environment.yaml changes.
COPY environment.yaml /tmp/environment.yaml
RUN conda env create -f /tmp/environment.yaml

# 3. Create a Non-Root User:
# As shown in your example, we create a dedicated, non-root user to run the application.
# This is a critical security and permissions best practice.
RUN useradd -m -u 1000 user

# 4. Copy Application Code:
# Copy the rest of your application code into the user's home directory.
# The `--chown=user:user` flag sets the correct ownership at the same time,
# which is more efficient and cleaner than a separate `chown` command.
COPY --chown=user:user . /home/user/app

# 5. Switch to Non-Root User:
# From this point on, all commands will be run as 'user'.
USER user

# 6. Set Working Directory:
# Set the working directory to where the code was copied.
WORKDIR /home/user/app

# 7. Expose Port:
# Expose the port your Gradio app will run on.
EXPOSE 7860

# 8. Run the Application:
# The JIT compilation will now happen here, on first startup.
# Because this happens on the GPU-enabled runtime machine, it will be much faster.
# The results will be cached in /home/user/.cache, making subsequent starts fast.
CMD ["conda", "run", "--no-capture-output", "-n", "tirex", "python", "app.py"]