The complete code for this section: GitHub

This article is part 6 of a series on building chat applications using ReactJS and Go. You can find part 5 – Optimizing the front end here

In this section, we will focus on adding Docker to back-end applications.

Why do you do that? Until we explore issues such as authentication, load balancing, and deployment, it is standard practice to deploy applications using container technology.

Why Docker

If this is the first time you’ve heard of Docker containerization, you might question the reason for using it.

For me, one of the main reasons is that it makes deployment much easier. You can deploy Docker-based applications to any docker-enabled server or platform.

This means that no matter where you deploy, you can start the application with a simple command.

Not only that, it solves the “fine on my machine” problem, because in your Dockerfile, you can specify the specific environment your application needs to start.

start

First we have to install Docker on our computer. Check out the Docker guide

Once docker is installed and running, we can create a Dockerfile:

FROM golang:1.11.1-alpine3.8 RUN mkdir /app ADD./ app/ WORKDIR /app RUN go mod download RUN go build -o main./... CMD ["/app/main"]Copy the code

Docker cli can be used to build docker images:

Note – if your network speed is poor, the next command may take a while to execute, but subsequent commands will be much faster due to caching.

$ docker build -t backend .Sending Build Context to Docker Daemon 11.26kB Step 1/8: FROM Golang :1.11.1- AlPINE3.8 ---> 95ec94706ff6
Step 2/8 : RUN apk add bash ca-certificates git gcc g++ libc-dev
 ---> Running in 763630b369ca.Copy the code

After successfully completing the Build step, we can start the container:

$ docker run -it -p 8080:8080 backend
Distributed Chat App v0.01
WebSocket Endpoint Hit
Size of Connection Pool:  1
&{ 0xc000124000 0xc0000902a0 {0 0}}
Message Received: {Type:1 Body:test}
Sending message to all clients in Pool
Copy the code

As you can see, after running this command and refreshing the client, you can see that you are now connected to the Docker-like application service and that the terminal is printing logs.

If you want to deploy this application to AWS now, this will greatly simplify the process. We can now deploy and run our container using some commands from AWS’s ECS service.

Similarly, if we want to use Google Cloud, we can deploy it into Google’s container products with no extra work! This is just one of the great benefits of highlighting Docker-ification.

Why doesn’t the front end use Docker

At this point, you might be wondering why not do the same for Frontend/applications? The reason is that we plan to deploy the front-end application to AWS S3 services.

When the deployment comes online, the front end doesn’t need any fancy services, we just need to be able to reliably provide the build’s front end files.

conclusion

Therefore, in this section, we try to add Docker to the back-end application for the benefit of people who continue to develop and deploy.


Original text: tutorialedge.net/projects/ch…

Author: Elliot Forbes

This article is originally compiled by GCTT and published by Go Chinese