Skip to content

Commit

Permalink
Merge pull request #149 from strapi/chore/beta
Browse files Browse the repository at this point in the history
Rebuild the docker images for strapi with beta
  • Loading branch information
alexandrebodin committed Oct 23, 2019
2 parents 44112fe + 037a16e commit e86c76c
Show file tree
Hide file tree
Showing 27 changed files with 1,772 additions and 160 deletions.
8 changes: 0 additions & 8 deletions .dockerignore

This file was deleted.

1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
examples
16 changes: 16 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = {
env: {
commonjs: true,
es6: true,
node: true,
},
extends: 'eslint:recommended',
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly',
},
parserOptions: {
ecmaVersion: 2018,
},
rules: {},
};
13 changes: 0 additions & 13 deletions .gitattributes

This file was deleted.

3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
app
db
node_modules
10 changes: 5 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
language: node_js
node_js:
- 12

sudo: required

services:
- docker

script:
- docker build -t strapi/strapi .
- docker-compose up -d
- sleep 60
- curl -f localhost:1337
- docker logs strapidocker_api_1
- ./bin/build.js --type=all
27 changes: 0 additions & 27 deletions Dockerfile

This file was deleted.

120 changes: 80 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,68 +5,108 @@
API creation made simple, secure and fast.
The most advanced open-source Content Management Framework to build powerful API with no effort.

***
---

[![Travis](https://img.shields.io/travis/strapi/strapi-docker.svg?style=for-the-badge)](https://travis-ci.org/strapi/strapi-docker)
[![GitHub release](https://img.shields.io/github/release/strapi/strapi-docker.svg?style=for-the-badge)](https://github.com/strapi/strapi-docker/releases)
[![Docker Pulls](https://img.shields.io/docker/pulls/strapi/strapi.svg?style=for-the-badge)](https://hub.docker.com/r/strapi/strapi)

## Quickstart (recommended)
## Images

> [Read the Medium post to have full explaination](https://medium.com/@lucaperret/strapi-quickstart-with-docker-d77ca7c86c1f)
Strapi comes with two images: `strapi/strapi` and `strapi/base`.

1. `git clone https://github.com/strapi/strapi-docker && cd strapi-docker`
2. Run using `docker-compose up`
Use [`strapi/strapi`](#strapi-strapi) to create a new project or run a project on your host machine.

## Pull from Docker Hub
Use [`strapi/base`](#strapi-base) to build a Dockerfile and create an image for your app.

## How to use `strapi/strapi`

This image allows you to create a new strapi project or run a project form your host machine. The default command that will run in your project is [`strapi develop`](https://strapi.io/documentation/3.0.0-beta.x/cli/CLI.html#strapi-develop-dev).

### Creating a new project

When running this image, strapi will check if there is a project in the `/src/app` folder of the container. If there is nothing then it will run the [`strapi new`](https://strapi.io/documentation/3.0.0-beta.x/cli/CLI.html#strapi-new) command in the container /srv/app folder. You can create a new project by running this command.

```bash
docker pull strapi/strapi
docker run -it -p 1337:1337 -v `pwd`/project-name:/srv/app strapi/strapi
```

### Then run image
This command creates a project with an SQLite database. Then starts it on port `1337`.

The `-v` option creates a `project-name` folder on your computer that will be shared with the docker container.
Once the project is created it will be available in this folder on your computer.

**Environment variables**

Start a database (e.g. MongoDB)
When creating a new project with this image you can pass database configurations to the [`strapi new`](https://strapi.io/documentation/3.0.0-beta.x/cli/CLI.html#strapi-new) command.

- `DATABASE_CLIENT` a database provider supported by Strapi: (sqlite, postgres, mysql ,mongo).
- `DATABASE_HOST` database host.
- `DATABASE_PORT` database port.
- `DATABASE_NAME` database name.
- `DATABASE_USERNAME` database username.
- `DATABASE_PASSWORD` database password.
- `DATABASE_SSL` boolean for SSL.
- `EXTRA_ARGS` pass extra args to the [`strapi new`](https://strapi.io/documentation/3.0.0-beta.x/cli/CLI.html#strapi-new).

**Example**

You can create a strapi project that will connect to a remote postgres database like so:

```bash
docker run -e MONGO_INITDB_DATABASE=strapi \
-v `pwd`/db/:/data/db \
--name strapi-mongo \
-d mongo
docker run -it \
-e DATABASE_CLIENT=postgres \
-e DATABASE_NAME=strapi \
-e DATABASE_HOST=0.0.0.0 \
-e DATABASE_PORT=5432 \
-e DATABASE_USERNAME=strapi \
-e DATABASE_PASSWORD=strapi \
-p 1337:1337 \
-v `pwd`/project-name:/srv/app \
strapi/strapi
```

Start strapi
You can also create projects using docker-compose. See examples of using these variables with docker-compose in the [examples folder](./examples).

### Running a project from your host machine

You can also use `strapi/strapi` to run a project you already have created (or cloned for a repo) on your computer.

First make sure to delete the `node_modules` folder if you have already installed your dependencies on your host machine. Then run:

```bash
docker run -e APP_NAME=strapi-app \
-e DATABASE_CLIENT=mongo \
-e DATABASE_HOST=strapi-mongo \
-e DATABASE_PORT=27017 \
-e DATABASE_NAME=strapi \
-v `pwd`/strapi-app:/usr/src/api/strapi-app \
--link strapi-mongo:mongo \
-p 1337:1337 \
--name strapi -d strapi/strapi
cd my-project
docker run -it -p 1337:1337 -v `pwd`:/srv/app strapi/strapi
```

You should the be able to access your Strapi installation at [localhost:1337](http://localhost:1337).
This will start by installing the dependencies and then run `strapi develop` in the project.

**Environment variables**

## Use as base image
If you are using environment variables in your code you can pass them with the -e option (e.g `docker run -e ENV_VAR=sth ...`).

```Dockerfile
FROM strapi/strapi
You can for example set your database configuration with envrionment variables.
Because the default container command is [`strapi develop`](https://strapi.io/documentation/3.0.0-beta.x/cli/CLI.html#strapi-develop-dev) you will need to update your `development` database configuration following the `production` example in the [documentation](https://strapi.io/documentation/3.0.0-beta.x/concepts/configurations.html#dynamic-configurations). Then you can run:

```bash
docker run -it \
-e DATABASE_NAME=strapi \
-e DATABASE_HOST=0.0.0.0 \
-e DATABASE_PORT=1234 \
-e DATABASE_USERNAME=strapi \
-e DATABASE_PASSWORD=strapi \
-p 1337:1337 \
-v `pwd`/project-name:/srv/app \
strapi/strapi
```

## Environment variables
## How to use `strapi/base`

- `APP_NAME` to override the `strapi-app` generated folder name (you should also update the volumes paths).
- `DATABASE_CLIENT` a database providers supported by Strapi: MongoDB, Postgres, MySQL, Sqlite3 and Redis.
- `DATABASE_HOST` database service name.
- `DATABASE_PORT` depends on your database client.
- `DATABASE_NAME` initializes a database with specific name (default strapi). When using MongoDB, you should also update the `MONGO_INITDB_DATABASE` environment in the db service.
- `DATABASE_USERNAME` set the username of the database connection.
- `DATABASE_PASSWORD` set the password of the database connection.
- `DATABASE_SRV` boolean for SRV.
- `DATABASE_SSL` boolean for SSL.
- `DATABASE_AUTHENTICATION_DATABASE` set the authentification.
- `EXTRA_ARGS` add any Extra supported arguments in the form of a string.
When deploying a strapi application to production you can use docker to package your whole app in an image. You can create a Dockerfile in your strapi project like the one in [`./examples/custom`](./examples/custom)

## Building the images in this repository

You can build the images with the build command. To see the options run:

```
./bin/build.js --help
```
4 changes: 4 additions & 0 deletions base/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ARG NODE_VERSION
FROM node:${NODE_VERSION}

EXPOSE 1337
6 changes: 6 additions & 0 deletions base/alpine/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ARG NODE_VERSION
FROM node:${NODE_VERSION}

RUN apk update && apk upgrade && apk add build-base gcc autoconf automake zlib-dev libpng-dev nasm

EXPOSE 1337
66 changes: 66 additions & 0 deletions bin/base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
'use strict';

const { execDocker } = require('./utils');
const {
NODE_VERSIONS,
BASE_IMAGE_NAME,
LATEST_NODE_VERSION,
} = require('./contstants');

module.exports = {
buildBaseImages,
};

async function buildBaseImages({ shouldPush = false } = {}) {
const createdTags = [];
for (const nodeVersion of NODE_VERSIONS) {
const tags = await buildBaseImage({ nodeVersion, shouldPush });
const alpineTags = await buildBaseImage({
nodeVersion,
alpine: true,
shouldPush,
});

createdTags.push(...tags, ...alpineTags);
}

return createdTags.map(tag => `${BASE_IMAGE_NAME}:${tag}`);
}

async function buildBaseImage({ nodeVersion, alpine, shouldPush = false }) {
let tmpImg = `${BASE_IMAGE_NAME}:tmp`;

await execDocker([
'build',
'--build-arg',
`NODE_VERSION=${nodeVersion}${alpine ? '-alpine' : ''}`,
'-t',
tmpImg,
`./base${alpine ? '/alpine' : ''}`,
]);

const tags = buildBaseTags({ nodeVersion, alpine });

for (const tag of tags) {
await execDocker(['tag', tmpImg, `${BASE_IMAGE_NAME}:${tag}`]);

if (shouldPush) {
await execDocker(['push', `${BASE_IMAGE_NAME}:${tag}`]);
}
}

await execDocker(['image', 'rm', tmpImg]);
return tags;
}

function buildBaseTags({ nodeVersion, alpine = false }) {
let tags = [];

tags.push(`${nodeVersion}${alpine ? '-alpine' : ''}`);

if (nodeVersion === LATEST_NODE_VERSION && !alpine) {
tags.push('latest');
}

return tags;
}
74 changes: 74 additions & 0 deletions bin/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/usr/bin/env node
'use strict';

const yargs = require('yargs');

const { buildBaseImages } = require('./base');
const { buildStrapiImages } = require('./strapi');

async function run() {
const shouldPush = argv.push;
const version = argv.strapiVersion;

switch (argv.type) {
case 'base': {
const images = await buildBaseImages({ shouldPush });
logImages(images);

break;
}
case 'strapi': {
const images = await buildStrapiImages({ version, shouldPush });
logImages(images);

break;
}
case 'all':
default: {
const baseImages = await buildBaseImages({ shouldPush });
const strapiImages = await buildStrapiImages({ version, shouldPush });
logImages([...baseImages, ...strapiImages]);

break;
}
}
}

const argv = yargs
.option('type', {
alias: 't',
describe: 'Which images to build (all,strapi,base)',
default: 'all',
type: 'string',
})
.option('push', {
alias: 'p',
describe: 'Should push the image after creating it',
default: process.env.PUSH || false,
type: 'boolean',
})
.option('strapiVersion', {
describe: 'strapi version to build',
default: process.env.STRAPI_VERSION || 'latest',
type: 'string',
})
.version(false)
.help('h')
.alias('h', 'help').argv;

if (argv.help) {
yargs.showHelp();
return;
}

run().catch(error => {
console.error(error);
process.exit(1);
});

function logImages(imgs) {
console.log('---------------------------------------');
console.log('Images created:');
console.log(imgs.map(img => `- ${img}`).join('\n'));
console.log('---------------------------------------');
}
Loading

0 comments on commit e86c76c

Please sign in to comment.