-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from ll7/25-create-a-devcontainer-for-development
Fixes #25 create a devcontainer for development
- Loading branch information
Showing
7 changed files
with
314 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
FROM nvidia/cuda:12.4.1-runtime-ubuntu22.04 | ||
|
||
ENV DEBIAN_FRONTEND=noninteractive | ||
RUN apt-get update \ | ||
&& apt-get -y install --no-install-recommends apt-utils 2>&1 | ||
|
||
# Verify git and needed tools are installed | ||
RUN apt-get install --no-install-recommends -y git | ||
|
||
# Install necessary dependencies | ||
RUN apt-get update && \ | ||
apt-get install --no-install-recommends -y \ | ||
python3 python-is-python3 python3-pip python3-dev python3.10-venv \ | ||
python3-opengl libsdl2-dev libsdl2-image-dev libsdl2-mixer-dev libsdl2-ttf-dev \ | ||
libsmpeg-dev libportmidi-dev libavformat-dev libswscale-dev libjpeg-dev libfreetype6-dev | ||
|
||
# Set git to auto adjust for the correct line endings between different operating systems | ||
RUN git config --global core.autocrlf true | ||
|
||
# Set the display environment variable | ||
ENV DISPLAY=host.docker.internal:0.0 | ||
|
||
# Create a virtual environment and activate it | ||
# Avoids pip complaint about being a root user in the docker container | ||
RUN python3 -m venv /opt/venv | ||
ENV PATH="/opt/venv/bin:$PATH" | ||
|
||
# Update pip | ||
RUN pip install --upgrade pip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"name": "robot-sf-ll7-devcontainer", | ||
"build": { | ||
"dockerfile": "Dockerfile", | ||
"context": "..", | ||
"args": { | ||
"VARIANT": "sf-ll7" | ||
} | ||
}, | ||
"runArgs": [ | ||
"--network=host", | ||
"-e", "DISPLAY=host.docker.internal:0.0", | ||
"--gpus=all" | ||
], | ||
"postCreateCommand": "./postCreate.sh", | ||
"customizations": { | ||
"vscode": { | ||
"extensions": [ | ||
"ms-python.python", | ||
"ms-vscode-remote.remote-containers", | ||
"GitHub.vscode-pull-request-github", | ||
"GitHub.copilot", | ||
"mhutchie.git-graph", | ||
"ms-python.pylint", | ||
"genieai.chatgpt-vscode", | ||
"ms-azuretools.vscode-docker", | ||
"eamodio.gitlens" | ||
] | ||
}, | ||
"settings": { | ||
"terminal.integrated.shell.linux": "/bin/bash" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
To achieve the same setup using `devcontainer.json` for a development container in Visual Studio Code, you'll need to set up a development container with the necessary configuration. The `devcontainer.json` file defines the configuration for the development container. | ||
|
||
Here's how you can do it: | ||
|
||
### Step 1: Install Required Extensions | ||
|
||
Make sure you have the following extensions installed in Visual Studio Code: | ||
- **Remote - Containers**: Allows you to open any folder or repository inside a container and take advantage of Visual Studio Code's full feature set. | ||
|
||
### Step 2: Create the Development Container Configuration | ||
|
||
1. **Create a `.devcontainer` folder** in your project directory. | ||
2. **Create a `devcontainer.json` file** inside the `.devcontainer` folder with the following content: | ||
|
||
```json | ||
{ | ||
"name": "Pygame Dev Container", | ||
"image": "mcr.microsoft.com/vscode/devcontainers/python:3.9", | ||
"runArgs": [ | ||
"--network=host", | ||
"-e", "DISPLAY=host.docker.internal:0.0" | ||
], | ||
"postCreateCommand": "pip install pygame", | ||
"customizations": { | ||
"vscode": { | ||
"extensions": [ | ||
"ms-python.python" | ||
] | ||
} | ||
}, | ||
"remoteUser": "vscode" | ||
} | ||
``` | ||
|
||
### Step 3: Create the Dockerfile | ||
|
||
In the same `.devcontainer` folder, create a `Dockerfile` with the following content: | ||
|
||
```Dockerfile | ||
# Use the official Python base image | ||
FROM mcr.microsoft.com/vscode/devcontainers/python:3.9 | ||
|
||
# Install necessary dependencies | ||
RUN apt-get update && \ | ||
apt-get install -y python3-opengl libsdl2-dev libsdl2-image-dev libsdl2-mixer-dev libsdl2-ttf-dev \ | ||
libsmpeg-dev libportmidi-dev libavformat-dev libswscale-dev libjpeg-dev libfreetype6-dev | ||
|
||
# Set the display environment variable | ||
ENV DISPLAY=host.docker.internal:0.0 | ||
|
||
# Install Pygame | ||
RUN pip install pygame | ||
``` | ||
|
||
### Step 4: Create the Pygame Script | ||
|
||
In the root of your project directory, create the Pygame script named `pygame_script.py`: | ||
|
||
```python | ||
import pygame | ||
import sys | ||
|
||
# Initialize Pygame | ||
pygame.init() | ||
|
||
# Set up the display | ||
screen = pygame.display.set_mode((640, 480)) | ||
pygame.display.set_caption('Pygame Window') | ||
|
||
# Main game loop | ||
running = True | ||
while running: | ||
for event in pygame.event.get(): | ||
if event.type == pygame.QUIT: | ||
running = False | ||
|
||
# Fill the screen with a color | ||
screen.fill((0, 128, 255)) | ||
|
||
# Update the display | ||
pygame.display.flip() | ||
|
||
# Quit Pygame | ||
pygame.quit() | ||
sys.exit() | ||
``` | ||
|
||
### Step 5: Open the Project in a Development Container | ||
|
||
1. **Open your project folder** in Visual Studio Code. | ||
2. **Open the Command Palette** (F1) and select `Remote-Containers: Reopen in Container`. | ||
|
||
Visual Studio Code will build the container based on the `Dockerfile` and `devcontainer.json` configuration. It will then open the project inside the development container. | ||
|
||
### Step 6: Run the Pygame Script | ||
|
||
1. **Open a terminal** in Visual Studio Code (inside the container). | ||
2. **Run the Pygame script**: | ||
|
||
```sh | ||
python pygame_script.py | ||
``` | ||
|
||
The Pygame window should appear on your Windows desktop, managed by the X server (VcXsrv). | ||
|
||
### Summary | ||
|
||
By following these steps, you can create a development container for running a Pygame window on Windows 11 using Visual Studio Code and a `devcontainer.json` configuration. This setup leverages the benefits of development containers, such as isolation and portability, while still allowing you to run graphical applications. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import pygame | ||
import sys | ||
|
||
# Initialize Pygame | ||
pygame.init() | ||
|
||
# Set up the display | ||
screen = pygame.display.set_mode((640, 480)) | ||
pygame.display.set_caption('Pygame Window') | ||
|
||
# Main game loop | ||
running = True | ||
while running: | ||
for event in pygame.event.get(): | ||
if event.type == pygame.QUIT: | ||
running = False | ||
|
||
# Fill the screen with a color | ||
screen.fill((0, 128, 255)) | ||
|
||
# Update the display | ||
pygame.display.flip() | ||
|
||
# Quit Pygame | ||
pygame.quit() | ||
sys.exit() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
# Devcontainer | ||
|
||
Creating a Docker image that can create a Pygame window on Windows 11 involves several steps. Pygame is a popular set of Python modules designed for writing video games. It requires access to graphical libraries which can be a bit tricky to set up in Docker because Docker containers are typically run in headless environments (without a graphical interface). However, it can be done with the help of X server. | ||
|
||
Here are the steps to create a Docker image for running a Pygame window on Windows 11: | ||
|
||
1. **Install Docker**: Ensure that Docker is installed on your Windows 11 machine. You can download and install Docker Desktop from the official Docker website. | ||
|
||
2. **Create Dockerfile**: Create a `Dockerfile` to specify the environment and dependencies. | ||
|
||
3. **Configure X Server**: On Windows, you will need an X server to display the Pygame window. Xming or VcXsrv are common choices for Windows. Install and configure an X server to allow connections from your Docker container. | ||
|
||
4. **Run Docker Container**: Start the Docker container and ensure it can connect to the X server on your Windows host. | ||
|
||
Here is a step-by-step guide: | ||
|
||
### Step 1: Install X Server on Windows | ||
|
||
1. **Download and Install VcXsrv**: | ||
- Download VcXsrv from [sourceforge.net](https://sourceforge.net/projects/vcxsrv/). | ||
- Install VcXsrv using the default settings. | ||
- Run VcXsrv, selecting the "Multiple windows" option, and ensure the "Disable access control" option is checked. | ||
|
||
### Step 2: Create the Dockerfile | ||
|
||
Create a directory for your Docker setup and create a file named `Dockerfile` with the following content: | ||
|
||
```Dockerfile | ||
# Use the official Python base image | ||
FROM python:3.9 | ||
|
||
# Install necessary dependencies | ||
RUN apt-get update && \ | ||
apt-get install -y python3-pip python3-dev \ | ||
python3-opengl libsdl2-dev libsdl2-image-dev libsdl2-mixer-dev libsdl2-ttf-dev \ | ||
libsmpeg-dev libportmidi-dev libavformat-dev libswscale-dev libjpeg-dev libfreetype6-dev | ||
|
||
# Install Pygame | ||
RUN pip install pygame | ||
|
||
# Set the display environment variable | ||
ENV DISPLAY=host.docker.internal:0.0 | ||
|
||
# Copy the Pygame script into the container | ||
COPY pygame_script.py /usr/src/app/ | ||
|
||
# Set the working directory | ||
WORKDIR /usr/src/app | ||
|
||
# Run the Pygame script | ||
CMD ["python", "pygame_script.py"] | ||
``` | ||
|
||
### Step 3: Create a Pygame Script | ||
|
||
In the same directory, create a simple Pygame script named `pygame_script.py`: | ||
|
||
```python | ||
import pygame | ||
import sys | ||
|
||
# Initialize Pygame | ||
pygame.init() | ||
|
||
# Set up the display | ||
screen = pygame.display.set_mode((640, 480)) | ||
pygame.display.set_caption('Pygame Window') | ||
|
||
# Main game loop | ||
running = True | ||
while running: | ||
for event in pygame.event.get(): | ||
if event.type == pygame.QUIT: | ||
running = False | ||
|
||
# Fill the screen with a color | ||
screen.fill((0, 128, 255)) | ||
|
||
# Update the display | ||
pygame.display.flip() | ||
|
||
# Quit Pygame | ||
pygame.quit() | ||
sys.exit() | ||
``` | ||
|
||
### Step 4: Build and Run the Docker Container | ||
|
||
1. **Build the Docker Image**: | ||
Open a terminal in the directory containing your `Dockerfile` and `pygame_script.py` and run: | ||
|
||
```sh | ||
docker build -t pygame-app . | ||
``` | ||
|
||
2. **Run the Docker Container**: | ||
Ensure your X server (VcXsrv) is running on Windows, then run the Docker container: | ||
|
||
```sh | ||
docker run --rm -e DISPLAY=host.docker.internal:0.0 pygame-app | ||
``` | ||
|
||
This command sets the `DISPLAY` environment variable to use the X server running on your Windows host. The `--rm` flag ensures the container is removed after it stops. | ||
|
||
You should see the Pygame window appear on your Windows desktop, managed by the X server. | ||
|
||
### Summary | ||
|
||
By following these steps, you should be able to create a Docker image that can display a Pygame window on Windows 11 using an X server like VcXsrv. This setup allows you to leverage Docker's isolation and portability while still being able to run graphical applications. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/bin/bash | ||
|
||
git submodule update --init --recursive | ||
pip install -r requirements.txt | ||
pip install -r fast-pysf/requirements.txt | ||
pip install -e . | ||
pip install -e fast-pysf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters