Skip to content

Commit 9f15fc3

Browse files
author
Alex Chantavy
authored
Make docker and docker-compose work on WSL2 (#1375)
### Summary > Describe your changes. Fixes the docker-compose steps to work on WSL2 (Windows Subsystem for Linux 2) and OSX. Updates documentation to use the new cncf tag when building the container. docker-compose is very helpful for dev setups and trying out cartography. ### Checklist ### Testing performed I started with a fresh clone of this branch and on _both_ OSX and WSL2ran 1. `docker build -t cartography-cncf/cartography-dev -f dev.Dockerfile ./` 1. `docker-compose run cartography-dev make test` I can confirm that both paths worked and ran the full test suite using docker-compose. #### WSL2 Linter: ![image](https://github.com/user-attachments/assets/b165feb6-0d0a-4a9c-90dd-fe47e8390b0d) Unit tests: ![image](https://github.com/user-attachments/assets/4cfa8a41-6775-4ac1-ad9c-6052163c0a17) Integration tests: ![image](https://github.com/user-attachments/assets/f134dec5-8df2-4c9b-a7f6-57cccac51f89) #### OSX Linter: <img width="1000" alt="Screenshot 2024-11-03 at 12 29 49 AM" src="https://github.com/user-attachments/assets/e7840c2a-8065-4163-8057-3de902532291"> Unit tests: <img width="997" alt="Screenshot 2024-11-03 at 12 30 07 AM" src="https://github.com/user-attachments/assets/11d91723-776c-4438-b2a1-fe783e75e414"> Integration tests: <img width="1003" alt="Screenshot 2024-11-03 at 12 30 22 AM" src="https://github.com/user-attachments/assets/c2360ad5-a2c2-4309-91d0-1080dc9d0bb3"> --------- Signed-off-by: Alex Chantavy <[email protected]>
1 parent 5029b00 commit 9f15fc3

File tree

8 files changed

+51
-34
lines changed

8 files changed

+51
-34
lines changed

.cache/.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# This .gitignore is a placeholder so that we can store .cache/ in github.
2+
# We are including a pre-created .cache in Github so that when
3+
# we run docker-compose in WSL2 during dev linting, WSL2 does not attempt
4+
# to create .cache/ with root as the owner. The contents of .cache
5+
# should never be pushed back upstream to the main cartography repo.

.env

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
# This file is for use with docker compose so that mounting Neo4j volumes doesn't fail with perms errs
2-
GID=10001
3-
UID=10001
1+
# This file is for docker-compose dev use so that mounting
2+
# Neo4j volumes doesn't fail with permissions errors.
3+
GID=1000
4+
UID=1000

Dockerfile

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# This is a thin distribution of the cartography software.
2+
# It is published at ghcr.io.
23
FROM python:3.10-slim
34

45
# the UID and GID to run cartography as

dev.Dockerfile

+20-15
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
1-
# Builds cartography container for development by performing a Python editable install of the current source code.
1+
# This image is for dev only.
2+
# Performs a Python editable install of the current Cartography source.
3+
# Assumptions:
4+
# - This dockerfile will get called with .cache as a volume mount.
5+
# - The current working directory on the host building this container
6+
# is the cartography source tree from github.
27
FROM python:3.10-slim
38

4-
# the UID and GID to run cartography as
5-
# (https://github.com/hexops/dockerfile#do-not-use-a-uid-below-10000).
6-
ARG uid=10001
7-
ARG gid=10001
9+
# The UID and GID to run cartography as.
10+
# This needs to match the gid and uid on the host.
11+
# Update this to match. On WSL2 this is usually 1000.
12+
ARG uid=1000
13+
ARG gid=1000
814

915
RUN apt-get update && \
1016
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends make git && \
1117
apt-get clean && \
1218
rm -rf /var/lib/apt/lists/*
1319

14-
# Assumption: current working directory is the cartography source tree from github.
15-
COPY . /var/cartography
20+
# Install dependencies.
1621
WORKDIR /var/cartography
17-
ENV HOME=/var/cartography
22+
COPY . /var/cartography
23+
RUN pip install -r test-requirements.txt && \
24+
pip install -U -e . && \
25+
chmod -R a+w /var/cartography
1826

19-
RUN pip install -U -e . && \
20-
pip install -r test-requirements.txt && \
21-
# Grant write access to the directory for unit and integration test coverage files
22-
chmod -R a+w /var/cartography && \
23-
# Sets the directory as safe due to a mismatch in the user that cloned the repo
24-
# and the user that is going to run the unit&integ tests. This lets pre-commit work.
25-
git config --global --add safe.directory /var/cartography && \
27+
# Now copy the entire source tree.
28+
ENV HOME=/var/cartography
29+
# Necessary for pre-commit.
30+
RUN git config --global --add safe.directory /var/cartography && \
2631
git config --local user.name "cartography"
2732

2833
USER ${uid}:${gid}

docker-compose.yml

+10-6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ services:
1616
- ./.compose/neo4j/import:/import
1717
- ./.compose/neo4j/logs:/logs
1818
- ./.compose/neo4j/plugins:/plugins
19-
user: "${UID}:${GID}"
2019
environment:
2120
# Raise memory limits:
2221
- NEO4J_dbms_memory_pagecache_size=1G
@@ -34,14 +33,15 @@ services:
3433
# Networking:
3534
- dbms.connector.bolt.listen_address=0.0.0.0:7687
3635
healthcheck:
37-
test: ["CMD", "curl", "-f", "http://localhost:7474"]
36+
test: ["CMD", "wget", "--no-verbose", "http://localhost:7474"]
3837
interval: 10s
3938
timeout: 10s
4039
retries: 10
4140

4241
# Runs the standard cartography image available at ghcr.io.
4342
cartography:
44-
image: ghcr.io/lyft/cartography:latest
43+
image: ghcr.io/cartography-cncf/cartography:latest
44+
platform: linux/x86_64
4545
# EXAMPLE: Our ENTRYPOINT is cartography, running specific command to sync AWS
4646
# command: ["-v", "--neo4j-uri=bolt://neo4j:7687", "--aws-sync-all-profiles"]
4747
init: true
@@ -50,6 +50,7 @@ services:
5050
- neo4j
5151
volumes:
5252
# Provide AWS creds to the container
53+
# Add other volumes here to support other data providers.
5354
- ~/.aws:/var/cartography/.aws/
5455
environment:
5556
# Point to the neo4j service defined in this docker-compose file.
@@ -58,9 +59,8 @@ services:
5859
# Intended to run local automated tests, custom sync scripts, and local changes.
5960
cartography-dev:
6061
# See dev instructions: we assume that you have built this with
61-
# `docker build -t lyft/cartography-dev . -f dev.Dockerfile`.
62-
# Do not push this image remotely!
63-
image: lyft/cartography-dev
62+
# `docker build -t cartography-cncf/cartography-dev -f dev.Dockerfile ./`
63+
image: cartography-cncf/cartography-dev
6464
init: true
6565
restart: on-failure
6666
depends_on:
@@ -71,6 +71,10 @@ services:
7171
# For pre-commit to work
7272
- .:/var/cartography
7373
- ./.cache/pre-commit:/var/cartography/.cache/pre-commit
74+
# for git, for precommit
75+
- ./.git:/var/cartography/.git
7476
environment:
7577
# Point to the neo4j service defined in this docker-compose file.
7678
- NEO4J_URL=bolt://cartography-neo4j-1:7687
79+
# this is actually needed
80+
- PRE_COMMIT_HOME=/var/cartography/.cache

docs/root/dev/developer-guide.md

+7-8
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ We include a dev.Dockerfile that can help streamline common dev tasks. It is dif
9999
To use it, build dev.Dockerfile with
100100
```bash
101101
cd /path/to/cartography/repo
102-
docker build -t lyft/cartography-dev -f . dev.Dockerfile
103-
docker-compose --profile dev up -d
102+
docker build -t cartography-cncf/cartography-dev -f dev.Dockerfile ./
104103
```
105104

106105
With that, there are some interesting things you can do with it.
@@ -137,7 +136,7 @@ If you don't like docker-compose or if it doesn't work for you for any reason, h
137136
#### Run unit tests with dev.Dockerfile
138137

139138
```bash
140-
docker run --rm lyft/cartography-dev make test_unit
139+
docker run --rm cartography-cncf/cartography-dev make test_unit
141140
```
142141

143142
This is a simple command because it doesn't require any volume mounts or docker networking.
@@ -148,7 +147,7 @@ This is a simple command because it doesn't require any volume mounts or docker
148147
docker run --rm \
149148
-v $(pwd):/var/cartography \
150149
-v $(pwd)/.cache/pre-commit:/var/cartography/.cache/pre-commit \
151-
lyft/cartography-dev \
150+
cartography-cncf/cartography-dev \
152151
make test_lint
153152
```
154153

@@ -173,7 +172,7 @@ and then call the integration test suite like this:
173172
docker run --rm \
174173
--network cartography-network \
175174
-e NEO4J_URL=bolt://cartography-neo4j:7687 \
176-
lyft/cartography-dev \
175+
cartography-cncf/cartography-dev \
177176
make test_integration
178177
```
179178

@@ -200,20 +199,20 @@ docker run --rm \
200199
-v $(pwd)/.cache/pre-commit:/var/cartography/.cache/pre-commit \
201200
--network cartography-network \
202201
-e NEO4J_URL=bolt://cartography-neo4j:7687 \
203-
lyft/cartography-dev \
202+
cartography-cncf/cartography-dev \
204203
make test
205204
```
206205

207206
#### Run a [custom sync script](#implementing-custom-sync-commands) with dev.Dockerfile
208207

209208
```bash
210-
docker run --rm lyft/cartography-dev python custom_sync.py
209+
docker run --rm cartography-cncf/cartography-dev python custom_sync.py
211210
```
212211

213212
#### Run cartography CLI with dev.Dockerfile
214213

215214
```bash
216-
docker run --rm lyft/cartography-dev cartography --help
215+
docker run --rm cartography-cncf/cartography-dev cartography --help
217216
```
218217

219218
## How to write a new intel module

docs/root/install.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ Read on to see [other things you can do with Cartography](#things-to-do-next).
124124
-v ~/.aws:/var/cartography/.aws/ \
125125
-e AWS_PROFILE=1234_testprofile \
126126
-e AWS_DEFAULT_REGION=us-east-1 \
127-
lyft/cartography --neo4j-uri bolt://cartography-neo4j:7687
127+
cartography-cncf/cartography --neo4j-uri bolt://cartography-neo4j:7687
128128
```
129129

130130
If things work, your terminal will look like this where you see log messages displaying how many assets are being loaded to the graph:
@@ -139,7 +139,7 @@ Read on to see [other things you can do with Cartography](#things-to-do-next).
139139

140140
- `AWS_DEFAULT_REGION` must be specified.
141141
- Our docker-compose.yml maps in `~/.aws/` on your host machine to `/var/cartography/.aws` in the cartography container, so the container has access to AWS profile and credential files.
142-
- You can view a full list of Cartography's CLI arguments by running `docker run lyft/cartography --help`.
142+
- You can view a full list of Cartography's CLI arguments by running `docker run cartography-cncf/cartography --help`.
143143
144144
1. **View the graph.**
145145

tests/unit/cartography/intel/github/test_github.py

+2
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
from tests.data.github.rate_limit import RATE_LIMIT_RESPONSE_JSON
1717

1818

19+
@patch('cartography.intel.github.util.time.sleep')
1920
@patch('cartography.intel.github.util.handle_rate_limit_sleep')
2021
@patch('cartography.intel.github.util.fetch_page')
2122
def test_fetch_all_handles_retries(
2223
mock_fetch_page: Mock,
2324
mock_handle_rate_limit_sleep: Mock,
25+
mock_sleep: Mock,
2426
) -> None:
2527
'''
2628
Ensures that fetch_all re-reaises the same exceptions when exceeding retry limit

0 commit comments

Comments
 (0)