Docker + Golang + Postgres
This article is an introductory tutorial, and the author describes how to deploy a Golang Web application using Postgres in the Docker. The author lists all the steps and points of interest in the article, recommending Docker beginners interested in Golang read.
Recently I have been studying Docker and decided to use it to deploy my Golang Negroni + Postgres web application. Taking into account the future of scalability and other people's best practices, I need to use two Docker containers: one for the application, the other for the database, and the two containers need to communicate. Since this is the first time I used Docker, so the whole deployment process also stepped on a lot of pit, in this article I would like to share with you my experience.
You can find the demo program in this tutorial at GitHub .
- In-depth understanding of Docker Volume (a)
- Getting Started with Docker Swarm (3) Swarm SOA
- Docker Getting Started Tutorial (3) Dockerfile
- 【Tutorial】 How to create a Docker container as small as possible
- Docker 17 recommendations for secure deployment
- Docker Getting Started Tutorial (7) Docker API
I default you have already installed the Docker, if not, please refer to the official document installation .
Step 1: Dockerize Postgres
First, download and run the official Docker image from the Docker Hub, name it "db", and set the password for the default "Postgres" user:
docker run --name db -e POSTGRES_PASSWORD=password123 -d postgres
-d parameter means that the Docker is run in a background process. Next, let's create the application-dependent Postgres user and the database, first open the "db" container's bash shell:
docker exec -it db /bin/bash
Login from this bash shell psql:
psql -U postgres
Next create the user and the database:
CREATE USER app;
CREATE DATABASE testapp;
GRANT ALL PRIVILEGES ON DATABASE testapp TO app;
Then exit psql (shortcut key: CTRL-D). Now that you have created the database, you need to edit the configuration file in this container, so you need to install a text editor (Vim! I'm looking you!).
apt-get install vim
Use Vim to edit Postgres's
pg_hba.conf file, which is used to manage client authentication.
We need to modify this file to ensure that the next created application container can use the newly created postgres user to access the database container. By default, only the "postgres" user has this permission. To find this configuration file you can use the following command:
psql -U postgres
Run in the Postgres interactive shell:
Copy the returned file path and exit the psql shell (shortcut key: CTRL-D). Now use Vim to edit the file:
To change the last line of a file and save the changes, type
Host all "app" 0.0.0.0/0 trust
Since this configuration file is only loaded when Postgres starts, we now need to restart the db container. Return to your host (shortcut: CTRL-D) and run:
docker stop db
docker start db
After the modified configuration has been in effect, the Postgres container is ready!
Step 2: Dockerize Golang application
For the Dockerize Go application, we must create the Dockerfile under the project folder.
If you do not want Dockerize you have already written the Golang app, you can download the Dockerize-tutorial Demo . In the root directory of the Golang project, create a Dockerfile:
In this file, we will add the following three lines:
RUN go get bitbucket.org/liamstask/goose/cmd/goose
– The first line runs the Gumang mirror onbuild version, which automatically copies the packet source and gets the dependency of the program, then builds the program and configures it to run at startup.
– install 'goose' on the second line, a migration tool that we will use in the application container.
– The last line opens port 4000.
Now we can build a Docker image for the application. In the project directory, run:
docker build -t app .
This command will eventually generate a Docker image named "app". Now, we can run a Docker container with this Docker mirror:
docker run -d -p 8080:4000 --name tutapp --link db:postgres app
The above order can be decomposed into:
-dto start the container hang in the background to run.
-p 8080:4000maps port 4000 (port required by this application) to port 8080 of the host.
--name tutappName the Docker container as "tutapp".
--link db:postgresLink the application container with the Postgres container named "db" that was previously created.
The link parameter ensures that our application container can access an environment variable named
$POSTGRES_PORT_5432_TCP_ADDR . This environment variable contains the host address when connecting to the Postgres DB. So, we have to make sure that the host variable in our
dbconf.yml file is:
open: host=$POSTGRES_PORT_5432_TCP_ADDR user=app dbname=testapp sslmode=disable
Replace the variable that demonstrates the application's
config.go file with the variable.
The last step is to migrate DB for our application, so run inside the tutapp container
goose up :
docker exec -it tutapp goose up
To access this program, visit http: // localhost: 8080 you should see this application running!
If the user's Docker daemon is running on another machine (or virtual machine), the user should change localhost to the host's address. If you are using boot2docker on OS X or Windows, you can use the command
boot2docker ip find the corresponding IP address
Now you have successfully run the Golang application and communicate with the Postgres database in another Docker container. If there is anything unclear or if you can not run, please leave a comment, I will be the first time perfect!
Source: How to Dockerize a Golang Webapp With Postgres DB (Translation: Tian Haohao )
Tian Haohao , University of Sydney USYD graduate, currently engaged in the development of Android in Zhuhai work. Spare time to focus on Docker's study and research, hoping to DockerOne to the latest and best translation of the contribution to everyone, with the reader to swim Docker's ocean.