Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the devcontainer configuration to use docker compose instead of collocated services #48891

Merged
merged 4 commits into from Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 5 additions & 6 deletions .devcontainer/Dockerfile
@@ -1,19 +1,18 @@
# See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.191.1/containers/ruby/.devcontainer/base.Dockerfile

# [Choice] Ruby version: 3, 3.0, 2, 2.7, 2.6
ARG VARIANT="3.0"
FROM mcr.microsoft.com/devcontainers/ruby:0-${VARIANT}
ARG VARIANT="3"
FROM mcr.microsoft.com/devcontainers/ruby:${VARIANT}

# [Choice] Node.js version: none, lts/*, 16, 14, 12, 10
ARG NODE_VERSION="none"
ARG NODE_VERSION="lts/*"
RUN if [ "${NODE_VERSION}" != "none" ]; then su vscode -c "umask 0002 && . /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; fi

# [Optional] Uncomment this section to install additional OS packages.
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
&& apt-get -y install --no-install-recommends \
mariadb-server libmariadb-dev \
postgresql postgresql-client postgresql-contrib libpq-dev \
redis-server memcached \
mariadb-client libmariadb-dev \
postgresql-client postgresql-contrib libpq-dev \
ffmpeg mupdf mupdf-tools libvips poppler-utils


Expand Down
16 changes: 5 additions & 11 deletions .devcontainer/boot.sh
Expand Up @@ -3,16 +3,10 @@ yarn install

sudo chown -R vscode:vscode /usr/local/bundle

sudo service postgresql start
sudo service mariadb start
sudo service redis-server start
sudo service memcached start
cd activerecord

# Create PostgreSQL users and databases
sudo su postgres -c "createuser --superuser vscode"
sudo su postgres -c "createdb -O vscode -E UTF8 -T template0 activerecord_unittest"
sudo su postgres -c "createdb -O vscode -E UTF8 -T template0 activerecord_unittest2"
# Create PostgreSQL databases
bundle exec rake db:postgresql:rebuild

# Create MySQL database and databases
cd activerecord
MYSQL_CODESPACES=1 bundle exec rake db:mysql:build
# Create MySQL databases
MYSQL_CODESPACES=1 bundle exec rake db:mysql:rebuild
49 changes: 24 additions & 25 deletions .devcontainer/devcontainer.json
@@ -1,24 +1,33 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.191.1/containers/ruby
// For format details, see https://aka.ms/devcontainer.json.
{
"name": "Ruby",
"build": {
"dockerfile": "Dockerfile",
"context": "..",
"args": {
// Update 'VARIANT' to pick a Ruby version: 3, 3.0, 2, 2.7, 2.6
"VARIANT": "3",
// Options
"NODE_VERSION": "lts/*"
}
},
"name": "Rails project development",
"dockerComposeFile": "docker-compose.yml",
"service": "rails",
"workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",

// Features to add to the dev container. More info: https://containers.dev/features.
"features": {
"ghcr.io/devcontainers/features/github-cli:1": {
"version": "latest"
}
},

"containerEnv": {
"PGHOST": "postgres",
"PGUSER": "postgres",
"PGPASSWORD": "postgres",
"MYSQL_HOST": "mariadb",
"REDIS_URL": "redis://redis/0",
"MEMCACHE_SERVERS": "memcached:11211"
},

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// This can be used to network with other containers or the host.
// "forwardPorts": [3000, 5432],

// Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": ".devcontainer/boot.sh",

// Configure tool-specific properties.
"customizations": {
"vscode": {
Expand All @@ -29,16 +38,6 @@
}
},

"containerEnv": {
"MYSQL_SOCK": "/run/mysqld/mysqld.sock"
},

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

// Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": ".devcontainer/boot.sh",

// Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "vscode"
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}
71 changes: 71 additions & 0 deletions .devcontainer/docker-compose.yml
@@ -0,0 +1,71 @@
version: '3'

services:
rails:
build:
context: ..
dockerfile: .devcontainer/Dockerfile

volumes:
- ../..:/workspaces:cached

# Overrides default command so things don't shut down after the process ends.
command: sleep infinity

# Runs app on the same network as the database container, allows "forwardPorts" in devcontainer.json function.
networks:
- default

depends_on:
- postgres
- mariadb
- redis
- memcached

# Use "forwardPorts" in **devcontainer.json** to forward an app port locally.
# (Adding the "ports" property to this file will not forward from a Codespace.)

postgres:
image: postgres:latest
restart: unless-stopped
networks:
- default
volumes:
- postgres-data:/var/lib/postgresql/data
environment:
POSTGRES_USER: postgres
POSTGRES_DB: postgres
POSTGRES_PASSWORD: postgres

mariadb:
image: mariadb:latest
restart: unless-stopped
networks:
- default
volumes:
- mariadb-data:/var/lib/mysql
environment:
MARIADB_ROOT_PASSWORD: root

redis:
image: redis:latest
restart: unless-stopped
networks:
- default
volumes:
- redis-data:/data

memcached:
image: memcached:latest
restart: unless-stopped
command: ["-m", "1024"]
networks:
- default

networks:
default:

volumes:
postgres-data:
mariadb-data:
redis-data:
18 changes: 7 additions & 11 deletions activerecord/Rakefile
Expand Up @@ -226,21 +226,17 @@ namespace :db do
desc "Create the MySQL Rails User"
task :build_user do
if ENV["MYSQL_CODESPACES"]
if ENV["MYSQL_SOCK"]
mysql_command = "sudo mysql -uroot -S #{ENV["MYSQL_SOCK"]} -e"
else
mysql_command = "sudo mysql -uroot -proot -e"
end
mysql_command = "mysql -uroot -proot -e"
else
mysql_command = "mysql -uroot -e"
end

mysql_configs.each do |config|
%x( #{mysql_command} "CREATE USER IF NOT EXISTS '#{config["arunit"]["username"]}'@'localhost';" )
%x( #{mysql_command} "CREATE USER IF NOT EXISTS '#{config["arunit2"]["username"]}'@'localhost';" )
%x( #{mysql_command} "GRANT ALL PRIVILEGES ON #{config["arunit"]["database"]}.* to '#{config["arunit"]["username"]}'@'localhost'" )
%x( #{mysql_command} "GRANT ALL PRIVILEGES ON #{config["arunit2"]["database"]}.* to '#{config["arunit2"]["username"]}'@'localhost'" )
%x( #{mysql_command} "GRANT ALL PRIVILEGES ON inexistent_activerecord_unittest.* to '#{config["arunit"]["username"]}'@'localhost';" )
%x( #{mysql_command} "CREATE USER IF NOT EXISTS '#{config["arunit"]["username"]}'@'%';" )
%x( #{mysql_command} "CREATE USER IF NOT EXISTS '#{config["arunit2"]["username"]}'@'%';" )
%x( #{mysql_command} "GRANT ALL PRIVILEGES ON #{config["arunit"]["database"]}.* to '#{config["arunit"]["username"]}'@'%'" )
%x( #{mysql_command} "GRANT ALL PRIVILEGES ON #{config["arunit2"]["database"]}.* to '#{config["arunit2"]["username"]}'@'%'" )
%x( #{mysql_command} "GRANT ALL PRIVILEGES ON inexistent_activerecord_unittest.* to '#{config["arunit"]["username"]}'@'%';" )
end
end

Expand All @@ -253,7 +249,7 @@ namespace :db do
end

desc "Drop the MySQL test databases"
task :drop do
task drop: ["db:mysql:build_user"] do
%x( mysql #{mysql2_connection_arguments["arunit"]} -e "drop database IF EXISTS #{mysql2_config["arunit"]["database"]}" )
%x( mysql #{mysql2_connection_arguments["arunit2"]} -e "drop database IF EXISTS #{mysql2_config["arunit2"]["database"]}" )

Expand Down