Skip to content
This repository was archived by the owner on Nov 30, 2023. It is now read-only.

Commit e619bca

Browse files
authored
Adding PostgreSQL support to Rust Dev Container (#1260)
1 parent d64dc90 commit e619bca

File tree

14 files changed

+462
-0
lines changed

14 files changed

+462
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[build]
2+
rustflags = ["-C", "link-arg=-fuse-ld=lld"]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
POSTGRES_USER=postgres
2+
POSTGRES_PASSWORD=postgres
3+
POSTGRES_DB=postgres
4+
POSTGRES_HOSTNAME=localhost
5+
POSTGRES_PORT=5432
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# [Choice] Debian OS version (use bullseye on local arm64/Apple Silicon): buster, bullseye
2+
ARG VARIANT="bullseye"
3+
FROM mcr.microsoft.com/vscode/devcontainers/rust:1-${VARIANT}
4+
5+
# Include lld linker to improve build times either by using environment variable
6+
# RUSTFLAGS="-C link-arg=-fuse-ld=lld" or with Cargo's configuration file (i.e see .cargo/config.toml).
7+
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
8+
&& apt-get -y install clang lld \
9+
&& apt-get autoremove -y && apt-get clean -y
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "Rust and PostgreSQL",
3+
"dockerComposeFile": "docker-compose.yml",
4+
"service": "app",
5+
"workspaceFolder": "/workspace",
6+
// Set *default* container specific settings.json values on container create.
7+
"settings": {
8+
"lldb.executable": "/usr/bin/lldb",
9+
// VS Code don't watch files under ./target
10+
"files.watcherExclude": {
11+
"**/target/**": true
12+
},
13+
"rust-analyzer.checkOnSave.command": "clippy"
14+
},
15+
16+
// Add the IDs of extensions you want installed when the container is created.
17+
"extensions": [
18+
"vadimcn.vscode-lldb",
19+
"mutantdino.resourcemonitor",
20+
"matklad.rust-analyzer",
21+
"tamasfe.even-better-toml",
22+
"serayuzgur.crates"
23+
],
24+
25+
// Use 'forwardPorts' to make a list of ports inside the container available locally.
26+
// "forwardPorts": [5432],
27+
28+
// Use 'postCreateCommand' to run commands after the container is created.
29+
// "postCreateCommand": "rustc --version",
30+
31+
// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
32+
"remoteUser": "vscode"
33+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
version: '3.8'
2+
3+
volumes:
4+
postgres-data:
5+
6+
services:
7+
app:
8+
build:
9+
context: .
10+
dockerfile: Dockerfile
11+
args:
12+
# Use the VARIANT arg to pick a Debian OS version: buster, bullseye
13+
# Use bullseye when on local on arm64/Apple Silicon.
14+
VARIANT: bullseye
15+
env_file:
16+
# Ensure that the variables in .env match the same variables in devcontainer.json
17+
- .env
18+
# Security Opt and cap_add for C++ based debuggers to work.
19+
# See `runArgs`: https://github.com/Microsoft/vscode-docs/blob/main/docs/remote/devcontainerjson-reference.md
20+
# security_opt:
21+
# - seccomp:unconfined
22+
# cap_add:
23+
# - SYS_PTRACE
24+
25+
volumes:
26+
- ..:/workspace:cached
27+
28+
# Overrides default command so things don't shut down after the process ends.
29+
command: sleep infinity
30+
31+
# Runs app on the same network as the database container, allows "forwardPorts" in devcontainer.json function.
32+
network_mode: service:db
33+
34+
# Uncomment the next line to use a non-root user for all processes.
35+
# user: vscode
36+
37+
# Use "forwardPorts" in **devcontainer.json** to forward an app port locally.
38+
# (Adding the "ports" property to this file will not forward from a Codespace.)
39+
40+
db:
41+
image: postgres:14.1
42+
restart: unless-stopped
43+
volumes:
44+
- postgres-data:/var/lib/postgresql/data
45+
env_file:
46+
# Ensure that the variables in .env match the same variables in devcontainer.json
47+
- .env
48+
49+
# Add "forwardPorts": ["5432"] to **devcontainer.json** to forward PostgreSQL locally.
50+
# (Adding the "ports" property to this file will not forward from a Codespace.)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
README.md
2+
test-project
3+
history
4+
definition-manifest.json
5+
.devcontainer/library-scripts
6+
target
7+
.vscode
8+
.npmignore
9+
Cargo.lock
10+
Cargo.toml
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "lldb",
9+
"request": "launch",
10+
"name": "Debug executable",
11+
"cargo": {
12+
"args": [
13+
"build",
14+
"--bin=hello_remote_world",
15+
"--package=hello_remote_world",
16+
"--manifest-path=test-project/Cargo.toml"
17+
],
18+
"filter": {
19+
"kind": "bin"
20+
}
21+
},
22+
"args": []
23+
},
24+
{
25+
"type": "lldb",
26+
"request": "launch",
27+
"name": "Debug Test",
28+
"cargo": {
29+
"args": [
30+
"test",
31+
"--test",
32+
"*",
33+
"--manifest-path=test-project/Cargo.toml"
34+
],
35+
"filter": {
36+
"kind": "test"
37+
}
38+
},
39+
"args": []
40+
}
41+
]
42+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"search.exclude": {
3+
"**/target": true
4+
},
5+
"lldb.verboseLogging": true,
6+
"lldb.launch.initCommands": ["settings set target.disable-aslr false"]
7+
}

containers/rust-postgres/README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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).
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "hello_remote_world"
3+
version = "0.1.0"
4+
authors = ["VS Code <[email protected]>"]
5+
6+
[dev-dependencies]
7+
postgres = "0.19"

0 commit comments

Comments
 (0)