Docker-based Golang development

Source: Internet
Author: User
Tags docker run
This is a creation in Article, where the information may have evolved or changed.

Docker-based Golang development

Author: Chszs, not allowed to be reproduced without the permission of Bo master. Permission to reprint should be marked by the author and blog home: Http://blog.csdn.net/chszs

First, Docker for development

Docker is not only used for deployment, it can also be used for development.

1. Why use Docker in development

There are several main reasons for this.
1) A consistent development environment
With Docker, you can ensure that the entire development team uses a consistent development environment.
2) The development environment is consistent with the final production environment
This reduces the likelihood of deployment errors.
3) simplifies compilation and build complexity
For a few hours of compilation and build work, you can use Docker to simplify.
4) at the time of development, only Docker
There is no need to build a variety of programming locales on your own development host.
5) Multiple versions of the same programming language can be used
You can use multiple versions of the same programming language (Python, Python, Ruby, Ruby, Java, node), and so on, without having to resolve multiple version conflicts.
6) Deployment is simple
The application runs in the container, and the deployment is the same in the production environment. Just package your code and deploy it to a server with the same image, or create a new Docker image with the original image and run the new image directly.
7) Use your favorite development IDE
You can still continue to use your favorite development IDE without running the VirtualBox virtual machine or SSH.

How to use Docker in development

The difference is two points:
1) Ensure that all dependencies are put into the working directory
2) Modify the build and run commands so that they can be run in the Docker container

For Web Apps:
If you are developing a Web application that requires an open port, simply use the-p parameter to open the port to the Docker runtime.

For the Java language:
Put all the required dependent jar packages into the working directory, then compile the program in the container and run it.
Suppose an example program is Hello.java, which uses Gson-2.2.4.jar dependencies and Json-java.jar dependencies.
Then the entire build command is as follows:

docker run --rm -v "$(pwd)":/app -w /app java sh -c 'javac -cp "json-java.jar:gson-2.2.4.jar" Hello.java'

To run and test this program, you can perform:

docker run --rm -v "$(pwd)":/app -w /app java sh -c 'java -cp gson-2.2.4.jar:json-java.jar:. Hello'

It's a little different for Golang languages.
Copy the code developed by Golang to the location specified in Gopath, and the system must have the GO environment installed. Suppose the program is Hello.go and uses the Iron.io platform. Then the process is as follows:

1) Installation Dependent

go get github.com/iron-io/iron_go/worker

2) Build the application

docker run --rm -v "$GOPATH":/gopath -v "$(pwd)":/app -w /app google/golang sh -c 'go build -o hello'

Be careful to mount the local gopath to the container.

3) Run the application

docker run --rm -v "$(pwd)":/app -w /app google/golang sh -c './hello'

Note that to keep the container always available, such as the ROCKSDB instance is always available, so that the container does not delete the Rocksdb instance every time it runs, you can execute:

docker run --name goapp -v "$GOPATH":/gopath -v "$(pwd)":/app -w /app google/golang sh -c 'go build -o hello && ./hello' || docker start -ia goapp

Second, DJ Tools

DJ Project homepage See: HTTPS://GITHUB.COM/TREEDER/DJ
The DJ, Docker jockey, is a Docker image that helps developers streamline the development environment by installing Docker, eliminating the need to install programming language runtime or environment configuration.

There are several advantages to using the DJ tool:
1) No need to install Golang
2) No need to configure Gopath environment variables
3) No need to put the code in the specified directory (such as the/src/github.com/user/hello directory) or to establish a Golang directory structure
4) dependency without re-import
5) Support cross-compilation
6) support for static compilation
7) build into the Docker image
8) Support remote Git repository build

DJ's four features:
1) Vendor
All dependencies are placed in the/vendor directory
2) Build
Building Applications with vendor dependencies
3) Run
Run your application
4) Image
Create a docker image for your application

DJ supports Golang, Ruby, node. js, PHP, Python, and other programming languages.

# vendor依赖dj LANG vendor# 测试dj LANG run [script.abc]# 创建Docker镜像dj LANG image username/myapp:latest# 测试Docker镜像docker run --rm -p 8080:8080 username/myapp [script.abc]# 把镜像推送到DockerHubdocker push username/myapp# 检查编程语言的版本dj LANG version

Port mappings
You can also map ports from Docker containers to applications. Similar to Docker, use the-p parameter, such as this:

dj -p "8080:8080" ruby run hello.rb

Environment variable Configuration
Similar to Docker, use the-e parameter, such as this:

dj -e "MYENVVAR=YO" ruby run hello.rb

Specifying the Golang programming language

# 只构建不运行dj go build# fmt:dj go fmt# 建立静态的二进制包dj go static# 跨平台编译dj go cross# 从远程仓库构建dj go remote http://github.com/org/project
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.