Allow the project to be developed inside a docker container

This commit is contained in:
Timothy Farrell 2018-08-24 14:25:00 -05:00
parent bd2b1d99b9
commit b6536700af
7 changed files with 81 additions and 14 deletions

11
bin/drun.sh Normal file
View File

@ -0,0 +1,11 @@
#!/bin/sh
DOCKER_CONTAINERS=`docker ps -a | grep portfolio_dev | wc -l`
if [ "$DOCKER_CONTAINERS" -ge "1" ]
then
echo "Running in docker... $@"
docker-compose run --rm dev $@
else
eval "$@"
fi

View File

@ -2,8 +2,8 @@
docker-compose up --detach test-web docker-compose up --detach test-web
nodejs node_modules/lerna/bin/lerna.js run pre-commit \ sh ./bin/drun.sh lerna run pre-commit \
&& npm run check_code_format && npm run check_code_format
EXIT=$? EXIT=$?
docker-compose stop test-web docker-compose stop test-web

View File

@ -32,7 +32,7 @@ const testPath = path.join(packageRoot, TEST_FILENAME);
const webRoot = path.relative(PORTFOLIO_DIR, testPath); const webRoot = path.relative(PORTFOLIO_DIR, testPath);
const items = fs.readdirSync(packageRoot); const items = fs.readdirSync(packageRoot);
let chrome, protocol; let chrome, protocol, chromeIsLocal;
writeSpecRunner(); writeSpecRunner();
runTestsInChrome(); runTestsInChrome();
@ -85,12 +85,24 @@ function writeSpecRunner() {
} }
async function runTestsInChrome() { async function runTestsInChrome() {
chrome = await launchChrome(); console.log(`Starting Chrome`);
protocol = await CDP({ try {
port: chrome.port chrome = await launchChrome();
}); protocol = await CDP({ port });
chromeIsLocal = true;
} catch(e) {
console.log(`Failed to start Chrome, accessing local on port 9222`);
// We were not able to launch Chrome. Assume we're in a docker container.
try {
protocol = await CDP({ host: 'host.docker.internal', port: 9222 });
chromeIsLocal = false;
} catch (f) {
console.log(`Error: ${f}`);
return;
}
}
const { DOM, Network, Page, Emulation, Runtime, Console, Debugger } = protocol; const { DOM, Network, Page, Runtime, Console, Debugger } = protocol;
await Promise.all([ await Promise.all([
Network.enable(), Network.enable(),
@ -100,6 +112,7 @@ async function runTestsInChrome() {
Console.enable(), Console.enable(),
DEBUG ? Debugger.enable() : Promise.resolve() DEBUG ? Debugger.enable() : Promise.resolve()
]); ]);
console.log(`Running tests at http://${HOST}:${PORT}/${webRoot}`);
await Page.navigate({ url: `http://${HOST}:${PORT}/${webRoot}` }); await Page.navigate({ url: `http://${HOST}:${PORT}/${webRoot}` });
let indentation = 0; let indentation = 0;
@ -195,8 +208,10 @@ function exit(code) {
if (DEBUG) { if (DEBUG) {
return; return;
} }
fs.unlink(testPath); fs.unlinkSync(testPath);
protocol.close(); protocol.close();
chrome.kill(); if (chromeIsLocal) {
chrome.kill();
}
process.exit(code); process.exit(code);
} }

View File

@ -7,6 +7,14 @@ services:
- 8090:80 - 8090:80
volumes: volumes:
- ./:/usr/share/nginx/html:ro - ./:/usr/share/nginx/html:ro
dev:
build:
context: ./docker
dockerfile: dev.dockerfile
volumes:
- ./:/workspace/
working_dir: /workspace
entrypoint: /docker-entrypoint.sh
cloud9: cloud9:
build: build:
context: ./docker context: ./docker

18
docker/dev.dockerfile Normal file
View File

@ -0,0 +1,18 @@
FROM node:slim
COPY docker-entrypoint.sh /
RUN chmod 755 /docker-entrypoint.sh
RUN buildDeps='g++ make python' && softDeps="git iproute2 iputils-ping" \
&& apt update && apt upgrade -y \
&& apt install -y $buildDeps $softDeps --no-install-recommends \
&& npm install -g lerna \
&& apt-get purge -y --auto-remove $buildDeps \
&& apt-get autoremove -y && apt-get autoclean -y && apt-get clean -y \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \
&& npm cache clean --force
VOLUME /workspace
ENTRYPOINT /docker-entrypoint.sh
CMD /bin/bash

14
docker/docker-entrypoint.sh Executable file
View File

@ -0,0 +1,14 @@
#!/bin/sh
# This entrypoint script is designed to populate "host.docker.internal" in /etc/hosts on startup until https://github.com/docker/for-linux/issues/264 is fixed and released.
HOST_DOMAIN="host.docker.internal"
grep $HOST_DOMAIN /etc/hosts && ping -q -c1 $HOST_DOMAIN
if [ $? -ne 0 ]; then
HOST_IP=$(ip route | awk 'NR==1 {print $3}')
echo "$HOST_IP\t$HOST_DOMAIN" >> /etc/hosts
echo "Added $HOST_IP $HOST_DOMAIN to /etc/hosts"
fi
echo Running command "$@"
eval "$@"

View File

@ -1,15 +1,16 @@
{ {
"devDependencies": { "devDependencies": {
"chrome-launcher": "^0.10.2", "chrome-launcher": "^0.10.2",
"chrome-remote-interface": "^0.25.5", "chrome-remote-interface": "0.26.1",
"husky": "1.0.0-rc.13", "husky": "1.0.0-rc.13",
"lerna": "3.1.4", "lerna": "3.1.4",
"prettier": "1.14.2" "prettier": "1.14.2"
}, },
"scripts": { "scripts": {
"check_code_format": "nodejs node_modules/prettier/bin-prettier.js --config ./prettier.config.js --list-different \"{.,{packages,bin}/**/!(dist)}/*.{js,json,md}\"", "check_code_format": "sh ./bin/drun.sh \"nodejs node_modules/prettier/bin-prettier.js --config ./prettier.config.js --list-different \\\"{.,{packages,bin}/**/!(dist)}/*.{js,json,md}\\\"\"",
"format_code": "nodejs node_modules/prettier/bin-prettier.js --config ./prettier.config.js --write \"{.,{packages,bin}/**/!(dist)}/*.{js,json,md}\"", "format_code": "sh ./bin/drun.sh \"nodejs node_modules/prettier/bin-prettier.js --config ./prettier.config.js --write \\\"{.,{packages,bin}/**/!(dist)}/*.{js,json,md}\\\"\"",
"test": "sh ./bin/pre-commit.sh" "test": "sh ./bin/pre-commit.sh",
"bootstrap": "sh ./bin/drun.sh lerna bootstrap"
}, },
"husky": { "husky": {
"hooks": { "hooks": {