zoukankan      html  css  js  c++  java
  • Mac下使用docker配置golang centos下编译环境

    golang 交叉编译的典型一个场景是使用 go-sqlite3。

    go-sqlite3 编译需要 cgo , 但是交叉编译 cgo 又需要很多库,环境并不好配,编译好执行有没有问题还很麻烦。

    Binary was compiled with 'CGO_ENABLED=0', go-sqlite3 requires cgo to work.

    所以直接装个 docker 来运行要编译的环境是最灵活的方案。 

     

    安装Docker

    1、安装 docker 客户端

    下载地址: 官方下载地址

     

    2、注册账号

    点击 Sign Up 注册,注册完成后进行登陆

    https://hub.docker.com/

     

    3、安装 centos

    命令行执行

    docker pull centos:centos6.9

     

    4、执行 centos

    docker run --privileged=true -it centos:6.9 

    --privileged是以获取系统权限的形式运行,

    -it是互动模式,跟本地的系统进行交互,调用的本地的终端

     

    % docker run --privileged=true -it centos:6.9
    Unable to find image 'centos:6.9' locally
    6.9: Pulling from library/centos
    Digest: sha256:6fff0a9edc920968351eb357c5b84016000fec6956e6d745f695e5a34f18ecd2
    Status: Downloaded newer image for centos:6.9
    [root@19c2ce3493ea /]#

     

    保存docker镜像

    如果需要退出,直接用 exit 就可以退出,但是docker是不会保存你再容器中做过的修改的,

    第二次进来容器,之前安装过的东西都得重新安装一遍,那怎么办呢

     

    解决办法

    在未退出来之前,另开一个窗口

    先 docker ps

    这个是你现在正在运行的容器

    然后docker commit d83c4279f146 centos:6 #d83c4279f146是CONTAINER ID,centos:6是IMAGE,容器名

    这样之后再回到容器的那个窗口运行 exit,下次再进来就不会丢失在容器内的保存了

    UntitledImage

    参考: 在mac上安装Docker,并在Docker上安装centos以及运行

    下次进来 docker run --privileged=true -it centos    

     

    配置Centos

    配置 centos 的yum源

     

    我自己默认yum源这里 wget 都没有, 需要先配置源

    cd /etc/yum.repos.d

    # 备份系统自带的yum源
    mv CentOS-Base.repo CentOS-Base.repo.save

    # 手工下载 http://mirrors.163.com/.help/CentOS6-Base-163.repo
    # 并通过 vi 把内容写到 CentOS-Base.repo 

    # 更新缓存
    yum makecache

    之后再执行 就有 wget了。

    yum install -y wget

    在 /etc/yum.repos.d 目录下,也配置下阿里云的源,已备用

    wget http://mirrors.aliyun.com/repo/Centos-6.repo

     

    参考: https://www.jianshu.com/p/6bdb10adbad6

     

    在 centos上配置 golang

    yum install -y gcc

    一些go程序的build需要gcc,所以要安装。

    参数 -y(当安装过程提示选择全部为"yes")

     

    yum 源默认没有golang的。

    # Install on CentOS: 
    # Current version: 1.11
    rpm --import https://mirror.go-repo.io/centos/RPM-GPG-KEY-GO-REPO
    curl -s https://mirror.go-repo.io/centos/go-repo.repo | tee /etc/yum.repos.d/go-repo.repo
    yum install golang

    安装方法如上。

    参考: https://cloud.tencent.com/developer/article/1478660

    其中 rpm --import 导入签名文件    (rpm --import /etc/pki/rpm-gpg/RPM* 有什么用?

     

    如果这个数据源下载太慢,也可以手工安装golang

    下载后的安装方法

    https://golang.org/doc/install?download=go1.13.8.linux-amd64.tar.gz

     

     

    docker 上访问物理机的目录

    在mac上面使用Docker挂载目录时,需要先在Docker->preference中添加该目录,才能进行挂载!

    UntitledImage

    然后在启动时 -v 指定映射关系

    docker run --privileged=true -v /Users/guohongjun/software:/software -it centos

    -v, --volume=[] 给容器挂载存储卷,挂载到容器的某个目录

    参考:  Docker run 命令参数及使用

     

     

    手工安装golang

    在映射的目录下执行

    tar -C /usr/local -xzf go1.13.8.linux-amd64.tar.gz

    vi /etc/bashrc 

    增加 配置 

    export PATH=$PATH:/usr/local/go/bin
    export GOPATH=/home/mygocodes

    加载配置

     source /etc/bashrc 

    注意: 

    启动docker时后应该是非交互方式进入bash,因此就不会读取profile文件,要从 /etc/bashrc 文件才能加载

     

    执行 go env 或 go version 就可以检查安装结果

    同样 gopath 也要配置

     

    这样启动时,就可以访问物理机的对应gopath目录了。

    docker run --privileged=true -v /Users/guohongjun/Documents/project/mygocodes:/home/mygocodes -it centos

  • 相关阅读:
    aspscheduler+uwsgi定时任务执行多次
    django定时任务
    django记录用户操作模块
    python缩小图片
    pymysql同时执行多条语句时报错
    MySQL8.0 修改密码
    linux中安装python3
    Mysql高性能
    Mysql高性能
    Mysql高性能
  • 原文地址:https://www.cnblogs.com/ghj1976/p/12344677.html
Copyright © 2011-2022 走看看