Go is an open source programming language that makes it easy to build simple, reliable, and efficient software. This is an example of a Go webserver without any frameworks (bareboned). You can choose any framework of your liking 'or' boycott the framework all together.
Go is compiled program language, so it makes sense that we don't really need the entire compiler for production docker images. Hence this repo has two dockerfile, one for production (Dockerfile-prod) and the other for development (Dockerfile)
The default docker build command
docker build -t <image-name>:<version> .
assumes there is a Dockerfile available in the present working directory.
Note:
Unless specified otherwise, the docker build command takes Dockerfile for the build instruction.
# The below example builds a docker image with Dockerfile
# this image is preferred for development and for git-push
docker build -t go-server:1.0 .
If the production image has a different build instruction-set found in Dockerfile-prod, we can pass the file using -f
flag followed by the filename.
# The below shell file makes the binary which is need for the final production docker image.
./app/conf/gobuild.sh
# The below example builds a docker image with Dockerfile-prod
docker build -t go-server:1.0 -f Dockerfile-prod .
After building your image (production or development) you can test the image local by running
docker run -p <host-port>:<container-port> <image-name>:<version>
below are example of docker run
# Example for default run command (logs will be printed on same terminal)
docker run -p 8080:8080 go-server:1.0 .
# Example for run with a background process
docker run -d -p 8080:8080 go-server:1.0
# Example for a run with change in environment variable for port.
docker run -d -p 7000:7000 -e APP_PORT=7000 go-server:1.0
Follow the steps mentioned below
- Ensure that you have a git project
- Edit
app/src/Server.go
- Remember to gitignore the binary file (Server).
- Commit your changes
git add .
git commit -m "message"
- Push the changes to git
git push <remote> master
Default Port for application is 8080
.
Default Application port can be changed by modifying the variable port
in app/src/Server.go
or setting Environment Variable
// port is not present then set default.
if port == "" {
port = "<NEW DEFAULT PORT>"
}
APP_PORT
- Application port can also be specified by setting APP_PORT ENV
docker run -d -p 8080:<APP_PORT> -e APP_PORT='<NEW_PORT>' go-server:1.0