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
30 changes: 29 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,34 @@ 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.

### Usage with a specified image
A specific image to be used 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({
image: 'bertuz/docker-chromium:chromium103.0.5060.53',
// 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_IMAGE` environment variable, 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"]
8 changes: 8 additions & 0 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
version: "3"

services:
chromium:
build:
Expand All @@ -7,3 +8,10 @@ services:
shm_size: "1gb"
ports:
- "9222:9222"
chromium-image:
image: ${DOCKER_CHROMIUM_IMAGE}
environment:
- CHROMIUM_ADDITIONAL_ARGS
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.
48 changes: 33 additions & 15 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const {
const dockerComposePath = path.join(__dirname, '../docker/docker-compose.yml');

let serviceToBuild = 'chromium';
let serviceToRun = serviceToBuild;
let image;
let chromiumAdditionalArgs;
let chromiumDownloadHost;
let chromiumRevision;
Expand Down Expand Up @@ -46,13 +48,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_IMAGE=${image} CHROMIUM_ADDITIONAL_ARGS='${chromiumAdditionalArgs}' docker-compose`,
['-f', `"${dockerComposePath}"`, 'up', '-d', serviceToRun]
);
console.log(
`${CONSOLE_PREFIX} Successfully started Docker container`.green
);
Expand Down Expand Up @@ -115,10 +114,37 @@ const contactChromium = async ({ config, maxAttempts, retryInterval }) => {

const dockerConfig = ({
flags,
useImage,
revision,
downloadHost,
useClosestUbuntuMirror
}) => {
image =
useImage ||
process.env.DOCKER_CHROMIUM_USE_IMAGE ||
process.env.npm_config_docker_chromium_use_image ||
process.env.npm_package_config_docker_chromium_use_image;
console.log(`${CONSOLE_PREFIX} Use Docker image ${useImage}`.green);

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

if (image) {
serviceToRun = 'chromium-image';
serviceToBuild = serviceToRun;

console.log(
`Ignoring the rest of the parameters because a specific image is used.`
.green
);
return;
}

shouldUseClosestUbuntuMirror =
useClosestUbuntuMirror ||
process.env.USE_CLOSEST_UBUNTU_MIRROR ||
Expand All @@ -142,14 +168,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
12 changes: 12 additions & 0 deletions tests/run-container/run-container.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,16 @@ describe('runContainer', () => {

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

it('runs container with a specific image and provides websocket uri', async () => {
dockerSetChromiumConfig({
useImage: 'bertuz/docker-chromium:chromium103.0.5060.53',
flags: [' -–ignore-certificate-errors'],
revision: 754306
});

const webSocketUri = await dockerRunChromium();

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