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
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ never happen.

## Usage

1. Run the API proxy (`--privileged` flag is required here because it connects with the
docker socket, which is a privileged connection in some SELinux/AppArmor contexts
and would get locked otherwise):
1. Run the API proxy. `--privileged` is requested so the container can open the Docker
socket on hosts where SELinux or AppArmor confines that socket. Hosts that do not
confine the socket do not need the flag:

$ docker container run \
-d --privileged \
Expand Down Expand Up @@ -168,33 +168,34 @@ poetry install

### Testing

To run the tests locally, add `--prebuild` to autobuild the image before testing:
`--image` selects the image pytest runs. It defaults to `docker-socket-proxy:local`.
`--prebuild` builds that same tag before the suite starts, which is also what CI runs:

```sh
poetry run pytest --prebuild
```

By default, the image that the tests use (and optionally prebuild) is named
`docker-socket-proxy:local`. If you prefer, you can build it separately before testing,
and remove the `--prebuild` flag, to run the tests with that image you built:
You can build the default tag yourself and run the suite without `--prebuild`:

```sh
docker image build -t docker-socket-proxy:local .
poetry run pytest
```

If you want to use a different image, export the `DOCKER_IMAGE_NAME` env variable with
the name you want:
Pass `--image` to use a different tag, with or without `--prebuild`:

```sh
# To build it automatically
env DOCKER_IMAGE_NAME=my_custom_image poetry run pytest --prebuild
poetry run pytest --prebuild --image my_custom_image

# To prebuild it separately
docker image build -t my_custom_image .
env DOCKER_IMAGE_NAME=my_custom_image poetry run pytest
poetry run pytest --image my_custom_image
```

The suite publishes the proxy on `127.0.0.1` only. It requests `--privileged` for the
same reason as the usage example: so the container can open the Docker socket on hosts
where SELinux or AppArmor confines it. Hosts that do not confine the socket do not need
that flag.

## Logging

You can set the logging level or severity level of the messages to be logged with the
Expand Down
13 changes: 8 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def pytest_addoption(parser):
parser.addoption(
"--image",
action="store",
default="test:docker-socket-proxy",
default="docker-socket-proxy:local",
help="Specify testing image name",
)

Expand Down Expand Up @@ -63,8 +63,11 @@ def _proxy(**env_vars):
"container",
"run",
"--detach",
# Requested so the container can open the Docker socket on hosts
# where SELinux or AppArmor confines it. Hosts that do not confine
# the socket do not need this flag.
"--privileged",
"--publish=2375",
"--publish=127.0.0.1::2375",
"--volume=/var/run/docker.sock:/var/run/docker.sock",
*env_list,
image,
Expand All @@ -73,9 +76,9 @@ def _proxy(**env_vars):
container_data = json.loads(
docker("container", "inspect", container_id.strip())
)
socket_port = container_data[0]["NetworkSettings"]["Ports"]["2375/tcp"][0][
"HostPort"
]
published = container_data[0]["NetworkSettings"]["Ports"]["2375/tcp"][0]
assert published["HostIp"] == "127.0.0.1"
socket_port = published["HostPort"]
with local.env(DOCKER_HOST=f"tcp://localhost:{socket_port}"):
yield container_id
finally:
Expand Down