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

feat(cli): Move docker setup out of experimental #11072

Merged
merged 10 commits into from
Jul 24, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changesets/11072.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- feat(cli): Move docker setup out of experimental (#11072) by @Josh-Walker-GM

This change introduces `yarn rw setup docker`. This is a result of moving our docker setup command out of it's experimental phase.
17 changes: 7 additions & 10 deletions docs/docs/docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,14 @@ description: Redwood's Dockerfile

# Docker

:::note The Dockerfile is experimental

Redwood's Dockerfile is the collective effort of several hard-working community members.
We've worked hard to optimize it, but expect changes as we collaborate with users and deploy providers.

:::

If you're not familiar with Docker, we recommend going through their [getting started](https://docs.docker.com/get-started/) documentation.

## Set up

To get started, run the setup command:

```
yarn rw experimental setup-docker
yarn rw setup docker
```

The setup commands does several things:
Expand Down Expand Up @@ -55,14 +48,18 @@ docker compose -f ./docker-compose.dev.yml run --rm -it console /bin/bash
root@...:/home/node/app# yarn rw prisma migrate dev
```

:::info database choice
The docker setup command assumes that you are using Postgres as your database provider and sets up a local Postgres database for you. You may have to switch from SQLite to Postgres if you have not done so and want to continue with the default setup.
:::

:::important
If you are using a [Server File](#using-the-server-file) then you should [change the command](#command) that runs the `api_serve` service.
:::

## Dockerfile

The documentation here goes through and explains every line of Redwood's Dockerfile.
If you'd like to see the whole Dockerfile for reference, you can find it [here](https://github.com/redwoodjs/redwood/tree/main/packages/cli/src/commands/experimental/templates/docker/Dockerfile) or by setting it up in your project: `yarn rw experimental setup-docker`.
If you'd like to see the whole Dockerfile for reference, you can find it [here](https://github.com/redwoodjs/redwood/tree/main/packages/cli/src/commands/setup/docker/templates/Dockerfile) or by setting it up in your project: `yarn rw setup docker`.

Redwood takes advantage of [Docker's multi-stage build support](https://docs.docker.com/build/building/multi-stage/) to keep the final production images lean.

Expand Down Expand Up @@ -285,7 +282,7 @@ Not updating the command will not completely configure the GraphQL Server and no
:::


Note that the Redwood CLI isn't available anymore. (It's a dev dependency.)
Note that the Redwood CLI isn't available anymore because it is a dev dependency.
To access the server bin, we have to find its path in `node_modules`.
Though this is somewhat discouraged in modern yarn, since we're using the `node-modules` node linker, it's in `node_modules/.bin`.

Expand Down
31 changes: 0 additions & 31 deletions packages/cli/src/commands/experimental/setupDocker.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { vi, test, describe, expect } from 'vitest'

import { recordTelemetryAttributes } from '@redwoodjs/cli-helpers'

import { command, description, builder, handler } from '../setupDocker'
import { command, description, builder, handler } from '../docker'

vi.mock('../setupDockerHandler.js')
vi.mock('../dockerHandler.js')

vi.mock('@redwoodjs/cli-helpers', () => {
return {
Expand All @@ -14,12 +14,12 @@ vi.mock('@redwoodjs/cli-helpers', () => {

describe('setupDocker', () => {
test("command didn't change unintentionally", () => {
expect(command).toMatchInlineSnapshot(`"setup-docker"`)
expect(command).toMatchInlineSnapshot(`"docker"`)
})

test("description didn't change unintentionally", () => {
expect(description).toMatchInlineSnapshot(
`"Setup the experimental Dockerfile"`,
`"Setup the default Redwood Dockerfile"`,
)
})

Expand All @@ -46,7 +46,7 @@ describe('setupDocker', () => {
await handler({})

expect(recordTelemetryAttributes).toHaveBeenCalledWith({
command: 'experimental setup-docker',
command: 'setup docker',
force: undefined,
verbose: undefined,
})
Expand Down
25 changes: 25 additions & 0 deletions packages/cli/src/commands/setup/docker/docker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { recordTelemetryAttributes } from '@redwoodjs/cli-helpers'

export const command = 'docker'

export const description = 'Setup the default Redwood Dockerfile'

export function builder(yargs) {
yargs.option('force', {
alias: 'f',
default: false,
description: 'Overwrite existing configuration',
type: 'boolean',
})
}

export async function handler(options) {
recordTelemetryAttributes({
command: 'setup docker',
force: options.force,
verbose: options.verbose,
})

const { handler } = await import('./dockerHandler.js')
return handler(options)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import { writeFile } from '@redwoodjs/cli-helpers'
import { getConfig, getConfigPath, getPaths } from '@redwoodjs/project-config'
import { errorTelemetry } from '@redwoodjs/telemetry'

import c from '../../lib/colors'
import c from '../../../lib/colors'

export async function handler({ force }) {
const TEMPLATE_DIR = path.join(__dirname, 'templates', 'docker')
const TEMPLATE_DIR = path.join(__dirname, 'templates')

let dockerfileTemplateContent = fs.readFileSync(
path.resolve(TEMPLATE_DIR, 'Dockerfile'),
Expand Down Expand Up @@ -43,21 +43,6 @@ export async function handler({ force }) {

const tasks = new Listr(
[
{
title: 'Confirmation',
task: async (_ctx, task) => {
const confirmation = await task.prompt({
type: 'Confirm',
message: 'The Dockerfile is experimental. Continue?',
})

if (!confirmation) {
throw new Error('User aborted')
}
},
skip: force,
},

{
title: 'Adding the official yarn workspace-tools plugin...',
task: async (_ctx, task) => {
Expand All @@ -83,7 +68,6 @@ export async function handler({ force }) {
}).stdout
},
},

{
title: 'Adding @redwoodjs/api-server and @redwoodjs/web-server...',
task: async (_ctx, task) => {
Expand Down Expand Up @@ -137,9 +121,8 @@ export async function handler({ force }) {
}).stdout
},
},

{
title: 'Adding the experimental Dockerfile and compose files...',
title: 'Adding the Dockerfile and compose files...',
task: (_ctx, task) => {
const shouldSkip = [
dockerfilePath,
Expand Down Expand Up @@ -217,7 +200,6 @@ export async function handler({ force }) {
)
},
},

{
title: 'Adding postgres to .gitignore...',
task: (_ctx, task) => {
Expand All @@ -239,48 +221,27 @@ export async function handler({ force }) {
)
},
},

{
title: 'Adding config to redwood.toml...',
task: (_ctx, task) => {
task: () => {
const redwoodTomlPath = getConfigPath()
let configContent = fs.readFileSync(redwoodTomlPath, 'utf-8')

const browserOpenRegExp = /open\s*=\s*true/

const hasOpenSetToTrue = browserOpenRegExp.test(configContent)
const hasExperimentalDockerfileConfig = configContent.includes(
'[experimental.dockerfile]',
)

if (!hasOpenSetToTrue && hasExperimentalDockerfileConfig) {
task.skip(
`The [experimental.dockerfile] config block already exists in your 'redwood.toml' file`,
)
return
}

if (hasOpenSetToTrue) {
if (browserOpenRegExp.test(configContent)) {
configContent = configContent.replace(
/open\s*=\s*true/,
'open = false',
)
}

if (!hasExperimentalDockerfileConfig) {
configContent = configContent.concat(
`\n[experimental.dockerfile]\n\tenabled = true\n`,
)
}

// using string replace here to preserve comments and formatting.
writeFile(redwoodTomlPath, configContent, {
existingFiles: 'OVERWRITE',
})
},
},
],

{
renderer: process.env.NODE_ENV === 'test' ? 'verbose' : 'default',
},
Expand Down Expand Up @@ -308,6 +269,7 @@ export async function handler({ force }) {
' docker compose -f ./docker-compose.dev.yml run --rm -it console /bin/bash',
' root@...:/home/node/app# yarn rw prisma migrate dev',
'',
"We assume you're using Postgres. If you're not, you'll need to make other changes to switch over.",
"Lastly, ensure you have Docker. If you don't, see https://docs.docker.com/desktop/",
'',
"There's a lot in the Dockerfile and there's a reason for every line.",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: "3.8"

services:
redwood:
build:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: "3.8"

services:
api:
build:
Expand Down
Loading