Docker based dev environment for developing web applications in Golang
Write your golang programs in any directory. Take your golang development environment with you to anywhere.
In the earlier post, we built a docker container for developing Golang applications. It could be used for command line applications.
What if you want to build web applications with Golang? Well, that is the topic for this post.
Reviewing dockerfile for golang development
Let us relook at the
Dockerfile we built in the last post.
FROM golang:1.10.2-alpine3.7 RUN apk update; apk upgrade RUN apk add git RUN go get -u github.com/golang/dep/cmd/dep WORKDIR /go/src/app VOLUME ["/go/src/app"]
We based our docker container on alpine since it has a smaller footprint and it is also secure.
dep as a dependency management tool and exposed
/go/src/app directory to the host.
Adding web framework to docker image
Let us add a web framework to this docker container.
Beego is a web-application framework for Golang. Beego comes with all the features that we need to develop enterprise level web application.
If you want a quick overview of Beego, please read Introduction to Beego
Adding Beego is just two more lines to the above
Dockerfile. First We have to add
beego itself. Secondly, expose the port on which Beego runs, which is 8080.
Dockerfile looks like this now:
FROM golang:1.10.2-alpine3.7 RUN apk update; apk upgrade RUN apk add git RUN go get -u github.com/golang/dep/cmd/dep RUN go get -u github.com/beego/bee WORKDIR /go/src/app VOLUME ["/go/src/app"] EXPOSE 8080
You can get all of my dockerfiles from my gitlab repository.
Building the docker image
You can build the image using the command
docker build -t jjude/beego .. You can tag it to a name other than
jjude/beego. This will download the necessary components and build a local image on your desktop or laptop.
Using Docker Container
Now that you have the docker image, go to the folder where you want to code , say
~/code. Issue this command:
docker run -v "$(pwd)":/go/src/app -p 8080:8080 -it jjude/beego /bin/sh
This will create a golang container with
beego installed. It will display a shell like this:
You can test it out by issuing
bee on the prompt (
bee is the command line helper tool for beego). It will list out something like this:
Bee is a Fast and Flexible tool for managing your Beego Web Application. USAGE bee command [arguments] AVAILABLE COMMANDS version Prints the current Bee version migrate Runs database migrations api Creates a Beego API application bale Transforms non-Go files to Go source files fix Fixes your application by making it compatible with newer versions of Beego dlv Start a debugging session using Delve dockerize Generates a Dockerfile for your Beego application generate Source code generator hprose Creates an RPC application based on Hprose and Beego frameworks new Creates a Beego application pack Compresses a Beego application into a single file rs Run customized scripts run Run the application by starting a local development server server serving static content over HTTP on port Use bee help [command] for more information about a command. ADDITIONAL HELP TOPICS Use bee help [topic] for more information about that topic.
Creating a new beego application
Let us now create a new web application. Within the container, where it showed the
/go/src/app # prompt, issue
bee new testapp. You will see this:
2018/06/06 01:51:15 INFO ▶ 0001 Creating application... create /go/src/app/testapp/ create /go/src/app/testapp/conf/ create /go/src/app/testapp/controllers/ create /go/src/app/testapp/models/ create /go/src/app/testapp/routers/ create /go/src/app/testapp/tests/ create /go/src/app/testapp/static/ create /go/src/app/testapp/static/js/ create /go/src/app/testapp/static/css/ create /go/src/app/testapp/static/img/ create /go/src/app/testapp/views/ create /go/src/app/testapp/conf/app.conf create /go/src/app/testapp/controllers/default.go create /go/src/app/testapp/views/index.tpl create /go/src/app/testapp/routers/router.go create /go/src/app/testapp/tests/default_test.go create /go/src/app/testapp/main.go 2018/06/06 01:51:15 SUCCESS ▶ 0002 New application successfully created!
It has created a scaffolding app. Let us run it. To run it, we need to install
beego itself. So go into the
testapp folder and issue
dep init. This will create 3 files:
├── Gopkg.lock ├── Gopkg.toml └── vendor 1 directory, 2 files
This will already install all the required libraries like
beego. You are now ready to start the web application.
bee run. It will start the beego application. You can open your browser to
https://localhost:8080 to access the application. When you open it, you will see this page.
You can use this container for web development in Golang.
Under: #code , #golang , #devops