Skip to content

Commit a77a048

Browse files
authored
Merge pull request #6424 from pablobm/devcontainer
Support for Development Containers (devcontainers)
2 parents 84df887 + 3a1c693 commit a77a048

20 files changed

+365
-19
lines changed

.devcontainer/Dockerfile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Make sure RUBY_VERSION matches the Ruby version in .ruby-version
2+
ARG RUBY_VERSION=3.4.7
3+
FROM ghcr.io/rails/devcontainer/images/ruby:$RUBY_VERSION
4+
5+
USER root
6+
ENV DEBIAN_FRONTEND=noninteractive
7+
8+
# Required on Windows
9+
RUN mkdir -p /workspaces/openstreetmap-website \
10+
&& chown -R vscode:vscode /workspaces/openstreetmap-website
11+
12+
# Install system packages then clean up to minimize image size
13+
RUN apt-get update \
14+
&& apt-get install --no-install-recommends -y \
15+
file \
16+
libarchive-dev \
17+
libgd-dev \
18+
osmosis \
19+
&& apt-get clean \
20+
&& apt-get distclean

.devcontainer/compose.yaml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
name: "open_street_map"
2+
3+
services:
4+
rails-app:
5+
build:
6+
context: ..
7+
dockerfile: .devcontainer/Dockerfile
8+
9+
volumes:
10+
- ../..:/workspaces:cached
11+
- mise-cache:/home/vscode/.local/share/mise
12+
13+
# Overrides default command so things don't shut down after the process ends.
14+
command: sleep infinity
15+
16+
# Uncomment the next line to use a non-root user for all processes.
17+
# user: vscode
18+
19+
# Use "forwardPorts" in **devcontainer.json** to forward an app port locally.
20+
# (Adding the "ports" property to this file will not forward from a Codespace.)
21+
depends_on:
22+
- selenium-default
23+
- selenium-de
24+
- selenium-nolang
25+
- postgres
26+
27+
selenium-default:
28+
image: selenium/standalone-firefox
29+
restart: unless-stopped
30+
31+
selenium-de:
32+
image: selenium/standalone-firefox
33+
restart: unless-stopped
34+
35+
selenium-nolang:
36+
image: selenium/standalone-firefox
37+
restart: unless-stopped
38+
39+
postgres:
40+
image: postgres:16.1
41+
restart: unless-stopped
42+
networks:
43+
- default
44+
volumes:
45+
- postgres-data:/var/lib/postgresql/data
46+
environment:
47+
POSTGRES_DB: openstreetmap
48+
POSTGRES_USER: openstreetmap
49+
POSTGRES_PASSWORD: openstreetmap
50+
51+
volumes:
52+
postgres-data:
53+
mise-cache:

.devcontainer/devcontainer.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// For format details, see https://containers.dev/implementors/json_reference/.
2+
// For config options, see the README at: https://github.com/devcontainers/templates/tree/main/src/ruby
3+
{
4+
"name": "open_street_map",
5+
"dockerComposeFile": "compose.yaml",
6+
"service": "rails-app",
7+
"workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",
8+
9+
// Features to add to the dev container. More info: https://containers.dev/features.
10+
"features": {
11+
"ghcr.io/devcontainers/features/github-cli:1": {},
12+
"ghcr.io/devcontainers/features/node:1": {},
13+
"ghcr.io/rails/devcontainer/features/activestorage": {},
14+
"ghcr.io/rails/devcontainer/features/postgres-client": {}
15+
},
16+
17+
"containerEnv": {
18+
"CAPYBARA_SERVER_PORT": "45678",
19+
"DB_HOST": "postgres"
20+
},
21+
22+
// Use 'forwardPorts' to make a list of ports inside the container available locally.
23+
"forwardPorts": [3000, 5432],
24+
25+
// Configure tool-specific properties.
26+
// "customizations": {},
27+
28+
// Uncomment to connect as root instead. More info: https://containers.dev/implementors/json_reference/#remoteUser.
29+
// "remoteUser": "root",
30+
31+
32+
// Use 'postCreateCommand' to run commands after the container is created.
33+
"postCreateCommand": ".devcontainer/start"
34+
}

.devcontainer/start

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env sh
2+
3+
# This setup uses https://mise.jdx.dev to manage language versions.
4+
# Mark the config file as trusted to avoid an interactive prompt.
5+
mise trust /workspaces/mise.local.toml
6+
7+
cp config/devcontainer.database.yml config/database.yml
8+
cp config/example.storage.yml config/storage.yml
9+
touch config/settings.local.yml
10+
11+
bin/setup --skip-server

.github/workflows/devcontainer.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: devcontainer
2+
3+
on:
4+
- push
5+
- pull_request
6+
7+
concurrency:
8+
group: ${{ github.workflow }}-${{ github.head_ref || github.ref }}
9+
cancel-in-progress: true
10+
11+
jobs:
12+
test:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout (GitHub)
16+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
17+
18+
- name: Build and run dev container task
19+
uses: devcontainers/ci@8bf61b26e9c3a98f69cb6ce2f88d24ff59b785c6 # v0.3.1900000417
20+
with:
21+
runCmd: bin/rails test:all

DEVCONTAINER.md

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# Using Development Containers for development and testing
2+
3+
You can set up a development environment for this project using [Development Containers](https://containers.dev/), aka devcontainers.
4+
5+
There are different ways of working with devcontainers. Some are automated and integrated in IDE applications, while others are more manual and require CLI tools. This guide will use [Visual Studio Code](https://code.visualstudio.com) and assumes that it's installed already.
6+
7+
Be aware that the containers will take significant space in your hard drive, in the region of 6GB.
8+
9+
## Install Git
10+
11+
You will need Git to clone (download) the code repository and push changes. How to install Git will depend on your operating system.
12+
13+
### Ubuntu 24.04 LTS, Debian 13 (Trixie)
14+
15+
```bash
16+
sudo apt-get update
17+
sudo apt-get install git-core
18+
```
19+
20+
### Fedora
21+
22+
```bash
23+
sudo dnf install git
24+
```
25+
26+
### macOS
27+
28+
For macOS, you will need [Xcode Command Line Tools](https://mac.install.guide/commandlinetools/); macOS 14 (Sonoma) or later; and some familiarity with Unix development via the Terminal.
29+
30+
### Windows
31+
32+
You can install Git using [WinGet](https://learn.microsoft.com/en-gb/windows/package-manager/winget/):
33+
34+
```bash
35+
winget install --id Git.Git -e --source winget
36+
```
37+
38+
Alternatively, see [other install options](https://git-scm.com/install/windows).
39+
40+
## Install Docker
41+
42+
You will need to install Docker in order to run devcontainers. Again, the methods will depend on your operating system.
43+
44+
In general: if you get an error about not having permission to use Docker, you may need to add your user to the `docker` group. The details vary, but often this is a case of running the following command:
45+
46+
```bash
47+
sudo usermod --append --group docker $USERNAME
48+
```
49+
50+
Substitute `$USERNAME` with your actual username in the command above. After that, reboot your computer to ensure that this change takes effect.
51+
52+
### Windows
53+
54+
1. Use Docker Desktop via [docker.com Download](https://www.docker.com/products/docker-desktop/).
55+
56+
2. You have to enable git symlinks before cloning the repository.
57+
This repository uses symbolic links that are not enabled by default on Windows git. To enable them, [turn on Developer Mode](https://windowsreport.com/windows-11-developer-mode/) on Windows and run `git config --global core.symlinks true` to enable symlinks in Git. See [this StackOverflow question](https://stackoverflow.com/questions/5917249/git-symbolic-links-in-windows) for more information.
58+
59+
### Mac
60+
61+
- Use Docker Desktop via [docker.com Download](https://www.docker.com/products/docker-desktop/).
62+
- Or [Homebrew](https://formulae.brew.sh/cask/docker).
63+
64+
### Linux
65+
66+
Use [Docker Engine](https://docs.docker.com/engine/install/ubuntu/) with the [docker-compose-plugin](https://docs.docker.com/compose/install/linux/)
67+
68+
## Clone the repository
69+
70+
The repository is reasonably large (~560MB) and it's unlikely that you'll need the full history. Therefore you can probably do with a shallow clone (~100MB):
71+
```bash
72+
git clone --depth=1 https://github.com/openstreetmap/openstreetmap-website.git
73+
```
74+
75+
If you want to add in the full history later on, perhaps to run `git blame` or `git log`, run `git fetch --unshallow`.
76+
77+
> [!TIP]
78+
> To download the full history from the start, run:
79+
> ```bash
80+
> git clone https://github.com/openstreetmap/openstreetmap-website.git
81+
> ```
82+
83+
## Install Dev Containers extension
84+
85+
Start by opening the project with VS Code. Within it, you will need to install the extension _Dev Containers_, which can be done from the _Extensions_ section, reachable via the sidebar icons. Or VS Code may show a popup recommending this extension, with a button to install it directly.
86+
87+
![VS Code: panel to install extensions](./docs/assets/vscode-devcontainers-extension.png)
88+
89+
## Open the project in a container
90+
91+
If everything is correct, this should make a few new commands available to you within VS Code. Find and run the command "Dev Containers: Reopen in Container".
92+
93+
![VS Code: command to open in a devcontainer](./docs/assets/vscode-dev-reopen.png)
94+
95+
The first time you do this, it will go into a bit of a process. It will create the devcontainer, pull the Docker images, install the dependencies, etc. Go drink some water while this runs.
96+
97+
![VS Code: notification that VS Code is connecting to the Dev Container](./docs/assets/vscode-connecting-to-devcontainer.png)
98+
99+
Eventually this will present you with a development environment ready to go. In subsequent occasions this should be much faster.
100+
101+
## Done!
102+
103+
If everything went well, you are done! For example, now you can open a shell in this environment using the VS Studio command "Create New Terminal (With Profile)":
104+
105+
![VS Code: command to open a terminal](./docs/assets/vscode-create-terminal.png)
106+
107+
From this terminal, you can run the test suite with `bundle exec rails test:all`:
108+
109+
![Running the test suite in the terminal](./docs/assets/vscode-rails-test-all.png)
110+
111+
Hopefully it should be all green? 🤞 You can also start a development server with `bundle exec rails s`:
112+
113+
![Running the dev server in the terminal](./docs/assets/vscode-rails-server.png)
114+
115+
It will take a moment to start, after which you will be able to take a browser to http://localhost:3000 and visit your own local version of the site.
116+
117+
## Other useful configuration
118+
119+
See [`CONFIGURE.md`](CONFIGURE.md) for information on how to manage users and enable OAuth for iD, JOSM etc.
120+
121+
## Other tools
122+
123+
VS Code is not the only way to work with devcontainers. Other options include:
124+
125+
- [RubyMine](https://www.jetbrains.com/help/ruby/start-dev-container-inside-ide.html): another popular environment to work with Ruby.
126+
- [DevPod](https://devpod.sh): a CLI tool to work with devcontainers.
127+
- [VSCodium](https://vscodium.com): a Free/Libre alternative to VS Code.
128+
- [GitHub Codespaces](https://github.com/codespaces): GitHub's hosted IDE.
129+
130+
## Troubleshooting
131+
132+
### `‘ruby’: No such file or directory`
133+
134+
In some cases Ruby may not install correctly. If you see this message, run these two commands:
135+
136+
```
137+
mise install
138+
bundle install
139+
```
140+
141+
This has been observed particularly when using RubyMine.

INSTALL.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@ There is more than one way to set up a development environment.
88

99
### Containerized Installation
1010

11-
We offer a containerized environment with Docker which may avoid installation difficulties. See [DOCKER.md](DOCKER.md) for complete instructions.
11+
We offer containerized environments with Docker which may avoid installation difficulties:
12+
13+
- To use Docker manually, see [DOCKER](DOCKER.md).
14+
- To use Docker via [Development Containers](https://containers.dev) (devcontainers), see [DEVCONTAINER.md](DEVCONTAINER.md).
1215

1316
### Manual Installation
1417

1518
Install dependencies directly on your machine (traditional approach, covered in this guide). This gives you the most control and is often preferred by experienced developers on Linux systems.
1619

1720
> [!WARNING]
18-
> **Windows Note:** We don't recommend using this approach on Windows, as some Ruby gems may not be supported. If you are using Windows, we recommend containerized setup using [Docker](DOCKER.md).
21+
> **Windows Note:** We don't recommend using this approach on Windows, as some Ruby gems may not be supported. If you are using Windows, we recommend a containerized setup as mentioned above.
1922
2023
## Manual Installation Guide
2124

bin/setup

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ FileUtils.chdir APP_ROOT do
1616

1717
puts "== Installing dependencies =="
1818
system("bundle check") || system!("bundle install")
19+
system("yarn install --check-dependencies")
1920

2021
# puts "\n== Copying sample files =="
2122
# unless File.exist?("config/database.yml")

config/devcontainer.database.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# This configuration is tailored for use with devcontainers. See DEVCONTAINER.md for more information.
2+
3+
development:
4+
adapter: postgresql
5+
database: openstreetmap
6+
username: openstreetmap
7+
password: openstreetmap
8+
host: <%= ENV["DB_HOST"] %>
9+
encoding: utf8
10+
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
11+
12+
test:
13+
adapter: postgresql
14+
database: osm_test
15+
username: openstreetmap
16+
password: openstreetmap
17+
host: <%= ENV["DB_HOST"] %>
18+
encoding: utf8
19+
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
20+
15.9 KB
Loading

0 commit comments

Comments
 (0)