mirror of
https://github.com/docker/awesome-compose.git
synced 2025-04-19 07:18:06 +02:00
Move all samples to the root dir
Signed-off-by: Anca Iordache <anca.iordache@docker.com>
This commit is contained in:
parent
0c6fcde001
commit
f1e4cca535
262 changed files with 0 additions and 0 deletions
75
nginx-golang-postgres/README.md
Normal file
75
nginx-golang-postgres/README.md
Normal file
|
@ -0,0 +1,75 @@
|
|||
## Compose sample application
|
||||
### Go server with an Nginx proxy and a Postgres database
|
||||
|
||||
Project structure:
|
||||
```
|
||||
.
|
||||
├── backend
|
||||
│ ├── Dockerfile
|
||||
│ ├── go.mod
|
||||
│ └── main.go
|
||||
├── db
|
||||
│ └── password.txt
|
||||
├── docker-compose.yaml
|
||||
├── proxy
|
||||
│ ├── conf
|
||||
│ └── Dockerfile
|
||||
└── README.md
|
||||
```
|
||||
|
||||
[_docker-compose.yaml_](docker-compose.yaml)
|
||||
```
|
||||
services:
|
||||
backend:
|
||||
build: backend
|
||||
...
|
||||
db:
|
||||
image: postgres
|
||||
...
|
||||
proxy:
|
||||
build: proxy
|
||||
ports:
|
||||
- 80:80
|
||||
...
|
||||
```
|
||||
The compose file defines an application with three services `proxy`, `backend` and `db`.
|
||||
When deploying the application, docker-compose maps port 80 of the proxy service container to port 80 of the host as specified in the file.
|
||||
Make sure port 80 on the host is not already being in use.
|
||||
|
||||
## Deploy with docker-compose
|
||||
|
||||
```
|
||||
$ docker-compose up -d
|
||||
Creating network "nginx-golang-postgres_default" with the default driver
|
||||
Pulling db (postgres:)...
|
||||
latest: Pulling from library/postgres
|
||||
...
|
||||
Successfully built 5f7c899f9b49
|
||||
Successfully tagged nginx-golang-postgres_proxy:latest
|
||||
WARNING: Image for service proxy was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
|
||||
Creating nginx-golang-postgres_db_1 ... done
|
||||
Creating nginx-golang-postgres_backend_1 ... done
|
||||
Creating nginx-golang-postgres_proxy_1 ... done
|
||||
```
|
||||
|
||||
## Expected result
|
||||
|
||||
Listing containers must show two containers running and the port mapping as below:
|
||||
```
|
||||
$ docker ps
|
||||
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
|
||||
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
|
||||
ffa1410b1c8a nginx-golang-postgres_backend "/server" 49 seconds ago Up 48 seconds 8000/tcp nginx-golang-postgres_backend_1
|
||||
e63be7db7cbc postgres "docker-entrypoint.s…" 49 seconds ago Up 49 seconds 5432/tcp nginx-golang-postgres_db_1
|
||||
```
|
||||
|
||||
After the application starts, navigate to `http://localhost:80` in your web browser or run:
|
||||
```
|
||||
$ curl localhost:80
|
||||
["Blog post #0","Blog post #1","Blog post #2","Blog post #3","Blog post #4"]
|
||||
```
|
||||
|
||||
Stop and remove the containers
|
||||
```
|
||||
$ docker-compose down
|
||||
```
|
10
nginx-golang-postgres/backend/Dockerfile
Executable file
10
nginx-golang-postgres/backend/Dockerfile
Executable file
|
@ -0,0 +1,10 @@
|
|||
FROM golang:1.13-alpine AS build
|
||||
WORKDIR /go/src/github.com/org/repo
|
||||
COPY . .
|
||||
|
||||
RUN go build -o server .
|
||||
|
||||
FROM alpine:3.7
|
||||
EXPOSE 8000
|
||||
COPY --from=build /go/src/github.com/org/repo/server /server
|
||||
CMD ["/server"]
|
10
nginx-golang-postgres/backend/go.mod
Normal file
10
nginx-golang-postgres/backend/go.mod
Normal file
|
@ -0,0 +1,10 @@
|
|||
module github.com/org/repo
|
||||
|
||||
go 1.13
|
||||
|
||||
require (
|
||||
github.com/gorilla/context v1.1.1
|
||||
github.com/gorilla/handlers v1.3.0
|
||||
github.com/gorilla/mux v1.6.2
|
||||
github.com/lib/pq v0.0.0-20180327071824-d34b9ff171c2
|
||||
)
|
88
nginx-golang-postgres/backend/main.go
Executable file
88
nginx-golang-postgres/backend/main.go
Executable file
|
@ -0,0 +1,88 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/handlers"
|
||||
"github.com/gorilla/mux"
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
func connect() (*sql.DB, error) {
|
||||
bin, err := ioutil.ReadFile("/run/secrets/db-password")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return sql.Open("postgres", fmt.Sprintf("postgres://postgres:%s@db:5432/example?sslmode=disable", string(bin)))
|
||||
}
|
||||
|
||||
func blogHandler(w http.ResponseWriter, r *http.Request) {
|
||||
db, err := connect()
|
||||
if err != nil {
|
||||
w.WriteHeader(500)
|
||||
return
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
rows, err := db.Query("SELECT title FROM blog")
|
||||
if err != nil {
|
||||
w.WriteHeader(500)
|
||||
return
|
||||
}
|
||||
var titles []string
|
||||
for rows.Next() {
|
||||
var title string
|
||||
err = rows.Scan(&title)
|
||||
titles = append(titles, title)
|
||||
}
|
||||
json.NewEncoder(w).Encode(titles)
|
||||
}
|
||||
|
||||
func main() {
|
||||
log.Print("Prepare db...")
|
||||
if err := prepare(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
log.Print("Listening 8000")
|
||||
r := mux.NewRouter()
|
||||
r.HandleFunc("/", blogHandler)
|
||||
log.Fatal(http.ListenAndServe(":8000", handlers.LoggingHandler(os.Stdout, r)))
|
||||
}
|
||||
|
||||
func prepare() error {
|
||||
db, err := connect()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
for i := 0; i < 60; i++ {
|
||||
if err := db.Ping(); err == nil {
|
||||
break
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
|
||||
if _, err := db.Exec("DROP TABLE IF EXISTS blog"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := db.Exec("CREATE TABLE IF NOT EXISTS blog (id SERIAL, title VARCHAR)"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for i := 0; i < 5; i++ {
|
||||
if _, err := db.Exec("INSERT INTO blog (title) VALUES ($1);", fmt.Sprintf("Blog post #%d", i)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
1
nginx-golang-postgres/db/password.txt
Normal file
1
nginx-golang-postgres/db/password.txt
Normal file
|
@ -0,0 +1 @@
|
|||
db-frknz
|
29
nginx-golang-postgres/docker-compose.yaml
Normal file
29
nginx-golang-postgres/docker-compose.yaml
Normal file
|
@ -0,0 +1,29 @@
|
|||
version: "3.7"
|
||||
services:
|
||||
backend:
|
||||
build: backend
|
||||
secrets:
|
||||
- db-password
|
||||
depends_on:
|
||||
- db
|
||||
db:
|
||||
environment:
|
||||
POSTGRES_DB: example
|
||||
POSTGRES_PASSWORD_FILE: /run/secrets/db-password
|
||||
image: postgres
|
||||
restart: always
|
||||
secrets:
|
||||
- db-password
|
||||
volumes:
|
||||
- db-data:/var/lib/postgresql/data
|
||||
proxy:
|
||||
build: proxy
|
||||
ports:
|
||||
- 80:80
|
||||
depends_on:
|
||||
- backend
|
||||
volumes:
|
||||
db-data: {}
|
||||
secrets:
|
||||
db-password:
|
||||
file: db/password.txt
|
2
nginx-golang-postgres/proxy/Dockerfile
Executable file
2
nginx-golang-postgres/proxy/Dockerfile
Executable file
|
@ -0,0 +1,2 @@
|
|||
FROM nginx:1.13-alpine
|
||||
COPY conf /etc/nginx/conf.d/default.conf
|
8
nginx-golang-postgres/proxy/conf
Executable file
8
nginx-golang-postgres/proxy/conf
Executable file
|
@ -0,0 +1,8 @@
|
|||
server {
|
||||
listen 80;
|
||||
server_name localhost;
|
||||
location / {
|
||||
proxy_pass http://backend:8000;
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue