nginx-golang-postgres: add dev envs config (#275)

* Add Docker Desktop Development Environments config
* Upgrade to Go 1.18
* Replace nginx build with image + read-only bind mount

Signed-off-by: Milas Bowman <milas.bowman@docker.com>
This commit is contained in:
Milas Bowman 2022-07-12 04:08:02 -04:00 committed by GitHub
parent 9d547d23fb
commit 7f5179ea3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 156 additions and 32 deletions

View File

@ -0,0 +1,50 @@
services:
backend:
build:
context: backend
target: dev-envs
volumes:
- /var/run/docker.sock:/var/run/docker.sock
secrets:
- db-password
depends_on:
db:
condition: service_healthy
db:
image: postgres
restart: always
user: postgres
secrets:
- db-password
volumes:
- db-data:/var/lib/postgresql/data
environment:
- POSTGRES_DB=example
- POSTGRES_PASSWORD_FILE=/run/secrets/db-password
expose:
- 5432
healthcheck:
test: [ "CMD", "pg_isready" ]
interval: 10s
timeout: 5s
retries: 5
proxy:
image: nginx
volumes:
- type: bind
source: ./proxy/nginx.conf
target: /etc/nginx/conf.d/default.conf
read_only: true
ports:
- 80:80
depends_on:
- backend
volumes:
db-data:
secrets:
db-password:
file: db/password.txt

View File

@ -7,27 +7,34 @@ Project structure:
├── backend ├── backend
│   ├── Dockerfile │   ├── Dockerfile
│   ├── go.mod │   ├── go.mod
│   ├── go.sum
│   └── main.go │   └── main.go
├── db ├── db
│   └── password.txt │   └── password.txt
├── compose.yaml ├── compose.yaml
├── proxy ├── proxy
│   ├── conf │   └── nginx.conf
│   └── Dockerfile
└── README.md └── README.md
``` ```
[_compose.yaml_](compose.yaml) [_compose.yaml_](compose.yaml)
``` ```shell
services: services:
backend: backend:
build: backend build:
context: backend
target: builder
... ...
db: db:
image: postgres image: postgres
... ...
proxy: proxy:
build: proxy image: nginx
volumes:
- type: bind
source: ./proxy/nginx.conf
target: /etc/nginx/conf.d/default.conf
read_only: true
ports: ports:
- 80:80 - 80:80
... ...
@ -38,7 +45,7 @@ Make sure port 80 on the host is not already being in use.
## Deploy with docker compose ## Deploy with docker compose
``` ```shell
$ docker compose up -d $ docker compose up -d
Creating network "nginx-golang-postgres_default" with the default driver Creating network "nginx-golang-postgres_default" with the default driver
Pulling db (postgres:)... Pulling db (postgres:)...
@ -55,21 +62,32 @@ Creating nginx-golang-postgres_proxy_1 ... done
## Expected result ## Expected result
Listing containers must show three containers running and the port mapping as below: Listing containers must show three containers running and the port mapping as below:
``` ```shell
$ docker ps $ docker compose ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES NAME COMMAND SERVICE STATUS PORTS
5e3ecd0289c0 nginx-golang-postgres_proxy "nginx -g 'daemon of…" 48 seconds ago Up 48 seconds 0.0.0.0:80->80/tcp nginx-golang-postgres_proxy_1 nginx-golang-postgres-backend-1 "/code/bin/backend" backend running
ffa1410b1c8a nginx-golang-postgres_backend "/server" 49 seconds ago Up 48 seconds 8000/tcp nginx-golang-postgres_backend_1 nginx-golang-postgres-db-1 "docker-entrypoint.s…" db running (healthy) 5432/tcp
e63be7db7cbc postgres "docker-entrypoint.s…" 49 seconds ago Up 49 seconds 5432/tcp nginx-golang-postgres_db_1 nginx-golang-postgres-proxy-1 "/docker-entrypoint.…" proxy running 0.0.0.0:80->80/tcp
``` ```
After the application starts, navigate to `http://localhost:80` in your web browser or run: After the application starts, navigate to `http://localhost:80` in your web browser or run:
``` ```shell
$ curl localhost:80 $ curl localhost:80
["Blog post #0","Blog post #1","Blog post #2","Blog post #3","Blog post #4"] ["Blog post #0","Blog post #1","Blog post #2","Blog post #3","Blog post #4"]
``` ```
Stop and remove the containers Stop and remove the containers
``` ```shell
$ docker compose down $ docker compose down
``` ```
## Use with Docker Development Environments
You can use this sample with the Dev Environments feature of Docker Desktop.
![Screenshot of creating a Dev Environment in Docker Desktop](../dev-envs.png)
To develop directly on the services inside containers, use the HTTPS Git url of the sample:
```
https://github.com/docker/awesome-compose/tree/master/nginx-golang-postgres
```

View File

@ -1,10 +1,41 @@
FROM golang:1.13-alpine AS build # syntax=docker/dockerfile:1.4
WORKDIR /go/src/github.com/org/repo FROM --platform=$BUILDPLATFORM golang:1.18-alpine AS builder
WORKDIR /code
ENV CGO_ENABLED 0
ENV GOPATH /go
ENV GOCACHE /go-build
COPY go.mod go.sum ./
RUN --mount=type=cache,target=/go/pkg/mod/cache \
go mod download
COPY . . COPY . .
RUN go build -o server . RUN --mount=type=cache,target=/go/pkg/mod/cache \
--mount=type=cache,target=/go-build \
go build -o bin/backend main.go
FROM alpine:3.12 CMD ["/code/bin/backend"]
EXPOSE 8000
COPY --from=build /go/src/github.com/org/repo/server /server FROM builder as dev-envs
CMD ["/server"]
RUN <<EOF
apk update
apk add git
EOF
RUN <<EOF
addgroup -S docker
adduser -S --shell /bin/bash --ingroup docker vscode
EOF
# install Docker tools (cli, buildx, compose)
COPY --from=gloursdocker/docker / /
CMD ["go", "run", "main.go"]
FROM scratch
COPY --from=builder /code/bin/backend /usr/local/bin/backend
CMD ["/usr/local/bin/backend"]

View File

@ -1,10 +1,11 @@
module github.com/org/repo module github.com/docker/awesome-compose/nginx-golang-postgres/backend
go 1.13 go 1.18
require ( require (
github.com/gorilla/context v1.1.1
github.com/gorilla/handlers v1.3.0 github.com/gorilla/handlers v1.3.0
github.com/gorilla/mux v1.6.2 github.com/gorilla/mux v1.6.2
github.com/lib/pq v1.10.3 github.com/lib/pq v1.10.3
) )
require github.com/gorilla/context v1.1.1 // indirect

View File

@ -0,0 +1,8 @@
github.com/gorilla/context v1.1.1 h1:AWwleXJkX/nhcU9bZSnZoi3h/qGYqQAGhq6zZe/aQW8=
github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
github.com/gorilla/handlers v1.3.0 h1:tsg9qP3mjt1h4Roxp+M1paRjrVBfPSOpBuVclh6YluI=
github.com/gorilla/handlers v1.3.0/go.mod h1:Qkdc/uu4tH4g6mTK6auzZ766c4CA0Ng8+o/OAirnOIQ=
github.com/gorilla/mux v1.6.2 h1:Pgr17XVTNXAk3q/r4CpKzC5xBM/qW1uVLV+IhRZpIIk=
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/lib/pq v1.10.3 h1:v9QZf2Sn6AmjXtQeFpdoq/eaNtYP6IN+7lcrygsIAtg=
github.com/lib/pq v1.10.3/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=

View File

@ -1,13 +1,18 @@
services: services:
backend: backend:
build: backend build:
context: backend
target: builder
secrets: secrets:
- db-password - db-password
depends_on: depends_on:
- db db:
condition: service_healthy
db: db:
image: postgres image: postgres
restart: always restart: always
user: postgres
secrets: secrets:
- db-password - db-password
volumes: volumes:
@ -17,15 +22,27 @@ services:
- POSTGRES_PASSWORD_FILE=/run/secrets/db-password - POSTGRES_PASSWORD_FILE=/run/secrets/db-password
expose: expose:
- 5432 - 5432
healthcheck:
test: [ "CMD", "pg_isready" ]
interval: 10s
timeout: 5s
retries: 5
proxy: proxy:
build: proxy image: nginx
volumes:
- type: bind
source: ./proxy/nginx.conf
target: /etc/nginx/conf.d/default.conf
read_only: true
ports: ports:
- 80:80 - 80:80
depends_on: depends_on:
- backend - backend
volumes: volumes:
db-data: db-data:
secrets: secrets:
db-password: db-password:
file: db/password.txt file: db/password.txt

View File

@ -1,2 +0,0 @@
FROM nginx:1.13-alpine
COPY conf /etc/nginx/conf.d/default.conf

View File

@ -3,6 +3,7 @@ server {
server_name localhost; server_name localhost;
location / { location / {
proxy_pass http://backend:8000; proxy_pass http://backend:8000;
proxy_http_version 1.1;
} }
} }