导航

Mac下使用docker配置Centos7的go开发环境

Posted on 2021-03-01 16:08  蝈蝈俊  阅读(670)  评论(1编辑  收藏  举报

之前配置Centos6看:https://www.cnblogs.com/ghj1976/p/12344677.html

安装Centos指定版本操作系统

https://hub.docker.com/_/centos?tab=tags 选择你需要安装的centos版本比如我下面选择的是 7.6 版本

% docker pull centos:centos7.6.1810
centos7.6.1810: Pulling from library/centos
ac9208207ada: Pull complete 
Digest: sha256:62d9e1c2daa91166139b51577fe4f4f6b4cc41a3a2c7fc36bd895e2a17a3e4e6
Status: Downloaded newer image for centos:centos7.6.1810
docker.io/library/centos:centos7.6.1810

查看本地镜像

使用以下命令来查看是否已安装了 centos7:

% docker images
REPOSITORY   TAG              IMAGE ID       CREATED         SIZE
centos       latest           f16ccff02e5a   15 hours ago    1.66GB
<none>       <none>           780a1bc94234   28 hours ago    1.62GB
centos       6.9              2199b8eb8390   23 months ago   195MB
centos       centos6.9        2199b8eb8390   23 months ago   195MB
centos       centos7.6.1810   f1cb7c7d58b7   23 months ago   202MB

执行 centos

docker run --privileged=true -it centos:centos7.6.1810 
  • --privileged是以获取系统权限的形式运行,
  • -it是互动模式,跟本地的系统进行交互,调用的本地的终端

保存

在未退出来之前,另开一个窗口,看正在运营的容器ID:

% docker ps    
CONTAINER ID   IMAGE                   COMMAND       CREATED         STATUS         PORTS     NAMES
bf41218a1046   centos:centos7.6.1810   "/bin/bash"   6 minutes ago   Up 6 minutes             infallible_elion

保存这个容器

% docker commit bf41218a1046 centos:myc7go 
sha256:f69db922e983a9cb528760db41015eb87bf38f86fb0022d29e9c6e244ba88d53

docker commit :从容器创建一个新的镜像。
docker commit [选项] <容器ID或容器名> [<仓库名>[:<标签>]]

配置 Centos

# yum install wget
# yum install gcc
# yum install vim

配置golang

在Docker->preference中添加目录共享。

带目录共享重新启动容器

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

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

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

安装go 1.6

删除旧版本go

cd /usr/local/
rm -rf ./go

Download the archive and extract it into /usr/local, creating a Go tree in /usr/local/go

# cd /software/golang
# tar -C /usr/local/ -xzf ./go1.16.linux-amd64.tar.gz 

Add /usr/local/go/bin to the PATH environment variable

增加
export PATH=$PATH:/usr/local/go/bin

确保及时生效
# source /etc/profile

注意,我们在docker中跑centos,默认是不加载 /etc/profile 的,需要在 /root/.bashrc 这个文件增加环境变量。
Docker之/etc/profile不生效得问题

Verify that you've installed Go by opening a command prompt and typing the following command:

# go version
go version go1.16 linux/amd64

配置go编译

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