-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
allow containers to connect to the host machine #79
Comments
hacky approach of achieving erikengervall#79 (comment) (for testing purposes)
First Idea regarding tackling this issue: Need to find out whether In case it is run inside a container we have to create a network for linking all runner containers to the dockest container. # create network
docker network create --driver bridge dockest_bridge_network
# add container that runs dockest to network
docker network connect dockest_bridge_network "$HOSTNAME"
# add remaining runner containers to the network
docker network connect dockest_bridge_network service-foobars
docker network connect dockest_bridge_network service-foobars
docker network connect dockest_bridge_network service-abcdef I tested this locally and it seems to work fine. The next problem is that connection check against ports must be done against the A quick solution would be to allow the following (or at least fallback to do this instead of using const runner = new runners.GeneralPurposeRunner({
service: "website",
build: "./app",
ports: {
"9000": "9000"
},
responsivenessTimeout: 30 * 1000,
connectionTimeout: 30 * 1000,
// return containerId as host
host: containerId => containerId
}); I hacked this into my testing fork (n1ru4l@de5bbfe) It would then also be helpful to add runtime helper that allows resolving the hostname of a given service at runtime. test("asdadasd", async () => {
// throws in case service does not exist/ is not up
const hostname = await getServiceHostname("website")
fetch(hostname) // ... do sth with the hostname
}) @erikengervall Please let me know what you think. |
Initially this responsibility could be placed on the user (i.e. via configuration) until a reliable solution is found.
Hm, is it safe to assume that all projects wants all services to be linked to one another? The default mode could definitely hook everything up to each other and down the line an option could be introduced where the networking can be managed more granularly.
Yeah definitely. If it's possible to globalize this type of information I'd be thrilled! |
We could either go that route or generate a network for each pair (dockest container and service container). I agree for keeping it simple and add a more granular configuration option in the future.
I will do some research! |
Looks like a solid library, could definitely use it in the first iteration of this feature.
Awesome, thanks! |
There is no simple way of doing this. jestjs/jest#7184 We could, however, export a module from "dockest" that could be used for doing such stuff. It could be an abstraction above the example api: import { getServiceHost } from "dockest/jest"
test("my test", async () => {
const host = await getServiceHost("website")
}) The remaining problem would be the following: how does the Alternative approach: Give the containers that you connect to the container an alias that is equivalent to the one in the import Dockest, { runners, logLevel } from "dockest";
const runner = new runners.GeneralPurposeRunner({
service: "website",
build: "./app",
ports: {
"9000": "9000"
},
responsivenessTimeout: 30 * 1000,
connectionTimeout: 30 * 1000,
// return continerId as host
host: containerId => containerId
});
const dockest = new Dockest({
opts: {
logLevel: logLevel.DEBUG
}
});
dockest.attachRunners([runner]);
dockest.run(); docker network connect dockest_bridge_network --alias website service-host The |
…it is executed inside a container or not. This is a prerequisite for using dockest inside docker containers. see erikengervall#79
I got a first docker-in-docker solution working over here: n1ru4l#1. Will create a pull request over here once my other ones are merged. The dockest in docker approach is working fine! However, there are some complications with connecting to the host machine. On Mac OSX and windows, you can reach the docker host trough the hostname However, there are various hacks available, the easiest seemed to be the following: check whether if (process.platform === 'linux' && !runner.isBridgeNetworkMode) {
const command = ` \
docker exec ${runner.containerId} \
/bin/sh -c "ip -4 route list match 0/0 | awk '{print \\$3\\" host.docker.internal\\"}' >> /etc/hosts"
`
await execa(command)
} The next issue I have is that ideally, I want to run We can share the docker-compose config with the test specs by setting them to |
…it is executed inside a container or not. This is a prerequisite for using dockest inside docker containers. see erikengervall#79
…ainer support (#91) * patch: allow to resolve the container host based on the containerId * feat: add internal config option for making dockest aware of whether it is executed inside a container or not. This is a prerequisite for using dockest inside docker containers. see #79 * feat: create bridge network if executed inside docker container. Detect whether dockest is run inside a docker container. Create network and link all runner containers to the dockest container. * feat: use target port and alias host in inside docker container mode Revert option of using a function for setting the host. Automatically set the host to the service name in dockest inside docker container mode. * chore: add aws-codebuild example. Example that showcases dockest in docker. Works inside docker container and outside docker container. * chore: fix eslint errors * test: update snapshots * chore: run aws-code-build-example on ci. * chore: disable tests outside container * feat: add helper methods that easily allow writing tests for "dockest inside docker" and "dockest on docker host" environments. * refactor: change approach of adding hostnames * test: pin docker image to hash * Update src/index.ts Co-Authored-By: Erik Engervall <erik.engervall@gmail.com>
In one of my integration tests, I am starting a webserver (via jest) and check whether a webhook on that webserver is called by a service in a docker container.
There are several options for this: https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach
It would be awesome if this could be easily achieved with this library!
Edit:
Some more thoughts:
On platforms like AWS Codebuild, the code is already built inside a container. For starting additional containers the docker socket is injected into the container. That means if you start a new container and want it to be able to connect to the main "test" container you will have to set up a network between those containers.
The text was updated successfully, but these errors were encountered: