Golang Development _golang based on Docker

Source: Internet
Author: User
Tags docker run
Based on the Docker of Golang development author: Chszs, without the owner's permission may not be reproduced. The permitted reprint should be annotated author and blog homepage: Http://blog.csdn.net/chszs, Docker for development

Docker is not only for deployment, it can also be used for development. 1. Why to use Docker in development

There are several main reasons.
1) A consistent development environment
With Docker, you can ensure that the entire development team uses a consistent development environment.
2 The development environment and the final production environment to maintain the same
This reduces the likelihood of a deployment error.
3 simplifies compilation and build complexity
For several hours of compilation and construction work, you can use Docker to simplify.
4 in development, only Docker
There is no need to build a variety of programming locales on your own development host.
5 can use multiple versions of the same programming language
You can use multiple versions of the same programming language, such as Python, Python, Ruby, Ruby, Java, node, and so on, without having to solve multiple versioning conflicts.
6) Deployment is simple
The application runs in a container and is the same as deployed in a production environment. Simply package your code and deploy it to a server with the same image, or create a new Docker mirror with the original image and run the new mirror 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 placed in the working directory
2 Modify the build and run commands so that they can be run in the Docker container

For Web applications:
If you are developing a Web application that requires an open port, just use the-p parameter to let the Docker run with open ports.

For the Java language:
Put all the required dependent jar packages into the working directory, and then compile the program in the container and run it.
Suppose an example program is Hello.java, it uses Gson-2.2.4.jar dependencies and Json-java.jar dependencies.
So 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 by 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 Dependency

Go to 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 '

Note that you want to mount the local gopath to the container.

3) Running 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 is run, you can perform:

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

Home page of the DJ project see: HTTPS://GITHUB.COM/TREEDER/DJ
DJ, Docker jockey, is a Docker mirror that helps developers simplify the process of building a development environment by simply installing Docker, without having to install the runtime or environment configuration of the programming language.

There are several advantages to using the DJ tool:
1 no need to install Golang
2 No need to configure Gopath environment variables
3 do not 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 dependencies do not need to be imported again
5) support for cross compilation
6) support for static compilation
7) build into Docker mirrors
8) Support for remote Git repository construction

DJ's four features:
1) Vendor
All dependencies are put into the/vendor directory
2) Build
Building Applications using Vendor dependencies
3) Run
Run your application
4) Image
Creating Docker mirrors for your application

DJ supports programming languages such as Golang, Ruby, Node.js, PHP, and Python.

# Vendor relies on
DJ Lang Vendor
# test
DJ Lang Run [SCRIPT.ABC]
# Create Docker Mirror
DJ Lang image Username/myapp:lat EST
# Test Docker Mirror
Docker run--rm-p 8080:8080 Username/myapp [SCRIPT.ABC]
# push Mirror to Dockerhub
Docker push Username/myapp
# Check the programming language version of
DJ LANG

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

Dj-p "8080:8080" Ruby run HELLO.RB

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

Dj-e "Myenvvar=yo" Ruby run HELLO.RB

Specifying the Golang programming language

# Build not run DJ Go builds
# FMT:
DJ Go FMT
# Create a static binary package DJ go
static
# cross-platform compiler DJ go
cross
  
   # from the remote warehouse to build the
DJ go remote Http://github.com/org/project

  

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.