Skip to content

Commit

Permalink
Refactoring Docker configuration
Browse files Browse the repository at this point in the history
- upgrade to Go 1.22.2
- adding ffmpeg dependency (gulp)
- splitting demo and production configurations (production still needs testing)
- renaming config files (since the "development" container didn't work out so great)
- adding "createOwner" switch for localhost servers
- single thread all initialization (including createOwner step)
- removing entrypoint.sh because we're now populating starter configs, so the server doesn't have to wait for config.json anymore
  • Loading branch information
benpate committed Apr 27, 2024
1 parent 1da84a4 commit 57e9e71
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 120 deletions.
1 change: 1 addition & 0 deletions config/domain.go
Expand Up @@ -16,6 +16,7 @@ type Domain struct {
SMTPConnection SMTPConnection `json:"smtp" bson:"smtp"` // Information for connecting to an SMTP server to send email on behalf of the domain.
Owner Owner `json:"owner" bson:"owner"` // Information about the owner of this domain
KeyEncryptingKey string `json:"keyEncryptingKey" bson:"keyEncryptingKey"` // Key used to encrypt/decrypt JWT keys stored in the database
CreateOwner bool `json:"createOwner" bson:"createOwner"` // TRUE if the owner should be created when the domain is created
}

// NewDomain returns a fully initialized Domain object.
Expand Down
58 changes: 0 additions & 58 deletions docker-compose-demo.yml

This file was deleted.

19 changes: 19 additions & 0 deletions docker-compose-production.yml
@@ -0,0 +1,19 @@
# This docker-compose file starts up a Emissary server for
# production use. This file assumes:
# 1) you will configure Emissary using a mongodb database.
# 2) the mongo database exists outside of this Docker config.
# 3) you have configured ENVIRONMENT VARIABLES to point to it.
#
# After installing this server, you will need to run the
# setup tool from another location to configure the server's
# settings and domains

name: emissary_prod
services:
emissary:
build:
context: .
dockerfile: ./docker/prod-dockerfile
ports:
- "80:80"
- "443:443"
47 changes: 31 additions & 16 deletions docker-compose.yml
@@ -1,43 +1,58 @@
version: '3.8'
# This docker-compose file starts up a Emissary server where you
# can "kick the tires" and see how it works. You should probably
# not use this in production.
#
# It defaults to serving one domain "localhost" on port 8080.
name: emissary_demo

services:
emissary:

# This is the Emissary server that runs a single "localhost" domain on port 8080.
# It runs on http://localhost:8080
server:
container_name: emissary_demo_server
build:
context: .
dockerfile: ./docker/Dockerfile
dockerfile: ./docker/demo-dockerfile
ports:
- "8080:8080"
environment:
- EMISSARY_CONFIG=file:///data/config/config.json
- WAIT_FOR_CONFIG=1
depends_on:
- mongodb
- smtp
volumes:
- configdata:/data/config
- config:/data/config
- uploads:/data/uploads

# The setup tool creates/modifies the server configuration,
# although you probably won't have to use it much for this demo.
# It runs on http://localhost:8888
setup:
container_name: emissary_demo_setup
build:
context: .
dockerfile: ./docker/Dockerfile
dockerfile: ./docker/demo-dockerfile
ports:
- "8888:8888"
environment:
- EMISSARY_CONFIG=file:///data/config/config.json
command: ["--setup", "--port", "8888"]
depends_on:
- mongodb
- smtp
volumes:
- configdata:/data/config
- config:/data/config

# This mongo database stores data for the Emissary site.
# You can connect to it using any MongoDB client.
mongodb:
container_name: emissary_demo_database
image: "mongo:latest"
ports:
- "27017:27017"
volumes:
- mongodata:/data/db
smtp:
image: ghcr.io/literalgarage/smtp-logger:latest
ports:
- "8025:8025"
- mongo:/data/db

volumes:
mongodata:
configdata:
config:
mongo:
uploads:
2 changes: 1 addition & 1 deletion docker/demo-dockerfile
Expand Up @@ -27,7 +27,7 @@ WORKDIR /app

# Copy the Pre-built binary file from the previous stage
COPY --from=builder /app/emissary .
COPY --from=builder /app/docker/demo-config.json /data/config/config.json
COPY --from=builder /app/docker/demo.json /data/config/config.json

# Entrypoint script
ENTRYPOINT [ "/app/emissary" ]
3 changes: 2 additions & 1 deletion docker/demo-config.json → docker/demo.json
Expand Up @@ -55,5 +55,6 @@
"adminEmail": "",
"httpPort": 8080,
"httpsPort": 0,
"debugLevel": "Trace"
"debugLevel": "Debug",
"createOwner": true
}
21 changes: 0 additions & 21 deletions docker/entrypoint.sh

This file was deleted.

10 changes: 7 additions & 3 deletions docker/Dockerfile → docker/prod-dockerfile
@@ -1,4 +1,4 @@
FROM golang:1.21-bookworm as builder
FROM golang:1.22.2-bookworm as builder

# Set the Current Working Directory inside the container
WORKDIR /app
Expand All @@ -18,11 +18,15 @@ RUN go build -o emissary server.go
# Start a new stage from scratch
FROM debian:bookworm-slim

# Install ffmpeg
# This is required for audio/video transcoding
RUN apt-get update -y
RUN apt-get install -y ffmpeg

WORKDIR /app

# Copy the Pre-built binary file from the previous stage
COPY --from=builder /app/emissary .
COPY --from=builder /app/docker/entrypoint.sh .

# Entrypoint script
ENTRYPOINT [ "/app/entrypoint.sh" ]
ENTRYPOINT [ "/app/emissary" ]
37 changes: 17 additions & 20 deletions service/domain.go
Expand Up @@ -155,26 +155,23 @@ func (service *Domain) Save(domain model.Domain, note string) error {
service.domain = domain

// If this is the first time saving a local domain, create an initial admin user.
if firstSave && service.IsLocalhost() {

go func() {
log.Trace().Msg("Creating admin user for local host")

admin := model.NewUser()
admin.DisplayName = "Admin"
admin.Username = "admin"
admin.EmailAddress = "admin@localhost"
admin.SetPassword("admin")
admin.IsOwner = true
admin.IsPublic = true

if err := service.userService.Save(&admin, "Create admin user for local host"); err != nil {
derp.Report(derp.Wrap(err, "service.Domain.Save", "Error creating admin user for local host"))
return
}

log.Trace().Msg("Added admin user for local host")
}()
if firstSave && service.configuration.CreateOwner && service.IsLocalhost() {

log.Trace().Msg("Creating admin user for local host")

admin := model.NewUser()
admin.DisplayName = "Admin"
admin.Username = "admin"
admin.EmailAddress = "admin@localhost"
admin.SetPassword("admin")
admin.IsOwner = true
admin.IsPublic = true

if err := service.userService.Save(&admin, "Create admin user for local host"); err != nil {
return derp.Wrap(err, "service.Domain.Save", "Error creating admin user for local host")
}

log.Trace().Msg("Added admin user for local host")
}

return nil
Expand Down

0 comments on commit 57e9e71

Please sign in to comment.