Skip to content

Commit b29b689

Browse files
author
Mike Kozicki
authored
Merge pull request #636 from FedericoMulas/docker-debug
Layer improvement for container and develop container
2 parents d3b262c + 95a6108 commit b29b689

File tree

6 files changed

+130
-63
lines changed

6 files changed

+130
-63
lines changed

.dockerignore

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,19 @@
1+
# hidden unused files
12
.git
3+
.vscode
4+
.gitignore
5+
.travis.yml
6+
7+
# unused folders
8+
docs
9+
test
10+
11+
# unused script and files
12+
README.md
13+
alive.bat
14+
browsers.ini.sample
15+
centos_install.sh
16+
ios_install.sh
17+
ubuntu_install.sh
18+
windows_install.ps1
19+
windows_post_reboot.ps1

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Ensure that linux files have lf endings even when working on windows
2+
*.sh text eol=lf

Dockerfile

Lines changed: 34 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,74 +7,65 @@
77
### docker run -it -d TAGNAME /bin/bash - Runs bash
88
### docker exec -it <IMAGE ID> /bin/bash - Connects to bash for terminal execution (Needs to be running first)
99

10-
### EXAMPLE DOCKER COMMANDS FOR RUNNING SERVER & AGENT
11-
12-
### docker run -d -p 4000:80 <IMAGE ID || <IMAGE TAG>
13-
### docker run -d -p 4001:80 --network="host" -e "SERVER_URL=http://localhost:4000/work/" -e "LOCATION=Test" -e "-v" <IMAGE ID || <IMAGE TAG>
14-
1510
### INSTALLING METHOD ###
1611

1712
### Recommend to install with "docker build <GITHUB-REPO-LINK> -t TAGNAME",
1813
### grabs the latest copy of WPT and build time on average takes 10 minutes.
1914

20-
FROM ubuntu:22.04
21-
22-
### PREVENTs INTERACTIVE PROMPTS WHILE INSTALLING ###
23-
ARG DEBIAN_FRONTEND=noninteractive
24-
25-
### COPYING ENTIRE DIR TO LOCAL DOCKER /wptagent
26-
COPY / /wptagent
27-
RUN apt-get update
15+
FROM ubuntu:22.04 as production
2816

29-
# Git Clone Install
30-
# RUN apt-get install -y git
31-
# RUN git clone -b dockerfile https://github.com/sammeboy635/wptagent.git
32-
33-
RUN curl -sL https://deb.nodesource.com/setup_16.x | bash -
17+
### TIMEZONE INSIDE THE CONTAINER ###
18+
ARG TIMEZONE=UTC
3419

3520
### UPDATE ###
36-
RUN apt-get update
21+
RUN curl -sL https://deb.nodesource.com/setup_16.x | bash -
22+
RUN apt update
3723

3824
### INSTALL APT-GET LIBS ###
39-
RUN apt-get install -y \
25+
# DEBIAN_FRONTEND prevents interactive prompts while installing
26+
# set default timezone beforehand to avoid user interaction for tzdata package
27+
RUN ln -fs /usr/share/zoneinfo/$TIMEZONE /etc/localtime && DEBIAN_FRONTEND=noninteractive apt install -y \
4028
python3 python3-pip python3-ujson \
4129
imagemagick dbus-x11 traceroute software-properties-common psmisc libnss3-tools iproute2 net-tools openvpn \
4230
libtiff5-dev libjpeg-dev zlib1g-dev libfreetype6-dev liblcms2-dev libwebp-dev tcl8.6-dev tk8.6-dev python3-tk \
4331
python3-dev libavutil-dev libmp3lame-dev libx264-dev yasm autoconf automake build-essential libass-dev libfreetype6-dev libtheora-dev \
4432
libtool libvorbis-dev pkg-config texi2html libtext-unidecode-perl python3-numpy python3-scipy perl \
4533
adb ethtool nodejs cmake git-core libsdl2-dev libva-dev libvdpau-dev libxcb1-dev libxcb-shm0-dev libxcb-xfixes0-dev texinfo wget \
46-
ttf-mscorefonts-installer fonts-noto fonts-roboto fonts-open-sans ffmpeg npm
34+
ttf-mscorefonts-installer fonts-noto fonts-roboto fonts-open-sans ffmpeg npm sudo curl xvfb
4735

48-
### Update the font cache
36+
### UPDATE FONT CACHE ###
4937
RUN fc-cache -f -v
5038

51-
### UPGRADING PIP AND INSTALLING REQUIRED PACKAGES ###
52-
RUN python3 -m pip install --upgrade --user pip && \
53-
python3 -m pip install --user -r /wptagent/.github/workflows/requirements.txt
54-
5539
### INSTALLING LIGHTHOUSE FROM NPM ###
5640
RUN npm install -g lighthouse
5741

5842
### INSTALLING CHROME BROWSER ###
59-
### Fails to Find all libs needed to run
60-
# RUN wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb && \
61-
# dpkg -i google-chrome-stable_current_amd64.deb; exit 0 && \
62-
# apt -f install -y && \
63-
# apt-get install google-chrome-stable
43+
RUN curl -o /tmp/google-chrome-stable_current_amd64.deb https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb && \
44+
apt install -y /tmp/google-chrome-stable_current_amd64.deb && rm /tmp/google-chrome-stable_current_amd64.deb
6445

65-
### BETTER INSTALLING CHROME BROWSER METHOD ###
66-
### Better Installing method but would like to change this to something less complex.
67-
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
68-
&& echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list
69-
RUN apt-get update && apt-get -y install google-chrome-stable ; exit 0
70-
RUN apt-get update --fix-missing -y
71-
RUN apt-get install -f -y
46+
### UPGRADING PIP AND INSTALLING REQUIRED PACKAGES ###
47+
COPY /.github/workflows/requirements.txt /tmp/agent_requirements.txt
48+
RUN python3 -m pip install --upgrade --user pip && \
49+
python3 -m pip install --user -r /tmp/agent_requirements.txt && \
50+
rm /tmp/agent_requirements.txt
7251

73-
### CLEAN UP ###
74-
# We could add some clean up here but in testing it was negotiable
52+
### COPYING ENTIRE DIR TO LOCAL DOCKER /wptagent ###
53+
# see .dockerignore for filterd out folders
54+
# source copy last so we don't need to rebuild all the other layers
55+
COPY / /wptagent
56+
WORKDIR /wptagent
7557

58+
ENTRYPOINT ["/bin/sh", "/wptagent/docker/linux-headless/entrypoint.sh"]
7659

77-
WORKDIR /wptagent
60+
### DEBUG CONTAINER ###
61+
FROM production as debug
62+
63+
### INSTALLING DEBUG DEPENDENCIES ###
64+
RUN pip install debugpy
65+
66+
### COPY DEBUG AGENT AND MOVE REAL ONE ###
67+
RUN mv wptagent.py wptagent_starter.py
68+
COPY wptagent_debug.py wptagent.py
7869

79-
### /bin/bash LOCATION OF COMMAND EXECUTION ###
80-
CMD ["/bin/bash", "/wptagent/docker/linux-headless/entrypoint.sh"]
70+
### SETTING PRODUCTION BUILD AS DEFAULT ###
71+
FROM production

docker/linux-headless/entrypoint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ fi
2929

3030
# exec replaces the shell process by the python process and is required to
3131
# propagate signals (i.e. SIGTERM)
32-
exec python3 /wptagent/wptagent.py --server "${SERVER_URL}" --location "${LOCATION}" ${EXTRA_ARGS} --xvfb --dockerized -vvvv
32+
exec python3 /wptagent/wptagent.py --server "${SERVER_URL}" --location "${LOCATION}" ${EXTRA_ARGS} --xvfb --dockerized -vvvv "@"

docs/docker.md

Lines changed: 59 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,85 @@
1-
# Linux Headless Agent
1+
# Docker Linux Headless Agent
22

3-
To run the agent, simply specify a few environment variables with docker:
3+
The `Dockerfile` has multi-stage definition:
4+
* **production**: Default stage, produce a image without debug features;
5+
* **debug**: When running the produced image the wptagent script will wait for a debugger to.attach
6+
7+
## Build the Image
8+
9+
Arguments can be passed at build time:
10+
* **TIMEZONE**: to set the timezone inside the container. Default `UTC.`
11+
12+
To build the production container with UTC timezone
13+
```bash
14+
docker build --tag wptagent .
15+
```
416

5-
* `SERVER_URL`: will be passed as `--server` (note: it must end with '/work/')
6-
* `LOCATION`: will be passed as `--location`
7-
* `KEY`: will be passed as `--key`
8-
* `NAME`: will be passed as `--name` (optional)
9-
* `SHAPER`: will be passed as `--shaper` (optional)
10-
* `EXTRA_ARGS`: extra command-line options that will be passed through verbatim (optional)
17+
changing the timezone at build time
18+
```bash
19+
docker build --build-arg TIMEZONE=EST .
20+
```
21+
22+
To build the debug container
23+
```bash
24+
docker build --target debug --tag wptagent-debug .
25+
```
1126

1227
## Prerequisites to use traffic shaping in docker
1328
**Experimental**: Running the agent with traffic shaping is experimental. It might
1429
have influence on the host system network. Running multiple agents on the
1530
same host might result in incorrect traffic shaping.
1631

1732
For traffic shaping to work correctly, you need to load the ifb module on the **host**:
18-
33+
```bash
1934
sudo modprobe ifb numifbs=1
35+
```
2036

2137
Also, the container needs `NET_ADMIN` capabilities, so run the container with
2238
`--cap-add=NET_ADMIN`.
2339

24-
To disable traffic-shaping, pass SHAPER="none".
25-
26-
## Container Disk Space Fills Up Quickly
40+
To disable traffic-shaping, pass environment variable at docker un `SHAPER="none"`.
2741

28-
If you see disk space within the container filling up rapidly and you notice
29-
core dump files in the /wptagent folder, try adding `--shm-size=1g` to your Docker run
30-
command. This can help resolve an issue with shared memory and headless Chrome in Docker.
42+
## Run the container
43+
To run the agent, simply specify a few environment variables with docker:
3144

32-
## Example
45+
- `SERVER_URL`: will be passed as `--server` (note: it must end with '/work/')
46+
- `LOCATION`: will be passed as `--location`
47+
- `KEY`: will be passed as `--key`
48+
- `NAME`: will be passed as `--name` (optional)
49+
- `SHAPER`: will be passed as `--shaper` (optional)
50+
- `EXTRA_ARGS`: extra command-line options that will be passed through verbatim (optional)
3351

34-
Build the image first (from project root), load ifb and start it the container:
52+
Build the image first (from project root), load ifb and start it the container.
3553

36-
sudo docker build --tag wptagent .
54+
A typical run :
55+
```bash
3756
sudo modprobe ifb numifbs=1
38-
sudo docker run -d \
57+
docker build --tag wptagent .
58+
docker run -d \
3959
-e SERVER_URL="http://my-wpt-server.org/work/" \
4060
-e LOCATION="docker-location" \
4161
-e NAME="Docker Test" \
4262
--cap-add=NET_ADMIN \
4363
--init \
4464
wptagent
65+
```
66+
67+
Additional parameters can be also passed as additional commands.
68+
A typical run in debug mode, note that we need to expose the port as `50000`:
69+
```bash
70+
sudo modprobe ifb numifbs=1
71+
docker run -d \
72+
-e SERVER_URL=http://127.0.0.1:80/work/ \
73+
-e LOCATION=Test \
74+
--init \
75+
--cap-add=NET_ADMIN \
76+
-p 50000:50000 \
77+
wptagent-debug
78+
--key 123456789
79+
```
4580

81+
## Container Disk Space Fills Up Quickly
82+
83+
If you see disk space within the container filling up rapidly and you notice
84+
core dump files in the /wptagent folder, try adding `--shm-size=1g` to your Docker run
85+
command. This can help resolve an issue with shared memory and headless Chrome in Docker.

wptagent_debug.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import debugpy
2+
import os
3+
# first try to import with the original name
4+
try:
5+
from wptagent import main
6+
except ImportError:
7+
# then import as the contianer require
8+
from wptagent_starter import main
9+
10+
if __name__ == '__main__':
11+
print("Waiting for debug")
12+
debugpy.listen(("0.0.0.0", 50000))
13+
debugpy.wait_for_client()
14+
main()
15+
# Force a hard exit so unclean threads can't hang the agent
16+
os._exit(0)

0 commit comments

Comments
 (0)