|
| 1 | +# Rust & PostgreSQL |
| 2 | + |
| 3 | +## Summary |
| 4 | + |
| 5 | +*Develop applications with Rust and PostgreSQL. Includes a Rust application container and PostgreSQL server.* |
| 6 | + |
| 7 | +| Metadata | Value | |
| 8 | +|----------|-------| |
| 9 | +| *Contributors* | The VS Code Team | |
| 10 | +| *Categories* | Core, Languages | |
| 11 | +| *Definition type* | Docker Compose | |
| 12 | +| *Available image variants* | buster, bullseye ([full list](https://mcr.microsoft.com/v2/vscode/devcontainers/rust/tags/list)) | |
| 13 | +| *Works in Codespaces* | Yes | |
| 14 | +| *Container host OS support* | Linux, macOS, Windows | |
| 15 | +| *Container OS* | Debian | |
| 16 | +| *Languages, platforms* | Rust | |
| 17 | + |
| 18 | +## Using this definition |
| 19 | + |
| 20 | +This definition creates two containers, one for Rust and one for PostgreSQL. VS Code will attach to the Rust dev container, and from within that container the PostgreSQL container will be available on **`localhost`** port 5432. The `.env` file sets the default credentials. The default database is named `postgres` with a user of `postgres` whose password is `postgres`, and if desired this may be changed in `docker-compose.yml`. Data is stored in a volume named `postgres-data`. |
| 21 | + |
| 22 | +While the definition itself works unmodified, you can select the version of Debian the container uses by updating the `VARIANT` arg in `.devcontainer/docker-compose.yml` (and rebuilding if you've already created the container). |
| 23 | + |
| 24 | +```yaml |
| 25 | +build: |
| 26 | + context: . |
| 27 | + dockerfile: Dockerfile |
| 28 | + args: |
| 29 | + # Use the VARIANT arg to pick a Debian OS version: buster, bullseye |
| 30 | + # Use bullseye when on local on arm64/Apple Silicon. |
| 31 | + VARIANT: buster |
| 32 | +``` |
| 33 | +
|
| 34 | +### Adding the definition to a project or codespace |
| 35 | +
|
| 36 | +1. If this is your first time using a development container, please see getting started information on [setting up](https://aka.ms/vscode-remote/containers/getting-started) Remote-Containers or [creating a codespace](https://aka.ms/ghcs-open-codespace) using GitHub Codespaces. |
| 37 | +
|
| 38 | +2. Start VS Code and open your project folder or connect to a codespace. |
| 39 | +
|
| 40 | +3. Press <kbd>F1</kbd> select and **Add Development Container Configuration Files...** command for **Remote-Containers** or **Codespaces**. |
| 41 | +
|
| 42 | + > **Note:** If needed, you can drag-and-drop the `.devcontainer` folder from this sub-folder in a locally cloned copy of this repository into the VS Code file explorer instead of using the command. |
| 43 | + |
| 44 | +4. Select this definition. You may also need to select **Show All Definitions...** for it to appear. |
| 45 | + |
| 46 | +5. Finally, press <kbd>F1</kbd> and run **Remote-Containers: Reopen Folder in Container** or **Codespaces: Rebuild Container** to start using the definition. |
| 47 | + |
| 48 | +### Adding another service |
| 49 | + |
| 50 | +You can add other services to your `docker-compose.yml` file [as described in Docker's documentation](https://docs.docker.com/compose/compose-file/#service-configuration-reference). However, if you want anything running in this service to be available in the container on localhost, or want to forward the service locally, be sure to add this line to the service config: |
| 51 | + |
| 52 | +```yaml |
| 53 | +# Runs the service on the same network as the database container, allows "forwardPorts" in devcontainer.json function. |
| 54 | +network_mode: service:[$SERVICENAME] |
| 55 | +``` |
| 56 | + |
| 57 | +## Testing the definition |
| 58 | + |
| 59 | +This definition includes some test code that will help you verify it is working as expected on your system. Follow these steps: |
| 60 | + |
| 61 | +1. If this is your first time using a development container, please follow the [getting started steps](https://aka.ms/vscode-remote/containers/getting-started) to set up your machine. |
| 62 | +1. Clone this repository. |
| 63 | +1. Start VS Code, press <kbd>F1</kbd>, and select **Remote-Containers: Open Folder in Container...** |
| 64 | +1. Select the `containers/rust-postgres` folder. |
| 65 | +1. After the folder has opened in the container, press <kbd>F5</kbd> to start the project. |
| 66 | +1. You should see "Hello, VS Code Remote - Containers!" in the Debug Console after the program executes. |
| 67 | +1. You can also run [test.sh](test-project/test.sh) in order to build and test the project. |
| 68 | +1. From here, you can add breakpoints or edit the contents of the `test-project` folder to do further testing. |
| 69 | + |
| 70 | +## License |
| 71 | + |
| 72 | +Copyright (c) Microsoft Corporation. All rights reserved. |
| 73 | + |
| 74 | +Licensed under the MIT License. See [LICENSE](https://github.com/microsoft/vscode-dev-containers/blob/main/LICENSE). |
0 commit comments