Dockerfile for Go + PostGreSQL
This commit is contained in:
parent
b0fce68955
commit
0226a02f69
3 changed files with 105 additions and 10 deletions
26
ch11/Docker/Dockerfile
Normal file
26
ch11/Docker/Dockerfile
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
FROM ubuntu:18.04
|
||||
|
||||
RUN apt-get update && apt-get install -y gnupg
|
||||
RUN apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8
|
||||
RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main" > /etc/apt/sources.list.d/pgdg.list
|
||||
RUN apt-get update && apt-get install -y software-properties-common postgresql-9.3 postgresql-client-9.3 postgresql-contrib-9.3
|
||||
RUN apt-get update && apt-get install -y git golang vim
|
||||
|
||||
USER postgres
|
||||
RUN /etc/init.d/postgresql start &&\
|
||||
psql --command "CREATE USER docker WITH SUPERUSER PASSWORD 'docker';" &&\
|
||||
createdb -O docker docker
|
||||
|
||||
RUN echo "host all all 0.0.0.0/0 md5" >> /etc/postgresql/9.3/main/pg_hba.conf
|
||||
RUN echo "listen_addresses='*'" >> /etc/postgresql/9.3/main/postgresql.conf
|
||||
|
||||
USER root
|
||||
RUN mkdir files
|
||||
COPY webServer.go files
|
||||
WORKDIR files
|
||||
RUN go get github.com/lib/pq
|
||||
RUN go build webServer.go
|
||||
RUN ls -l
|
||||
|
||||
USER postgres
|
||||
CMD ["/usr/lib/postgresql/9.3/bin/postgres", "-D", "/var/lib/postgresql/9.3/main", "-c", "config_file=/etc/postgresql/9.3/main/postgresql.conf"]
|
||||
79
ch11/Docker/webServer.go
Normal file
79
ch11/Docker/webServer.go
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
_ "github.com/lib/pq"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
func myHandler(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintf(w, "Serving: %s\n", r.URL.Path)
|
||||
fmt.Printf("Served: %s\n", r.Host)
|
||||
}
|
||||
|
||||
func timeHandler(w http.ResponseWriter, r *http.Request) {
|
||||
t := time.Now().Format(time.RFC1123)
|
||||
Body := "The current time is:"
|
||||
fmt.Fprintf(w, "<h1 align=\"center\">%s</h1>", Body)
|
||||
fmt.Fprintf(w, "<h2 align=\"center\">%s</h2>\n", t)
|
||||
fmt.Fprintf(w, "Serving: %s\n", r.URL.Path)
|
||||
fmt.Printf("Served time for: %s\n", r.Host)
|
||||
}
|
||||
|
||||
func getData(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Printf("Serving: %s\n", r.URL.Path)
|
||||
fmt.Printf("Served: %s\n", r.Host)
|
||||
|
||||
connStr := "user=postgres dbname=s2 sslmode=disable"
|
||||
db, err := sql.Open("postgres", connStr)
|
||||
if err != nil {
|
||||
fmt.Fprintf(w, "<h1 align=\"center\">%s</h1>", err)
|
||||
return
|
||||
}
|
||||
|
||||
rows, err := db.Query("SELECT * FROM users")
|
||||
if err != nil {
|
||||
fmt.Fprintf(w, "<h3 align=\"center\">%s</h3>\n", err)
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var id int
|
||||
var firstName string
|
||||
var lastName string
|
||||
err = rows.Scan(&id, &firstName, &lastName)
|
||||
if err != nil {
|
||||
fmt.Fprintf(w, "<h1 align=\"center\">%s</h1>\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(w, "<h3 align=\"center\">%d, %s, %s</h3>\n", id, firstName, lastName)
|
||||
}
|
||||
|
||||
err = rows.Err()
|
||||
if err != nil {
|
||||
fmt.Fprintf(w, "<h1 align=\"center\">%s</h1>", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
PORT := ":8001"
|
||||
arguments := os.Args
|
||||
if len(arguments) != 1 {
|
||||
PORT = ":" + arguments[1]
|
||||
}
|
||||
fmt.Println("Using port number: ", PORT)
|
||||
http.HandleFunc("/time", timeHandler)
|
||||
http.HandleFunc("/getdata", getData)
|
||||
http.HandleFunc("/", myHandler)
|
||||
|
||||
err := http.ListenAndServe(PORT, nil)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
@ -1,10 +0,0 @@
|
|||
FROM golang:alpine
|
||||
|
||||
RUN mkdir /files
|
||||
COPY hw.go /files
|
||||
WORKDIR /files
|
||||
|
||||
RUN go get github.com/lib/pq
|
||||
RUN go build -o /files/hw hw.go
|
||||
|
||||
ENTRYPOINT ["/bin/bash"]
|
||||
Loading…
Add table
Add a link
Reference in a new issue