diff options
| author | Mel <einebeere@gmail.com> | 2024-02-17 02:09:26 +0100 |
|---|---|---|
| committer | Mel <einebeere@gmail.com> | 2024-02-17 02:09:26 +0100 |
| commit | c1cab90dbcd0f1fc743cb733114064f602766db6 (patch) | |
| tree | f63817122163eb7ef99b7e329279a6225f2df440 | |
| download | single-container-example-c1cab90dbcd0f1fc743cb733114064f602766db6.tar.zst single-container-example-c1cab90dbcd0f1fc743cb733114064f602766db6.zip | |
Single container with a service and Postgres main
| -rw-r--r-- | Dockerfile | 14 | ||||
| -rw-r--r-- | Makefile | 8 | ||||
| -rwxr-xr-x | script.sh | 4 | ||||
| -rw-r--r-- | service.c | 22 |
4 files changed, 48 insertions, 0 deletions
diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..3f3de0f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +FROM alpine:latest AS build +WORKDIR /build +RUN apk update && \ + apk add --no-cache gcc musl-dev postgresql-dev + +COPY service.c . +RUN gcc -o service service.c -lpq + +FROM postgres:alpine + +COPY script.sh . +COPY --from=build /build/service . + +ENTRYPOINT ["./script.sh"] diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..511c3c3 --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +.PHONY: build-image run-container +.DEFAULT_GOAL := run-container + +build-image: + docker build -t onecontainer . + +run-container: build-image + docker run -e POSTGRES_HOST_AUTH_METHOD=trust -it --rm onecontainer diff --git a/script.sh b/script.sh new file mode 100755 index 0000000..3a6e0f5 --- /dev/null +++ b/script.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +/usr/local/bin/docker-entrypoint.sh postgres & +./service diff --git a/service.c b/service.c new file mode 100644 index 0000000..edc6fd0 --- /dev/null +++ b/service.c @@ -0,0 +1,22 @@ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <libpq-fe.h> + +int main() { + PGconn *conn; + for (int i = 0; i < 100; i++) { + printf("trying to connect...\n"); + conn = PQconnectdb("host=localhost dbname=postgres user=postgres password="); + if (PQstatus(conn) == CONNECTION_OK) { + printf("connected :)\n"); + break; + } else { + fprintf(stderr, "connection failed: %s", PQerrorMessage(conn)); + sleep(2); + } + } + + PQfinish(conn); +} + |
