Skip to content
Open
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
33 changes: 32 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const {

(async () => {
await dockerSetChromiumConfig({
revision: "123456"
revision: "123456",
flags: [' -–ignore-certificate-errors']
});
const webSocketUri = await dockerRunChromium();
Expand All @@ -47,6 +47,37 @@ const webSocketUri = await dockerRunChromium({

Or by defining environment variables `DOCKER_CHROMIUM_MAX_ATTEMPTS` and `DOCKER_CHROMIUM_RETRY_INTERVAL`. Passing arguments to `dockerRunChromium` takes precedence over environment variables.

### Use with a specified Dockerfile
A specific dockerile to build a different container can be set via the configuration or the environment variable. This is especially useful if you use an arm processor (Apple silicon) and the image you'll use
is platform compatible (more on this on [this repo](https://github.com/bertuz/docker-chromium-img))


```javascript
const {
dockerSetChromiumConfig,
dockerRunChromium,
dockerShutdownChromium
} = require("docker-chromium");

(async () => {
await dockerSetChromiumConfig({
useDockerBuild: {
dockerFile: 'Dockerfile',
contextPath: path.join(__dirname, '/dockerFiles')
},
// this property will be used when running the image
flags: [' -–ignore-certificate-errors'],
// this property will be ignored: the image is built already
revision: "123456",
});
const webSocketUri = await dockerRunChromium();
// do some other stuff...
await dockerShutdownChromium();
})();
```

Alternatively, you can set the `DOCKER_CHROMIUM_USE_DOCKERFILE` and `DOCKER_CHROMIUM_USE_CONTEXT_PATH` environment variables, but the config's property will take precedence on it, if passed.

## How it works

`docker-chromium` pulls a pre-built Docker image running a version of Chromium specified by you from a Docker Hub repository. You can then fetch the WebSocket URI to connect to the instance in your own application. If the pre-built image is unavailable or corrupt (rare case), a backup mechanism is in place, which builds the image from scratch locally instead.
Expand Down
2 changes: 1 addition & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ VOLUME /data
ENV HOME=/data DEBUG_ADDRESS=0.0.0.0 DEBUG_PORT=9222

ENTRYPOINT ["/usr/bin/dumb-init", "--"]
CMD ["/usr/bin/entrypoint.sh"]
CMD ["/usr/bin/entrypoint.sh"]
5 changes: 3 additions & 2 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
version: "3"

services:
chromium:
build:
context: ./
dockerfile: Dockerfile
context: ${DOCKER_CHROMIUM_BUILD_CONTEXT}
dockerfile: ${DOCKER_CHROMIUM_BUILD_DOCKERFILE}
shm_size: "1gb"
ports:
- "9222:9222"
Empty file modified docker/entrypoint.sh
100644 → 100755
Empty file.
Empty file modified docker/import_cert.sh
100644 → 100755
Empty file.
88 changes: 58 additions & 30 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ const {
} = require('./defaults');

const dockerComposePath = path.join(__dirname, '../docker/docker-compose.yml');

let serviceToBuild = 'chromium';
let dockerBuildParams = {
dockerFile: 'Dockerfile',
contextPath: path.join(__dirname, '../docker')
};
let chromiumAdditionalArgs;
let chromiumDownloadHost;
let chromiumRevision;
Expand All @@ -22,19 +24,22 @@ let shouldUseClosestUbuntuMirror;
const dockerBuild = async () => {
try {
console.log(`${CONSOLE_PREFIX} Building Docker image...`.green);
await runCommand('docker-compose', [
'-f',
`"${dockerComposePath}"`,
'build',
`--build-arg CHROMIUM_ADDITIONAL_ARGS="${chromiumAdditionalArgs}"`,
`--build-arg CHROMIUM_DOWNLOAD_HOST=${chromiumDownloadHost}`,
`--build-arg CHROMIUM_REVISION=${chromiumRevision ||
DEFAULT_CHROMIUM_REVISION}`,
`--build-arg USE_CLOSEST_UBUNTU_MIRROR=${shouldUseClosestUbuntuMirror ||
DEFAULT_USE_CLOSEST_UBUNTU_MIRROR}`,
'--pull',
serviceToBuild
]);
await runCommand(
`DOCKER_CHROMIUM_BUILD_DOCKERFILE=${dockerBuildParams.dockerFile} DOCKER_CHROMIUM_BUILD_CONTEXT=${dockerBuildParams.contextPath} docker-compose`,
[
'-f',
`"${dockerComposePath}"`,
'build',
`--build-arg CHROMIUM_ADDITIONAL_ARGS="${chromiumAdditionalArgs}"`,
`--build-arg CHROMIUM_DOWNLOAD_HOST=${chromiumDownloadHost}`,
`--build-arg CHROMIUM_REVISION=${chromiumRevision ||
DEFAULT_CHROMIUM_REVISION}`,
`--build-arg USE_CLOSEST_UBUNTU_MIRROR=${shouldUseClosestUbuntuMirror ||
DEFAULT_USE_CLOSEST_UBUNTU_MIRROR}`,
'--pull',
'chromium'
]
);
console.log(`${CONSOLE_PREFIX} Successfully built Docker image`.green);
} catch (error) {
throw new Error(
Expand All @@ -46,13 +51,10 @@ const dockerBuild = async () => {
const dockerUp = async () => {
try {
console.log(`${CONSOLE_PREFIX} Starting Docker container...`.green);
await runCommand('docker-compose', [
'-f',
`"${dockerComposePath}"`,
'up',
'-d',
serviceToBuild
]);
await runCommand(
`DOCKER_CHROMIUM_BUILD_DOCKERFILE=${dockerBuildParams.dockerFile} DOCKER_CHROMIUM_BUILD_CONTEXT=${dockerBuildParams.contextPath} CHROMIUM_ADDITIONAL_ARGS='${chromiumAdditionalArgs}' docker-compose`,
['-f', `"${dockerComposePath}"`, 'up', '-d', 'chromium']
);
console.log(
`${CONSOLE_PREFIX} Successfully started Docker container`.green
);
Expand Down Expand Up @@ -115,10 +117,44 @@ const contactChromium = async ({ config, maxAttempts, retryInterval }) => {

const dockerConfig = ({
flags,
useDockerBuild,
revision,
downloadHost,
useClosestUbuntuMirror
}) => {
const dockerBuildParamsFile =
(useDockerBuild && useDockerBuild.dockerFile) ||
process.env.DOCKER_CHROMIUM_USE_DOCKERFILE ||
process.env.npm_config_docker_chromium_use_dockerfile ||
process.env.npm_package_config_docker_chromium_use_dockerfile ||
dockerBuildParams.dockerFile;

const dockerBuildParamsContext =
(useDockerBuild && useDockerBuild.contextPath) ||
process.env.DOCKER_CHROMIUM_USE_CONTEXT_PATH ||
process.env.npm_config_docker_chromium_use_contextPath ||
process.env.npm_package_config_docker_chromium_use_contextPath ||
dockerBuildParams.contextPath;

dockerBuildParams = {
dockerFile: dockerBuildParamsFile,
contextPath: dockerBuildParamsContext
};

console.log(
`${CONSOLE_PREFIX} Use Docker build ${dockerBuildParams.dockerFile}, with context path ${dockerBuildParams.contextPath}`
.green
);

chromiumAdditionalArgs =
flags || process.env.CHROMIUM_ADDITIONAL_ARGS || '';
if (chromiumAdditionalArgs) {
console.log(
`${CONSOLE_PREFIX} Setting Chromium flags to ${chromiumAdditionalArgs}...`
.green
);
}

shouldUseClosestUbuntuMirror =
useClosestUbuntuMirror ||
process.env.USE_CLOSEST_UBUNTU_MIRROR ||
Expand All @@ -142,14 +178,6 @@ const dockerConfig = ({
.green
);

chromiumAdditionalArgs = flags || process.env.CHROMIUM_ADDITIONAL_ARGS;
if (chromiumAdditionalArgs) {
console.log(
`${CONSOLE_PREFIX} Setting Chromium flags to ${chromiumAdditionalArgs}...`
.green
);
}

if (revision) {
console.log(
`${CONSOLE_PREFIX} Setting Chromium version to rev-${revision}...`
Expand Down
3 changes: 3 additions & 0 deletions tests/run-container/dockerFiles/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM bertuz/docker-chromium:chromium103.0.5060.53

CMD ["/usr/bin/entrypoint.sh"]
16 changes: 16 additions & 0 deletions tests/run-container/run-container.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const {
dockerRunChromium,
dockerShutdownChromium
} = require('../../lib/index');
const path = require('path');

describe('runContainer', () => {
afterAll(async () => {
Expand All @@ -19,4 +20,19 @@ describe('runContainer', () => {

expect(webSocketUri).toContain('ws://');
}, 300000);

it('runs container with a specific dockerfile and provides websocket uri', async () => {
dockerSetChromiumConfig({
useDockerBuild: {
dockerFile: 'Dockerfile',
contextPath: path.join(__dirname, '/dockerFiles')
},
flags: [' -–ignore-certificate-errors'],
revision: 754306
});

const webSocketUri = await dockerRunChromium();

expect(webSocketUri).toContain('ws://');
}, 300000);
});