-
-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add method to build and run an image (#104)
Signed-off-by: Sylvain Cleymans <sylvain.cleymans@gmail.com>
- Loading branch information
Showing
3 changed files
with
88 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
`./db/image/Dockerfile` | ||
```Dockerfile | ||
FROM postgres:latest | ||
|
||
# Add your customizations here | ||
``` | ||
|
||
`./db_test.go` | ||
```go | ||
pool, err := dockertest.NewPool("") | ||
if err != nil { | ||
log.Fatalf("Could not connect to docker: %s", err) | ||
} | ||
|
||
// Build and run the given Dockerfile | ||
resource, err := pool.BuildAndRun("my-postgres-test-image", "./db/image/Dockerfile", []string{}) | ||
if err != nil { | ||
log.Fatalf("Could not start resource: %s", err) | ||
} | ||
|
||
if err = pool.Retry(func() error { | ||
var err error | ||
db, err = sql.Open("postgres", fmt.Sprintf("postgres://postgres:secret@localhost:%s/%s?sslmode=disable", resource.GetPort("5432/tcp"), database)) | ||
if err != nil { | ||
return err | ||
} | ||
return db.Ping() | ||
}); err != nil { | ||
log.Fatalf("Could not connect to docker: %s", err) | ||
} | ||
|
||
// When you're done, kill and remove the container | ||
err = pool.Purge(resource) | ||
``` |