Skip to content

Commit 6b23f39

Browse files
committed
feat(docker): support running with docker
1 parent 3290757 commit 6b23f39

File tree

8 files changed

+301
-21
lines changed

8 files changed

+301
-21
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
name: docker-latest-image
2+
3+
# This workflow uses actions that are not certified by GitHub.
4+
# They are provided by a third-party and are governed by
5+
# separate terms of service, privacy policy, and support
6+
# documentation.
7+
8+
on:
9+
workflow_dispatch:
10+
push:
11+
branches:
12+
- 'master'
13+
- 'docker-image'
14+
15+
env:
16+
# Use docker.io for Docker Hub if empty
17+
REGISTRY: quay.io
18+
# github.repository as <account>/<repo>
19+
IMAGE_NAME: 'ptcpdump/ptcpdump'
20+
21+
22+
jobs:
23+
build:
24+
25+
runs-on: ubuntu-latest
26+
permissions:
27+
contents: read
28+
29+
steps:
30+
- name: Checkout repository
31+
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4
32+
with:
33+
fetch-depth: '100'
34+
fetch-tags: 'true'
35+
36+
# Set up BuildKit Docker container builder to be able to build
37+
# multi-platform images and export cache
38+
# https://github.com/docker/setup-buildx-action
39+
- name: Set up Docker Buildx
40+
uses: docker/setup-buildx-action@988b5a0280414f521da01fcc63a27aeeb4b104db # v3.6.1
41+
with:
42+
platforms: linux/amd64,linux/arm64
43+
44+
# Login against a Docker registry except on PR
45+
# https://github.com/docker/login-action
46+
- name: Log into registry ${{ env.REGISTRY }}
47+
if: github.event_name != 'pull_request'
48+
uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3.3.0
49+
with:
50+
registry: ${{ env.REGISTRY }}
51+
username: ${{ secrets.QUAY_USER }}
52+
password: ${{ secrets.QUAY_PASSWD }}
53+
54+
# Extract metadata (tags, labels) for Docker
55+
# https://github.com/docker/metadata-action
56+
- name: Extract Docker metadata
57+
id: meta
58+
uses: docker/metadata-action@8e5442c4ef9f78752691e2d8f8d19755c6f78e81 # v5.5.1
59+
with:
60+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
61+
62+
- name: generate tag name
63+
run: |
64+
echo "TAG_NAME=$(git describe --always)" >> $GITHUB_ENV
65+
66+
# Build and push Docker image with Buildx (don't push on PR)
67+
# https://github.com/docker/build-push-action
68+
- name: Build and push Docker image (no latest tag)
69+
id: build-and-push
70+
uses: docker/build-push-action@ca052bb54ab0790a636c9b5f226502c73d547a25 # v5.4.0
71+
with:
72+
context: .
73+
file: Dockerfile
74+
platforms: linux/amd64,linux/arm64
75+
# platforms: linux/amd64
76+
push: ${{ github.event_name != 'pull_request' }}
77+
tags: '${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.TAG_NAME }}'
78+
labels: ${{ steps.meta.outputs.labels }}
79+
cache-from: type=gha
80+
cache-to: type=gha,mode=max
81+
82+
- name: test image
83+
run: |
84+
set -xe
85+
86+
IMAGE='${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.TAG_NAME }}'
87+
bash testdata/test_run_with_docker.sh ${IMAGE}
88+
89+
# Build and push Docker image with Buildx (don't push on PR)
90+
# https://github.com/docker/build-push-action
91+
- name: Build and push Docker image (latest tag)
92+
id: build-and-push-latest
93+
uses: docker/build-push-action@ca052bb54ab0790a636c9b5f226502c73d547a25 # v5.4.0
94+
with:
95+
context: .
96+
file: Dockerfile
97+
platforms: linux/amd64,linux/arm64
98+
# platforms: linux/amd64
99+
push: ${{ github.event_name != 'pull_request' }}
100+
tags: '${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest'
101+
labels: ${{ steps.meta.outputs.labels }}
102+
cache-from: type=gha
103+
cache-to: type=gha,mode=max
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
name: docker-latest-image
2+
3+
# This workflow uses actions that are not certified by GitHub.
4+
# They are provided by a third-party and are governed by
5+
# separate terms of service, privacy policy, and support
6+
# documentation.
7+
8+
on:
9+
workflow_dispatch:
10+
push:
11+
tags:
12+
- v*
13+
14+
15+
env:
16+
# Use docker.io for Docker Hub if empty
17+
REGISTRY: quay.io
18+
# github.repository as <account>/<repo>
19+
IMAGE_NAME: 'ptcpdump/ptcpdump'
20+
21+
22+
jobs:
23+
build:
24+
25+
runs-on: ubuntu-latest
26+
permissions:
27+
contents: read
28+
29+
steps:
30+
- name: Checkout repository
31+
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4
32+
with:
33+
fetch-depth: '100'
34+
fetch-tags: 'true'
35+
36+
# Set up BuildKit Docker container builder to be able to build
37+
# multi-platform images and export cache
38+
# https://github.com/docker/setup-buildx-action
39+
- name: Set up Docker Buildx
40+
uses: docker/setup-buildx-action@988b5a0280414f521da01fcc63a27aeeb4b104db # v3.6.1
41+
with:
42+
platforms: linux/amd64,linux/arm64
43+
44+
# Login against a Docker registry except on PR
45+
# https://github.com/docker/login-action
46+
- name: Log into registry ${{ env.REGISTRY }}
47+
if: github.event_name != 'pull_request'
48+
uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3.3.0
49+
with:
50+
registry: ${{ env.REGISTRY }}
51+
username: ${{ secrets.QUAY_USER }}
52+
password: ${{ secrets.QUAY_PASSWD }}
53+
54+
# Extract metadata (tags, labels) for Docker
55+
# https://github.com/docker/metadata-action
56+
- name: Extract Docker metadata
57+
id: meta
58+
uses: docker/metadata-action@8e5442c4ef9f78752691e2d8f8d19755c6f78e81 # v5.5.1
59+
with:
60+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
61+
62+
- name: generate tag name
63+
run: |
64+
echo TAG_NAME=${{ github.ref }} | sed 's/=v/=/' >> $GITHUB_ENV
65+
66+
# Build and push Docker image with Buildx (don't push on PR)
67+
# https://github.com/docker/build-push-action
68+
- name: Build and push Docker image
69+
id: build-and-push
70+
uses: docker/build-push-action@ca052bb54ab0790a636c9b5f226502c73d547a25 # v5.4.0
71+
with:
72+
context: .
73+
file: Dockerfile
74+
platforms: linux/amd64,linux/arm64
75+
# platforms: linux/amd64
76+
push: ${{ github.event_name != 'pull_request' }}
77+
tags: '${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.TAG_NAME }}'
78+
labels: ${{ steps.meta.outputs.labels }}
79+
cache-from: type=gha
80+
cache-to: type=gha,mode=max

Dockerfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# .github/build.Dockerfile
2+
FROM quay.io/ptcpdump/develop:latest as build
3+
WORKDIR /app
4+
COPY . .
5+
RUN make build
6+
7+
FROM busybox:latest
8+
WORKDIR /ptcpdump
9+
COPY --from=build /app/ptcpdump /usr/local/bin/

Makefile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ CARCH ?= $(shell uname -m)
2828
LIBPCAP_ARCH = $(CARCH)-unknown-linux-gnu
2929
LIBPCAP_CC ?= gcc
3030

31-
IMAGE_BUILD ?= quay.io/ptcpdump/develop:latest
31+
IMAGE_DEV ?= quay.io/ptcpdump/develop:latest
32+
IMAGE_BIN ?= quay.io/ptcpdump/ptcpdump:latest
3233

3334
.PHONY: libpcap
3435
libpcap: $(LIBPCAP_OBJ)
@@ -76,12 +77,12 @@ build-bpf:
7677

7778
.PHONY: build-bpf-via-docker
7879
build-bpf-via-docker:
79-
docker run --rm -v `pwd`:/app quay.io/ptcpdump/develop:latest make build-bpf
80+
docker run --rm -v `pwd`:/app $(IMAGE_DEV) make build-bpf
8081

8182

8283
.PHONY: build-via-docker
8384
build-via-docker:
84-
docker run --rm -v `pwd`:/app quay.io/ptcpdump/develop:latest make build
85+
docker run --rm -v `pwd`:/app $(IMAGE_DEV) make build
8586

8687

8788
.PHONY: lint

README.md

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@ Table of Contents
1818

1919
* [Features](#features)
2020
* [Installation](#installation)
21-
* [Requirements](#requirements)
21+
* [Requirements](#requirements)
2222
* [Usage](#usage)
23-
* [Example commands](#example-commands)
24-
* [Example output](#example-output)
25-
* [Flags](#flags)
23+
* [Example commands](#example-commands)
24+
* [Example output](#example-output)
25+
* [Running with Docker](#running-with-docker)
26+
* [Flags](#flags)
2627
* [Compare with tcpdump](#compare-with-tcpdump)
2728
* [Developing](#developing)
28-
* [Dependencies](#dependencies)
29-
* [Building](#building)
29+
* [Dependencies](#dependencies)
30+
* [Building](#building)
3031

3132

3233
## Features
@@ -60,9 +61,10 @@ Linux kernel version >= 5.2.
6061
### Example commands
6162

6263
Filter like tcpdump:
64+
6365
```
6466
sudo ptcpdump -i eth0 tcp
65-
sudo ptcpdump -i eth0 -A -v tcp and port 80 and host 10.10.1.1
67+
sudo ptcpdump -i eth0 -A -s 0 -n -v tcp and port 80 and host 10.10.1.1
6668
sudo ptcpdump -i eth0 'tcp[tcpflags] & (tcp-syn|tcp-fin) != 0'
6769
```
6870

@@ -145,7 +147,7 @@ Accept: */*
145147
With `-x`:
146148

147149
```
148-
14:44:34.457504 ens33 curl.205562 IP 10.0.2.15.39984 > 139.178.84.217.80: Flags [P.], seq 2722472188:2722472262, ack 892036871, win 64240, length 74, ParentProc [bash.180205]
150+
14:44:34.457504 ens33 curl.205562 Out IP 10.0.2.15.39984 > 139.178.84.217.80: Flags [P.], seq 2722472188:2722472262, ack 892036871, win 64240, length 74, ParentProc [bash.180205]
149151
0x0000: 4500 0072 de2c 4000 4006 6fbf 0a00 020f
150152
0x0010: 8bb2 54d9 9c30 0050 a245 a0fc 352b 6707
151153
0x0020: 5018 faf0 ecfe 0000 4745 5420 2f20 4854
@@ -159,7 +161,7 @@ With `-x`:
159161
With `-X`:
160162

161163
```
162-
14:44:34.457504 ens33 curl.205562 IP 10.0.2.15.39984 > 139.178.84.217.80: Flags [P.], seq 2722472188:2722472262, ack 892036871, win 64240, length 74, ParentProc [bash.180205]
164+
14:44:34.457504 ens33 curl.205562 Out IP 10.0.2.15.39984 > 139.178.84.217.80: Flags [P.], seq 2722472188:2722472262, ack 892036871, win 64240, length 74, ParentProc [bash.180205]
163165
0x0000: 4500 0072 de2c 4000 4006 6fbf 0a00 020f E..r.,@.@.o.....
164166
0x0010: 8bb2 54d9 9c30 0050 a245 a0fc 352b 6707 ..T..0.P.E..5+g.
165167
0x0020: 5018 faf0 ecfe 0000 4745 5420 2f20 4854 P.......GET / HT
@@ -174,6 +176,18 @@ With `-X`:
174176
<p align="right"><a href="#top">🔝</a></p>
175177

176178

179+
### Running with Docker
180+
181+
Docker images for `ptcpdump` are published at https://quay.io/repository/ptcpdump/ptcpdump.
182+
183+
```
184+
docker run --privileged --rm -t --net=host --pid=host \
185+
quay.io/ptcpdump/ptcpdump:latest ptcpdump -i any -c 2 tcp
186+
```
187+
188+
<p align="right"><a href="#top">🔝</a></p>
189+
190+
177191
### Flags
178192

179193

README.zh-CN.md

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,16 @@ Table of Contents
2020

2121
* [Features](#features)
2222
* [Installation](#installation)
23-
* [Requirements](#requirements)
23+
* [Requirements](#requirements)
2424
* [Usage](#usage)
25-
* [Example commands](#example-commands)
26-
* [Example output](#example-output)
27-
* [Flags](#flags)
25+
* [Example commands](#example-commands)
26+
* [Example output](#example-output)
27+
* [Running with Docker](#running-with-docker)
28+
* [Flags](#flags)
2829
* [Compare with tcpdump](#compare-with-tcpdump)
2930
* [Developing](#developing)
30-
* [Dependencies](#dependencies)
31-
* [Building](#building)
31+
* [Dependencies](#dependencies)
32+
* [Building](#building)
3233

3334

3435
## Features
@@ -71,7 +72,7 @@ Table of Contents
7172

7273
```
7374
sudo ptcpdump -i eth0 tcp
74-
sudo ptcpdump -i eth0 -A -v tcp and port 80 and host 10.10.1.1
75+
sudo ptcpdump -i eth0 -A -s 0 -n -v tcp and port 80 and host 10.10.1.1
7576
sudo ptcpdump -i eth0 'tcp[tcpflags] & (tcp-syn|tcp-fin) != 0'
7677
```
7778

@@ -154,7 +155,7 @@ Accept: */*
154155
通过 `-x` 参数以 16 进制格式输出:
155156

156157
```
157-
14:44:34.457504 ens33 curl.205562 IP 10.0.2.15.39984 > 139.178.84.217.80: Flags [P.], seq 2722472188:2722472262, ack 892036871, win 64240, length 74, ParentProc [bash.180205]
158+
14:44:34.457504 ens33 curl.205562 Out IP 10.0.2.15.39984 > 139.178.84.217.80: Flags [P.], seq 2722472188:2722472262, ack 892036871, win 64240, length 74, ParentProc [bash.180205]
158159
0x0000: 4500 0072 de2c 4000 4006 6fbf 0a00 020f
159160
0x0010: 8bb2 54d9 9c30 0050 a245 a0fc 352b 6707
160161
0x0020: 5018 faf0 ecfe 0000 4745 5420 2f20 4854
@@ -168,7 +169,7 @@ Accept: */*
168169
通过 `-X` 参数以 16 进制和 ASCII 格式输出:
169170

170171
```
171-
14:44:34.457504 ens33 curl.205562 IP 10.0.2.15.39984 > 139.178.84.217.80: Flags [P.], seq 2722472188:2722472262, ack 892036871, win 64240, length 74, ParentProc [bash.180205]
172+
14:44:34.457504 ens33 curl.205562 Out IP 10.0.2.15.39984 > 139.178.84.217.80: Flags [P.], seq 2722472188:2722472262, ack 892036871, win 64240, length 74, ParentProc [bash.180205]
172173
0x0000: 4500 0072 de2c 4000 4006 6fbf 0a00 020f E..r.,@.@.o.....
173174
0x0010: 8bb2 54d9 9c30 0050 a245 a0fc 352b 6707 ..T..0.P.E..5+g.
174175
0x0020: 5018 faf0 ecfe 0000 4745 5420 2f20 4854 P.......GET / HT
@@ -183,6 +184,18 @@ Accept: */*
183184
<p align="right"><a href="#top">🔝</a></p>
184185

185186

187+
### Running with Docker
188+
189+
Docker images for `ptcpdump` are published at https://quay.io/repository/ptcpdump/ptcpdump.
190+
191+
```
192+
docker run --privileged --rm -t --net=host --pid=host \
193+
quay.io/ptcpdump/ptcpdump:latest ptcpdump -i any -c 2 tcp
194+
```
195+
196+
<p align="right"><a href="#top">🔝</a></p>
197+
198+
186199
### Flags
187200

188201

testdata/run_with_docker.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bash
2+
3+
set -ex
4+
5+
IMAGE="$1"
6+
TMP=${TMP:-/tmp/}
7+
shift
8+
9+
docker run --privileged --rm -t --net=host --pid=host \
10+
-v /sys/kernel/debug/:/sys/kernel/debug/ \
11+
-v /run/netns/:/run/netns/ \
12+
-v ${TMP}:/tmp/ \
13+
-v `pwd`:/ptcpdump "${IMAGE}" ptcpdump $@

0 commit comments

Comments
 (0)