Add create-from-docker-file

This commit is contained in:
Burin Choomnuan 2020-07-17 11:31:14 -04:00
parent a3fda15a49
commit d2237b08b0
5 changed files with 86 additions and 4 deletions

View file

@ -31,6 +31,29 @@ The library provides a set of functions to interact with the testcontainers. A s
(tc/stop! container)
```
To create a container using your local Dockerfile
```clojure
;; Example via test
(require '[clojure.test :refer :all])
(require '[clj-test-containers.core :refer :all])
(testing "Testing basic testcontainer image creation from docker file"
(let [container (create-from-docker-file {:env-vars
{"FOO" "bar"
"MAGIC_NUMBER" "42"}
:exposed-ports [80]
:docker-file "resources/Dockerfile"})
initialized-container (start! container)
stopped-container (stop! container)]
(is (some? (:id initialized-container)))
(is (some? (:mapped-ports initialized-container)))
(is (some? (get (:mapped-ports initialized-container) 80)))
(is (nil? (:id stopped-container)))
(is (nil? (:mapped-ports stopped-container)))))
```
## Functions and Properties
### create

2
resources/Dockerfile Normal file
View file

@ -0,0 +1,2 @@
FROM nginx:alpine
COPY index.html /usr/share/nginx/html/index.html

20
resources/index.html Normal file
View file

@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Hello World - Nginx Docker</title>
<style>
h1{
font-weight:lighter;
font-family: Arial, Helvetica, sans-serif;
}
</style>
</head>
<body>
<h1>
Hello World - Docker/Nginx
</h1>
</body>
</html>

View file

@ -1,9 +1,14 @@
(ns clj-test-containers.core
(:require [clojure.java.io :as io])
(:import [org.testcontainers.containers
GenericContainer]
[org.testcontainers.utility
MountableFile]
[org.testcontainers.containers BindMode]))
[org.testcontainers.containers BindMode]
[org.testcontainers.images.builder
ImageFromDockerfile]
[java.nio.file
Path Paths]))
(defn- resolve-bind-mode
[bind-mode]
@ -29,6 +34,24 @@
:env-vars (.getEnvMap container)
:host (.getHost container)}))
(defn create-from-docker-file
[{:keys [exposed-ports env-vars command docker-file]
:or {docker-file "Dockerfile"}}]
(let [docker-image (ImageFromDockerfile.)
docker-image (-> docker-image
(.withDockerfile (Paths/get "." (into-array [docker-file]))))
container (GenericContainer. docker-image)]
(.setExposedPorts container (map int exposed-ports))
(if env-vars
(doseq [[k v] env-vars]
(.addEnv container k v)))
(if command
(.setCommand container command))
{:container container
:exposed-ports (.getExposedPorts container)
:env-vars (.getEnvMap container)
:host (.getHost container)}))
(defn map-classpath-resource!
"Maps a resource in the classpath to the given container path. Should be called before starting the container!"
[container-config

View file

@ -3,7 +3,7 @@
[clj-test-containers.core :refer :all]))
(deftest init-test
(testing "Testing basic testcontainer generic image initialisation"
(testing "Testing basic testcontainer generic image initialization"
(let [container (create {:image-name "postgres:12.2"
:exposed-ports [5432]
:env-vars {"POSTGRES_PASSWORD" "pw"}})
@ -13,6 +13,20 @@
(is (some? (:mapped-ports initialized-container)))
(is (some? (get (:mapped-ports initialized-container) 5432)))
(is (nil? (:id stopped-container)))
(is (nil? (:mapped-ports stopped-container)))))
(testing "Testing basic testcontainer image creation from docker file"
(let [container (create-from-docker-file {:env-vars
{"FOO" "bar"
"MAGIC_NUMBER" "42"}
:exposed-ports [80]
:docker-file "resources/Dockerfile"})
initialized-container (start! container)
stopped-container (stop! container)]
(is (some? (:id initialized-container)))
(is (some? (:mapped-ports initialized-container)))
(is (some? (get (:mapped-ports initialized-container) 80)))
(is (nil? (:id stopped-container)))
(is (nil? (:mapped-ports stopped-container))))))
(deftest execute-command-in-container
@ -51,8 +65,8 @@
:exposed-ports [5432]
:env-vars {"POSTGRES_PASSWORD" "pw"}})
(bind-filesystem! {:host-path "."
:container-path "/opt"
:mode :read-only}))
:container-path "/opt"
:mode :read-only}))
initialized-container (start! container)
file-check (execute-command! initialized-container ["tail" "/opt/README.md"])
stopped-container (stop! container)]