Skip to content

Commit

Permalink
Merge pull request #26 from ll7/25-create-a-devcontainer-for-development
Browse files Browse the repository at this point in the history
Fixes #25 create a devcontainer for development
  • Loading branch information
ll7 authored May 29, 2024
2 parents b346133 + 68db4ef commit 6b6c32f
Show file tree
Hide file tree
Showing 7 changed files with 314 additions and 2 deletions.
29 changes: 29 additions & 0 deletions .devcontainer/Dockerfile
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
34 changes: 34 additions & 0 deletions .devcontainer/devcontainer.json
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"
}
}
}
108 changes: 108 additions & 0 deletions .devcontainer/devcontainer.md
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.
26 changes: 26 additions & 0 deletions .devcontainer/pygame_script.py
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()
109 changes: 109 additions & 0 deletions .devcontainer/readme.md
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.
7 changes: 7 additions & 0 deletions postCreate.sh
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
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,12 @@ Pygments==2.17.2
pylint==3.0.3
pynvml==11.4.1
pyparsing==3.1.1
-e git+https://github.com/ll7/pysocialforce-ll7@1b3caf09ffe37784ef894f0530f715a39caea66e#egg=PySocialForce&subdirectory=../../fast-pysf
pytest==8.0.1
python-dateutil==2.8.2
pytz==2024.1
PyYAML==6.0.1
requests==2.31.0
rich==13.7.0
-e git+https://github.com/ll7/robot_sf_ll7@0d1e3d5537929d500ace5fa6badd4757731c54cf#egg=robot_sf
scalene==1.5.34
scipy==1.12.0
sentry-sdk==1.40.6
Expand All @@ -99,3 +97,4 @@ tzdata==2024.1
urllib3==2.2.1
wandb==0.16.4
Werkzeug==3.0.1
loguru==0.7.2

0 comments on commit 6b6c32f

Please sign in to comment.