Recently, I made a light full stack project, using Golang to write the server API interface and using Vue2.6 to realize the front-end UI. Finally, Docker was used to build and deploy.

The whole development process is still very smooth, did not encounter too much trouble. When finally using Docker to build up to run, really found Docker this thing is really convenient ah!

Without further ado, I will introduce the whole idea of this small project.

The project structure

This is a project where the front and back ends are separated, but the code is put together.

|-- src/
|-- www/
|-- main.go
|-- go.mod
Copy the code

The project structure is probably like this, SRC put Golang source code, WWW put the front end project.

Real-time development

The development experience is very important to us, and we have hot updates provided by VUe-CLI for the front-end project. Golang we use Air for file listening and restart. Also configure.air. Toml to ignore front-end file changes.

Dockerfile

When the development work is complete, we need to package the project and deploy it to the online environment. Of course, we can clone the project directly to the online server, and then build the Golang environment, Nodejs environment, and then build the Golang application, build the front-end project…

If there is only one environment, the workload is ok. If there’s more than one…

So we considered using Docker to help us build the project. The first thing to do is write a Dockerfile.

Base image

Golang’s base image, Golang :1.15, was found on DockerHub. It depends on your project.

FROM golang:1.15
Copy the code

Nodejs and NPM

The base image only has the Golang environment installed, and the NodeJS and NPM base image required for our front-end project are not provided. We need to install it ourselves.

The introduction on DockerHub shows that Golang :1.15 mirror is based on debian-Buster. We found the following commands according to the Nodejs installation guide:

curl -sL https://deb.nodesource.com/setup_15.x | bash -
apt-get install -y nodejs
Copy the code

Build the Golang program

Use the official mirror recommendation method directly.

WORKDIR /go/src/app
COPY.

RUN go get -d -v ./...
RUN go build -o /usr/local/bin
Copy the code

Build a front-end project

WORKDIR /go/src/app/www
RUN npm install
RUN npm run build
# static resource directory specified in our Go program
RUN mv ./dist/* /var/www/html
Copy the code

The last

ENTRYPOINT [ "/usr/local/bin/easy-etcd" ]
Copy the code

An executable generated after go Build. The name of the executable comes from the package name at compile time. The easy-ETCD here is the package name for my Golang project.

Build and deploy

Finally, the basic image build and run.

build

Docker build-t easy-etCD :1.0.Copy the code

run

Docker run-ITD -P 9000:9000 Easy-ETCD :1.0Copy the code

One click. But the network is poor in the case of installing both projects depends on, you know.

Attach the complete Dockerfile

# Debian 10
FROM golang:1.15

RUN mkdir -p /var/www/html

# nodejs installation
RUN apt-get -yqq update
RUN curl -sL https://deb.nodesource.com/setup_15.x | bash -
RUN apt-get install -y nodejs

# Build the Go program
WORKDIR /go/src/app
COPY.

RUN go get -d -v ./...
RUN go build -o /usr/local/bin


Build a static UI file
WORKDIR /go/src/app/www
RUN npm install
RUN npm run build
The static resource directory specified in the # Go program
RUN mv ./dist/* /var/www/html

ENTRYPOINT [ "/usr/local/bin/easy-etcd" ]
Copy the code

The follow-up to optimize

The images built in Debian are a bit too big. I then adopted the alpine version of the base image Golang: 1.15-Alpine. To install nodeJS, run the apk command.

Then add golang and NPM’s Chinese agents. The final final Dockerfile is shown below

# Debian 10
FROM golang:1.15-alpine


RUN mkdir -p /var/www/html

# nodejs installation
RUN apk add --update nodejs npm

# Taobao Mirror
RUN npm install -g cnpm --registry=https://registry.npm.taobao.org

# Build the Go program
WORKDIR /go/src/app
COPY.

RUN go env -w GOPROXY=https://goproxy.cn,direct
RUN go get -d -v ./...
RUN go build -o /usr/local/bin


Build a static UI file
WORKDIR /go/src/app/www
RUN cnpm install
RUN npm run build
The static resource directory specified in the # Go program
RUN mv ./dist/* /var/www/html

ENTRYPOINT [ "/usr/local/bin/easy-etcd" ]

Copy the code

Dependency package installation speed straight up!! It’s also a lot smaller.

History of hits

  1. How to complete a Business page in 10 minutes – the art of Vue packaging
  2. Novice can also understand the virtual rolling method
  3. Axios source code analysis

Original text – My little station