diff --git a/README.md b/README.md
index 6efebe1..ba80bea 100644
--- a/README.md
+++ b/README.md
@@ -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
diff --git a/resources/Dockerfile b/resources/Dockerfile
new file mode 100644
index 0000000..5ef646a
--- /dev/null
+++ b/resources/Dockerfile
@@ -0,0 +1,2 @@
+FROM nginx:alpine
+COPY index.html /usr/share/nginx/html/index.html
diff --git a/resources/index.html b/resources/index.html
new file mode 100644
index 0000000..5bc105f
--- /dev/null
+++ b/resources/index.html
@@ -0,0 +1,20 @@
+
+
+
+
+
+
+ Hello World - Nginx Docker
+
+
+
+
+ Hello World - Docker/Nginx
+
+
+
diff --git a/src/clj_test_containers/core.clj b/src/clj_test_containers/core.clj
index dc307a7..4f84909 100644
--- a/src/clj_test_containers/core.clj
+++ b/src/clj_test_containers/core.clj
@@ -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
diff --git a/test/clj_test_containers/core_test.clj b/test/clj_test_containers/core_test.clj
index 9e9de05..a04e439 100644
--- a/test/clj_test_containers/core_test.clj
+++ b/test/clj_test_containers/core_test.clj
@@ -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)]