embark-docker/README.md

377 lines
8.4 KiB
Markdown
Raw Normal View History

2018-07-11 13:30:32 +00:00
# Embark for Docker
2018-07-24 23:56:07 +00:00
## Quick start
In a Bash shell:
``` shell
2018-07-30 22:50:30 +00:00
source <(curl https://bit.ly/run_embark)
2018-07-25 03:27:03 +00:00
run_embark demo
2018-07-24 23:56:07 +00:00
cd embark_demo
2018-07-25 03:27:03 +00:00
run_embark
2018-07-24 23:56:07 +00:00
```
2018-08-02 17:51:08 +00:00
Note that the `run_embark demo` command will create an `embark_demo` directory in
the docker host's `$PWD`.
2018-08-02 02:34:27 +00:00
## Usage via `run.sh`
[`run.sh`](https://github.com/embark-framework/embark-docker/blob/master/run.sh)
is a Bash script that simplifies usage of the embark container: publishing
ports, bind mounting a host volume, and so on.
Many aspects of the script's behavior can be overridden with environment
variables, and that approach can be (optionally) combined with `docker build`:
2018-07-24 23:56:07 +00:00
``` shell
export EMBARK_DOCKER_IMAGE=statusim/embark
export EMBARK_DOCKER_TAG=custom
2018-08-02 02:36:40 +00:00
export EMBARK_DOCKERFILE='https://github.com/embark-framework/embark-docker.git#master'
export EMBARK_VERSION='embark-framework/embark#develop'
2018-08-02 17:51:08 +00:00
export NODE_VERSION=10.7.0
2018-08-02 02:34:27 +00:00
export RUNNER='https://raw.githubusercontent.com/embark-framework/embark-docker/master/run.sh'
2018-07-24 23:56:07 +00:00
docker build \
2018-07-25 03:02:27 +00:00
--build-arg EMBARK_VERSION=$EMBARK_VERSION \
--build-arg NODE_TAG=$NODE_TAG \
-t $EMBARK_DOCKER_IMAGE:$EMBARK_DOCKER_TAG \
$EMBARK_DOCKERFILE
2018-07-24 23:56:07 +00:00
source <(curl $RUNNER)
2018-08-02 17:51:08 +00:00
run_embark --rm -- demo
2018-07-24 23:56:07 +00:00
cd embark_demo
2018-08-02 17:51:08 +00:00
run_embark --rm --
2018-07-24 23:56:07 +00:00
```
2018-07-25 20:52:49 +00:00
Review the
[Dockerfile](https://github.com/embark-framework/embark-docker/blob/master/Dockerfile)
and
2018-08-02 02:34:27 +00:00
[run.sh](https://github.com/embark-framework/embark-docker/blob/master/run.sh)
2018-07-25 20:52:49 +00:00
for all possible overrides.
2018-07-24 23:56:07 +00:00
2018-08-02 17:51:08 +00:00
It's possible to pass additional options to `docker run` by specifying them
before `--`:
``` shell
run_embark [docker-run-opts] -- [command]
```
To completely replace the default `docker run` options:
``` shell
EMBARK_DOCKER_RUN_OPTS_REPLACE=true
run_embark [docker-run-opts] -- [command]
```
2018-07-25 03:27:03 +00:00
### Shortcuts
2018-07-25 20:17:31 +00:00
These are equivlent:
``` shell
run_embark
```
``` shell
run_embark run
```
``` shell
run_embark embark run
```
The following are also equivalent:
2018-07-25 03:27:03 +00:00
``` shell
run_embark demo
```
``` shell
run_embark embark demo
```
2018-07-25 20:17:31 +00:00
The same is true for the rest of the `embark` commands. To see the full list:
2018-07-25 03:27:03 +00:00
``` shell
2018-07-25 20:17:31 +00:00
run_embark --help
2018-07-25 03:27:03 +00:00
```
2018-07-25 20:17:39 +00:00
2018-08-02 02:34:27 +00:00
### Utilities
The container comes equipped with
[nodeenv](https://github.com/ekalinin/nodeenv) and
[nvm](https://github.com/creationix/nvm). A `default` Node.js environment is
installed via `nodeenv` during image build and placed in
2018-08-02 17:51:08 +00:00
`~embark/.local/nodeenv/default`. The `default` environment is automatically
activated by the container's entrypoint.
Both `nodeenv` and `nvm` can be used in
2018-08-02 02:34:27 +00:00
interactive and non-interactive scripts.
#### `nodeenv`
These are equivalent:
``` shell
nodeenv --prebuilt --node 10.7.0 ~/.local/nodeenv/my_node
```
``` shell
simple_nodeenv 10.7.0 my_node
```
Activate and deactivate environments with `nac` and `denac`:
``` shell
nac my_node
```
``` shell
denac
```
Note that `simple_nodeenv` automatically activates an environment after
installation, while `nodeenv` does not.
#### `nvm`
If `nvm` is preferable, it needs to be loaded first:
``` shell
nvm_load
nvm install --latest-npm 8.11.3
```
`nvm deactivate` and `nvm unload` will work as expected. It's also possible to
move between `nodeenv` and `nvm` environments without first deactivating or
unloading:
``` shell
nac default
nvm_load && nvm use v10.7.0
# ^ assuming 10.7.0 is already installed
nac default
```
2018-08-02 17:54:46 +00:00
#### `micro`
The [micro](https://github.com/zyedidia/micro) editor is installed during image
build, should you need to edit files within a running container.
2018-08-02 02:34:27 +00:00
#### `install-extras.sh`
Some nice-to-have utilities are not installed by default, but this can be done
by running
[`install-extras.sh`](https://github.com/embark-framework/embark-docker/blob/master/env/install-extras.sh)
as the `root` user in an already running container:
``` shell
docker exec -it $container_id install-extras.sh
```
### Commands
#### Simple
2018-07-25 20:17:39 +00:00
A single command with options can be supplied directly:
2018-07-25 03:27:03 +00:00
``` shell
2018-07-25 20:17:39 +00:00
run_embark bash
```
``` shell
2018-08-02 02:34:27 +00:00
run_embark node -i -e 'console.log(process.version)'
# ^ press return again to get a blank REPL prompt
```
``` shell
2018-07-25 20:17:39 +00:00
run_embark ps -ef
```
2018-08-02 02:34:27 +00:00
#### Compound
2018-07-25 20:17:39 +00:00
Compound commands should be passed to `bash -[i]c`:
2018-08-02 02:34:27 +00:00
``` shell
run_embark bash -c 'ps -ef && ls / ; which embark'
```
``` shell
run_embark bash -c 'nvm_load && nvm install --latest-npm 8.11.3 && node --version && npm --version'
```
Bash
[here-documents](https://www.gnu.org/software/bash/manual/html_node/Redirections.html#Here-Documents)
can be used to compose scripts without employing an abundance of `&&`, `;`, and
`\`. Just be mindful of umatched quotes and quotes-escaping when building
meta-scripts; in such scenarios, use of
[envsubst](https://www.gnu.org/software/gettext/manual/html_node/envsubst-Invocation.html)
is probably called for.
2018-08-02 02:34:27 +00:00
2018-07-25 20:17:39 +00:00
``` shell
run_embark bash -c 'exec bash << "SCRIPT"
simple_nodeenv 10.7.0 my_node
npm i -g http-server
2018-08-02 02:34:27 +00:00
exec http-server -p 8000
2018-07-25 20:17:39 +00:00
SCRIPT
'
2018-07-25 03:27:03 +00:00
```
2018-08-02 02:34:27 +00:00
Since `run.sh` mounts the docker host's `$PWD` into the container's `/dapp`, and
since `/dapp` is the container's default working directory, it's also possible
to do:
``` shell
run_embark ./my_script.sh
# ^ assuming my_script.sh is in the docker host's $PWD
```
Just make sure the script has a `#!` line and that you did `chmod +x
my_script.sh` on the docker host before invoking `run_embark`.
##### `$EMBARK_DOCKER_RUN`
For greater flexibility, you can specify a script with
`$EMBARK_DOCKER_RUN`. Arguments passed to `run_embark` will be forwarded to the
script, and extra flags can be provided to `docker run` to forward docker host
environment variables.
2018-08-02 17:51:08 +00:00
Keep in mind that such scripts will run as the `embark` user owing to the
2018-08-02 02:34:27 +00:00
container's entrypoint.
``` shell
#!/bin/bash
# this script is located at /path/to/my_script.sh on the docker host, not necessarily in host's $PWD
# dangling "
c=container!
echo $HOST_HOSTNAME
echo $HOSTNAME
echo $@
echo $1
# a comment
echo $2
echo $3
eval echo \\\$\$3
# another comment
```
Invoke with:
``` shell
EMBARK_DOCKER_RUN=/path/to/my_script.sh
a=host!
2018-08-02 17:51:08 +00:00
run_embark -e HOST_HOSTNAME=$HOSTNAME -- $a b c
2018-08-02 02:34:27 +00:00
```
Node.js variant:
``` javascript
#!/usr/bin/env node
// this script is located at /path/to/my_node_script.js on the docker host, not necessarily in host's $PWD
const o = {c: 'container!'};
console.log(process.env.HOST_HOSTNAME);
console.log(process.env.HOSTNAME);
console.log(JSON.stringify(process.argv));
console.log(process.argv[2]);
console.log(process.argv[3]);
console.log(process.argv[4]);
console.log(o[process.argv[4]]);
```
Invoke the same way:
``` shell
EMBARK_DOCKER_RUN=/path/to/my_node_script.js
a=host!
2018-08-02 17:51:08 +00:00
run_embark -e HOST_HOSTNAME=$HOSTNAME -- $a b c
2018-08-02 02:34:27 +00:00
```
#### `docker exec`
2018-07-25 20:17:39 +00:00
When executing compound commands via `docer exec` in a running embark
2018-08-02 02:34:27 +00:00
container, `su-exec` and `bash -[i]c` can be used together:
2018-07-25 03:27:03 +00:00
``` shell
2018-08-02 02:34:27 +00:00
docker exec -it $container_id su-exec embark \
2018-07-25 20:17:39 +00:00
bash -ic 'exec bash << "SCRIPT"
2018-08-02 02:43:21 +00:00
simple_nodeenv 10.7.0 my_node || nac my_node
2018-08-02 02:34:27 +00:00
npm i -g http-server
exec http-server -p 8000
2018-07-25 20:17:39 +00:00
SCRIPT
'
2018-07-25 03:27:03 +00:00
```
2018-07-25 20:17:39 +00:00
2018-08-02 02:34:27 +00:00
To go non-interactive, manually source the embark user's `.bash_env`:
2018-07-25 20:17:39 +00:00
2018-07-25 03:27:03 +00:00
``` shell
2018-08-02 02:34:27 +00:00
docker exec -it $container_id su-exec embark \
2018-07-25 20:17:39 +00:00
bash -c 'exec bash << "SCRIPT"
. ~/.bash_env
2018-08-02 02:43:21 +00:00
simple_nodeenv 10.7.0 my_node || nac my_node
2018-07-25 20:17:39 +00:00
npm i -g http-server
2018-08-02 02:34:27 +00:00
exec http-server -p 8000
2018-07-25 20:17:39 +00:00
SCRIPT
'
2018-07-25 03:27:03 +00:00
```
2018-08-02 02:34:27 +00:00
## Container development
### Updating versions
2018-07-11 13:30:32 +00:00
* Open `Dockerfile`
2018-07-24 23:55:54 +00:00
* On the `ARG` directives, update necessary versions.
2018-07-11 13:30:32 +00:00
2018-08-02 02:34:27 +00:00
### Building
2018-07-11 13:30:32 +00:00
2018-07-24 23:55:40 +00:00
Building requires Docker to be installed on your local machine.
2018-07-11 18:53:08 +00:00
2018-08-02 02:34:27 +00:00
#### Scripted
2018-07-11 18:53:08 +00:00
If you have Ruby installed in your system, run:
2018-07-11 13:30:32 +00:00
```
2018-07-11 18:53:08 +00:00
$ ruby script/build
```
To release, add `--release` as a parameter of the build script.
2018-08-02 02:34:27 +00:00
#### Manually
2018-07-11 18:53:08 +00:00
2018-07-25 20:52:49 +00:00
Building and releasing manually isn't too hard either, but there are a couple
steps.
2018-07-11 18:53:08 +00:00
2018-08-02 02:34:27 +00:00
##### Tags
2018-07-11 18:53:08 +00:00
2018-07-25 20:52:49 +00:00
To facilitate the images being found, we tag them with the following rules (as
an example, the `3.1.5` version will be used.)
2018-07-11 18:53:08 +00:00
- Tag with `statusim/embark:latest` if `3.1.5` is the latest version.
- Tag with `statusim/embark:3.1.5`
- Tag with `statusim/embark:3.1` if `3.1.5` is the highest patch level on `3.1`
2018-07-25 20:52:49 +00:00
- Tag with `statusim/embark:3` if `3.1.5` is the highest minor and patch level
on `3`
2018-07-11 18:53:08 +00:00
2018-08-02 02:34:27 +00:00
##### Generating the image
2018-07-11 18:53:08 +00:00
To generate the image, run:
```
docker build . -t statusim/embark:<version> [...tags]
2018-07-11 13:30:32 +00:00
```
2018-08-02 02:34:27 +00:00
### Releasing
2018-07-11 13:30:32 +00:00
Releasing requires that you're authenticated to Docker Hub. To do so, run:
```
$ docker login
```
2018-08-02 02:34:27 +00:00
#### Scripted
2018-07-11 18:53:08 +00:00
If you have Ruby installed in your system, run:
```
$ ruby script/build --release
```
2018-08-02 02:34:27 +00:00
#### Manual
2018-07-11 18:53:08 +00:00
2018-07-25 20:52:49 +00:00
Pushing the tags manually implies that the image has been previously built. To
push your local images, run:
2018-07-11 13:30:32 +00:00
```
2018-07-11 18:53:08 +00:00
docker push statusim/embark:version
2018-07-11 13:30:32 +00:00
```