Golang Development based on Docker

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

Order

Many times we use Docker for deployment, but it can also be used for development.

Why use Docker in development?

There are several main reasons for this:

    1. a consistent development environment uses Docker to ensure that the entire development team uses a consistent development environment.
    2. The development Environment is aligned with the final production environment This reduces the likelihood of deployment errors.
    3. simplifies compilation and build complexity for a few hours of compiling and building work, you can use Docker to simplify.
    4. in development, Docker does not need to build a variety of programming locales on its own development host.
    5. Multiple versions of the same programming language can be used in multiple versions of the same programming language (such as Python, Python, Ruby, Ruby, Java, node), without the need to resolve the issue of multiple versioning conflicts.
    6. deploying a very simple application runs in a container and is the same as deploying it in a 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. using your favorite development IDE , you can still continue to use your favorite development IDE without running the VirtualBox virtual machine or SSH.

Install Docker and verify

➜ tonny@tonny-pc  ~ sudo dpkg --get-selections |grep linux➜ tonny@tonny-pc  ~ cat /etc/issue➜ tonny@tonny-pc  ~ dpkg -l | grep aufs➜ tonny@tonny-pc  ~ sudo apt-get install -y aufs-tools cgroupfs-mount mountall➜ tonny@tonny-pc  ~ sudo curl -sSL https://get.docker.com/ | sh➜ tonny@tonny-pc  ~ sudo usermod -aG docker $(whoami)➜ tonny@tonny-pc  ~  systemctl restart docker➜ tonny@tonny-pc  ~  ps aux |grep `cat /var/run/docker.pid`➜ tonny@tonny-pc  ~  sudo docker versionClient: Version:      1.12.0 API version:  1.24 Go version:   go1.6.3 Git commit:   8eab29e Built:        Thu Jul 28 21:40:59 2016 OS/Arch:      linux/amd64Server: Version:      1.12.0 API version:  1.24 Go version:   go1.6.3 Git commit:   8eab29e Built:        Thu Jul 28 21:40:59 2016 OS/Arch:      linux/amd64

How do I use Docker in development?

There are two differences between the use of Docker development and general development:

    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'

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 WebSocket. Then the process is as follows:

    1. Installation dependencies
      ➜ tonny@tonny-pc  ~  github.com/gorilla/websocket
    2. Building Applications
      ➜ tonny@tonny-pc  ~  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. Running the application
      ➜ tonny@tonny-pc  ~  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

DJ Tools

For the 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 build the Golang directory structure
    4. Dependencies do not need to be re-imported
    5. Support Cross-compilation
    6. Support for static compilation
    7. Build into a Docker image
    8. Support Remote Git repository build

DJ's four features:

    1. Vendor all dependencies into the/vendor directory
    2. Build builds applications with vendor dependencies
    3. Run your application
    4. Image to create a Docker image for the application
      DJ supports Golang, Ruby, node. js, PHP, Python, and other programming languages.

      # Vendor Dependent
      DJ LANG Vendor
      # test
      DJ LANG Run [SCRIPT.ABC]
      # Create a Docker image
      DJ LANG Image Username/myapp:latest
      # test Docker image
      Docker run--rm-p 8080:8080 Username/myapp [SCRIPT.ABC]
      # Push the Mirror to the Dockerhub
      Docker Push Username/myapp
      # Check the version of the programming language
      DJ LANG Version

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.