Skip to content

Commit 1661688

Browse files
Added native communication methods for Docker containers and improved Docker Compose configuration
Signed-off-by: dev-priyanshu15 <[email protected]>
1 parent 6c64dd3 commit 1661688

File tree

2 files changed

+185
-71
lines changed

2 files changed

+185
-71
lines changed

gin-redis/README.md

+185-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1+
<h1 align="center"> Keploy Go Samples Apps </h1>
2+
<p align="center">
13
## Introduction
4+
<a href="CODE_OF_CONDUCT.md" alt="Contributions welcome">
5+
<img src="https://img.shields.io/badge/Contributions-Welcome-brightgreen?logo=github" /></a>
26

7+
<a href="https://join.slack.com/t/keploy/shared_invite/zt-12rfbvc01-o54cOG0X1G6eVJTuI_orSA" alt="Slack">
8+
<img src=".github/slack.svg" /></a>
9+
10+
<a href="https://opensource.org/licenses/Apache-2.0" alt="License">
11+
<img src=".github/License-Apache_2.0-blue.svg" /></a>
312
A sample user authentication to test Keploy integration capabilities using [Gin](https://gin-gonic.com/) and [Redis](https://redis.io/).
413

514
## Setup URL shortener
@@ -29,24 +38,99 @@ We need create an alias for Keploy:
2938
alias keploy='sudo docker run --pull always --name keploy-v2 -p 16789:16789 --privileged --pid=host -it -v $(pwd):$(pwd) -w $(pwd) -v /sys/fs/cgroup:/sys/fs/cgroup -v /sys/kernel/debug:/sys/kernel/debug -v /sys/fs/bpf:/sys/fs/bpf -v /var/run/docker.sock:/var/run/docker.sock -v '"$HOME"'/.keploy-config:/root/.keploy-config -v '"$HOME"'/keploy-config:/root/keploy-config --rm ghcr.io/keploy/keploy'
3039
```
3140

32-
### Update the Host
41+
### Update the Redis Connection Settings
42+
43+
> **Important:** When running in Docker, the Redis URL should be `redis:6379` instead of `localhost:6379`. This needs to be updated in `helpers/redis/redisConnect.go` file.
44+
45+
```go
46+
// Change this:
47+
Addr: "localhost:6379"
3348

34-
> **Since, we are on the docker image the Redis URL will be myredis:6379 instead of localhost:6379. This needs to be updated in `helpers/redis/redisConnect.go` file**
49+
// To this:
50+
Addr: "redis:6379"
51+
```
52+
53+
This change is necessary because in Docker, `localhost` points to the container itself, not the Redis service. Docker Compose uses an internal network where each service can be accessed via its container name.
3554

3655
### Create a Docker network
3756
```
3857
sudo docker network create <networkName>
3958
```
4059

41-
### Let's start the Redis Instance
42-
Using the docker-compose file we will start our Redis instance:-
60+
### Using Docker Compose
61+
62+
Create a `docker-compose.yml` file with the following content:
63+
64+
```yaml
65+
version: '3.7'
66+
services:
67+
go-app:
68+
build:
69+
context: .
70+
container_name: ginRedisApp
71+
ports:
72+
- "3001:3001"
73+
environment:
74+
- REDIS_HOST=redis
75+
- REDIS_PORT=6379
76+
depends_on:
77+
- redis
78+
redis:
79+
image: redis
80+
container_name: myredis
81+
ports:
82+
- "6379:6379"
83+
```
84+
85+
This setup properly manages the dependency between the Go application and Redis server.
86+
87+
To start the application using Docker Compose:
88+
4389
```bash
44-
sudo docker run -p 6379:6379 -d --network <networkName> --name myredis redis
90+
docker-compose up
4591
```
92+
93+
### Using individual Docker containers
94+
95+
Alternatively, you can start Redis manually and build the application:
96+
4697
```bash
98+
sudo docker run -p 6379:6379 -d --network <networkName> --name myredis redis
4799
docker build -t gin-app:1.0 .
48100
```
49101

102+
### Communicating with Dockerized App Natively
103+
104+
If you want to run your application in Docker but interact with it from your host machine or other environments, follow these steps:
105+
106+
#### Option 1: Port Mapping (Already included above)
107+
108+
The Docker Compose and individual container setups both include port mapping (`3001:3001`), which allows you to access the containerized application via `localhost:3001` from your host machine.
109+
110+
#### Option 2: Using Docker's IP Address
111+
112+
Find the Docker container's IP address:
113+
114+
```bash
115+
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' ginRedisApp
116+
```
117+
118+
Then use this IP to communicate with the application:
119+
120+
```bash
121+
curl --location 'http://<container-ip>:3001/api/[email protected]&username=shivamsourav'
122+
```
123+
124+
#### Option 3: Docker Host Network (Linux only)
125+
126+
Run your container using the host network:
127+
128+
```bash
129+
docker run --network host --name ginRedisApp gin-app:1.0
130+
```
131+
132+
This will make the container share the host's network stack, allowing you to access it via `localhost:3001`.
133+
50134
### Capture the Testcases
51135

52136
```shell
@@ -101,28 +185,60 @@ Now that we have our testcase captured, run the test file.
101185
```shell
102186
keploy test -c "sudo docker run -p 3001:3001 --rm --network <networkName> --name ginRedisApp gin-app:1.0" --delay 10
103187
```
188+
**Note** :- Issue Creation is disabled on this Repository, please visit [here](https://github.com/keploy/keploy/issues/new/choose) to submit Issue.
104189

105190
So no need to setup dependencies like Redis, web-go locally or write mocks for your testing.
106191

192+
# Keploy Go Samples
193+
This repo contains the sample for [Keploy's](https://keploy.io)🐰 with Golang. Please feel free to contribute if you'd like submit a sample for another use-case or library.
194+
107195
**The application thinks it's talking to Redis 😄**
108196

109197
We will get output something like this:
110198

199+
## Go Sample Apps with Keploy
111200
![TestRun](./img/testRunFail.png)
112201

202+
1. [Echo-MySQL](https://github.com/keploy/samples-go/tree/main/echo-mysql)
203+
2. [Echo-SQL](https://github.com/keploy/samples-go/tree/main/echo-sql)
204+
3. [Fasthttp-Postgress](https://github.com/keploy/samples-go/tree/main/fasthttp-postgres)
205+
4. [Gin-Mongo](https://github.com/keploy/samples-go/tree/main/gin-mongo)
206+
5. [Gin-Redis](https://github.com/keploy/samples-go/tree/main/gin-redis)
207+
6. [Go-Grpc](https://github.com/keploy/samples-go/tree/main/go-grpc)
208+
7. [Go-Jwt](https://github.com/keploy/samples-go/tree/main/go-jwt)
209+
8. [Go-Twilio](https://github.com/keploy/samples-go/tree/main/go-twilio)
210+
9. [GraphQL-SQL](https://github.com/keploy/samples-go/tree/main/graphql-sql)
211+
10. [Mux-SQL](https://github.com/keploy/samples-go/tree/main/mux-sql)
212+
11. [Mux-Elasticsearch](https://github.com/keploy/samples-go/tree/main/mux-elasticsearch)
213+
12. [Mux-MySQL](https://github.com/keploy/samples-go/tree/main/mux-mysql)
214+
13. [S3-Keploy](https://github.com/keploy/samples-go/tree/main/S3-Keploy)
215+
14. [SSE-Svelte](https://github.com/keploy/samples-go/tree/main/sse-svelte)
216+
15. [Users-Profile](https://github.com/keploy/samples-go/tree/main/users-profile)
217+
16. [HTTP-PokeAPI](https://github.com/keploy/samples-go/tree/main/http-pokeapi)
218+
17. [book-store-inventory (`gin + sqlite`) ](https://github.com/keploy/samples-go/tree/main/book-store-inventory)
219+
113220
#### Let's add token to Noisy field:
114221

115222
In `test-2.yml` go to the noisefield and `-body.token` in noise. Now, it's the time to run the test cases again.
116223

224+
## Community Support ❤️
117225
```bash
118226
keploy test -c "sudo docker run -p 3001:3001 --rm --network <networkName> --name ginRedisApp gin-app:1.0" --delay 10
119227
```
120-
228+
### 🤔 Questions?
229+
Reach out to us. We're here to help!
121230
This time all the test cases will pass.
122231
![testruns](./img/testRunPass.png?raw=true "Recent testruns")
123232

233+
[![Slack](https://img.shields.io/badge/Slack-4A154B?style=for-the-badge&logo=slack&logoColor=white)](https://join.slack.com/t/keploy/shared_invite/zt-12rfbvc01-o54cOG0X1G6eVJTuI_orSA)
234+
[![LinkedIn](https://img.shields.io/badge/linkedin-%230077B5.svg?style=for-the-badge&logo=linkedin&logoColor=white)](https://www.linkedin.com/company/keploy/)
235+
[![YouTube](https://img.shields.io/badge/YouTube-%23FF0000.svg?style=for-the-badge&logo=YouTube&logoColor=white)](https://www.youtube.com/channel/UC6OTg7F4o0WkmNtSoob34lg)
236+
[![Twitter](https://img.shields.io/badge/Twitter-%231DA1F2.svg?style=for-the-badge&logo=Twitter&logoColor=white)](https://twitter.com/Keployio)
237+
124238
## Run app Natively on local machine
125239

240+
### 💖 Let's Build Together!
241+
Whether you're a newbie coder or a wizard 🧙‍♀️, your perspective is golden. Take a peek at our:
126242
Keploy can be installed on Linux directly and on Windows with the help of WSL. Based on your system archieture, install the keploy latest binary release
127243

128244
**1. AMD Architecture**
@@ -240,4 +356,66 @@ sudo -E keploy test -c "./gin-redis" --delay 10
240356

241357
This time all the test cases will pass.
242358

243-
![testruns](./img/testRunPass.png?raw=true "Recent testruns")
359+
![testruns](./img/testRunPass.png?raw=true "Recent testruns")
360+
361+
## Known Issues and Fixes
362+
363+
### Redis Connectivity Issues in Dockerized Gin Application
364+
365+
When using Docker, you might face connectivity issues between the Gin application and Redis. Here's how to resolve them:
366+
367+
#### Problem 1: Redis Connection Settings in Go Application
368+
**Issue:**
369+
The Go application might be configured to connect to Redis using:
370+
```go
371+
Addr: "localhost:6379"
372+
```
373+
This configuration works when running the application locally (without Docker) but fails in a containerized environment.
374+
375+
**Solution:**
376+
Update the Redis address in the application code to:
377+
```go
378+
Addr: "redis:6379"
379+
```
380+
Where `redis` matches the service name defined in `docker-compose.yml`.
381+
382+
#### Problem 2: Docker Compose Configuration
383+
**Issue:**
384+
The Docker Compose setup might not properly manage the dependency between the Go application and the Redis server.
385+
386+
**Solution:**
387+
Improve the `docker-compose.yml` to properly manage service dependencies as shown in the Docker Compose section above.
388+
389+
#### Environment-Variable Based Configuration (Recommended)
390+
391+
For a more flexible setup, modify your Go code to use environment variables:
392+
393+
```go
394+
func RedisConnect() *redis.Client {
395+
redisHost := os.Getenv("REDIS_HOST")
396+
if redisHost == "" {
397+
redisHost = "localhost" // Default fallback
398+
}
399+
400+
redisPort := os.Getenv("REDIS_PORT")
401+
if redisPort == "" {
402+
redisPort = "6379" // Default fallback
403+
}
404+
405+
rdb := redis.NewClient(&redis.Options{
406+
Addr: redisHost + ":" + redisPort,
407+
Password: "",
408+
DB: 0,
409+
})
410+
411+
return rdb
412+
}
413+
```
414+
📜 [Contribution Guidelines](https://github.com/keploy/keploy/blob/main/CONTRIBUTING.md)
415+
This way, you can easily configure the Redis connection for different environments without changing the code.
416+
417+
418+
❤️ [Code of Conduct](https://github.com/keploy/keploy/blob/main/CODE_OF_CONDUCT.md)
419+
**Future Improvements:**
420+
- Implement retry logic in the Go application to handle situations where Redis is not immediately available.
421+
- Add health checks to the Docker Compose file to ensure Redis is fully initialized before starting the Go application.

gin-redis/keploy.yml

-64
This file was deleted.

0 commit comments

Comments
 (0)