Using Docker mirroring to build the GO language environment

Source: Internet
Author: User
Tags docker cp docker run

1. Installing Docker

The system environment I am currently using is CentOS7 , and the command to install Docker is the yum install docker*. As for other systems, you can go to Baidu to find its corresponding installation method.

2. Configure the address of the Docker image warehouse

For now, direct access to the Docker's DockerHub will be slow, but fortunately there are some DockerHub acceleration sites in the country. Use the USTC accelerator below.

Modify the file/etc/docker/daemon.json to change its contents to

{    "registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]}

After the configuration is complete, restart the Docker service: Systemctl Restart Docker

This makes it possible to use the https://docker.mirrors.ustc.edu.cn accelerator.

If the USTC accelerator is not fast enough, you can also try other accelerators, please refer to the Docker image Accelerator here

3. Download the Golang Docker image

You can now download the Golang Docker image.

Execute the command at the command line Docker search Golang can query the Golang image information.

$ docker Search Golangindex       NAME                              DESCRIPTION         STARS     official   automateddocker.io   docker.io/ Golang                  Go (Golang) is ...   1649      [OK]       docker.io   docker.io/circleci/golang         Go (Golang) is ...   8                    docker.io   docker.io/resin/artik710-golang   Go (Golang) is ...   1                    docker.io   docker.io/resin/artik10-golang    Go (Golang) is ...   

As you can see, the highest number of STARS is ranked first in the query results, and the official option below is OK, stating that it is official, so it should be more credible in theory. So we chose to use the first one.

Execute the command docker pull Docker.io/golang to download the official image of Golang.

$ Docker Pull docker.io/Golang[[email protected] golang]$ Docker pull Docker.io/golangusing default tag:latesttrying to pull repository Docker.io/library/Golang latest:pulling from Docker.io/library/golang723254a2c089:extracting [===============================>]40.83MB/45.12mbabe15a44e12f:download Complete 409a28e3cc3d:download complete503166935590: Download Complete abe52c89597f:download complete ce145c5cf4da:downloading [===============>]31.36MB/104.3mb96e333289084:download Complete 39cd5f38ffb8:download complete 39cd5f38ffb8:pulling FS Layer

After the download, look at the information in the image Docker images Golang

[email protected] golang]$ Docker images golangrepository          TAG                 IMAGE ID            CREATED             sizedocker.io/golang    latest              138bd936fa29        4 weeks ago         733.3 MB

4. Using Golang mirroring

Execute the command Docker run-it--rm Golang bash , test the Golang image you just downloaded

[[email protected] golang]$ Docker run-it--rm  golang bash[email protected]:/go# go Versiongo versi On Go1. 9.2 LINUX/AMD64

As you can see, the latest is version 1.9.2.

View the default directory after entering the container

pwd/go

It appears to be under the/go directory.

However, because we added the--RM parameter when we created the container, the temporary container will be deleted when we exit the container by executing the exit command.

So how do we use this golang image to work?

5. One way to use the Golang Docker image

First, if we write the file directly inside the Docker container, the data cannot be persisted. Even if you can use the Docker CP command to copy our files to the inside of the container, or to copy the files from the container, there are still a lot of inconvenient places in the operation.

Furthermore, the third-party package that the program relies on is downloaded to the container's/go directory when the build operation is performed in the Golang image. If each container is deleted, or every time a new container is created, a third-party package will need to be pulled from the network. If the network speed is good, otherwise the time wasted a bit serious.

So, we have to map the host directory to the container so that the file can be persisted.

We can use the-v parameter of the Docker command to map the host's directory to the container. However, if you use the-v parameter only, you will have permission restrictions to manipulate the directory inside the container, so you need to add the--privileged parameter to the container to decentralize.

[Email protected] golang]$pwd# View your current directory/root/Golang[[email protected] golang]$ls-L # View sub-directories and files in the current directory, etc. total0drwxrwxrwx.2Root root6JanTen  on: +codedrwxrwxrwx.2Root root6JanTen  on: -Go[[email protected] golang]$ docker run \ # Execute Docker command, create a new container and run the container>-it \ # using interactive mode>--RM\ # When exiting this container, the system automatically deletes this container>-v/root/golang/go:/go \ # put the host directory'/root/golang/go'Map to a directory within a container'/go'>-v/root/golang/code:/code \ # put the host directory'/root/golang/code'Map to a directory within a container'/code'>--Privileged \ # Give this container super permissions, otherwise you can't manipulate the above two directories mapped in> Docker.io/golang \ # based on mirroring'Docker.io/golang'Create a container> Bash # Execute commands inside a container'Bash'> [Email protected]:/go# # has entered the container.

We can create some files inside the container's directory/go and directory/code, and then return to the host for viewing.

[Email protected]:/go#Echo "Hello">/go/h.txt # Inside the container, create the file'/go/h.txt'and write data [email protected]:/go#Echo " World">/code/W. txt # inside the container, create the file'/code/w.txt'and write data [email protected]:/go# Exit # Exit Container Exit[[email protected] golang]#Find# has returned to the mainframe. In the host's'/root/golang'view files under directory :/code./code/W. txt # to see the file'W.txt'./go./go/h.txt # and file'H.txt'[email protected] golang]#Catgo/h.txt hello[[email protected] golang]#Catcode/W. txt World

Using the method above, each time a container is created, the directory specified on the host is mapped to the container, and the modified data is reflected on the host.

6. Convenient for later inspection and direct use, the complete command is as follows


Opening the container

Docker run-it--rm -v/root/golang/go:/go-v/root/golang/code:/code--privileged Docker.io/golang Bash

Sync post:https://www.fengbohello.top/archives/go-env-docker

Using Docker mirroring to build the GO language environment

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.