搭建go开发docker容器环境

#基础镜像
FROM ubuntu:22.04  AS builder
LABEL stage=plat

#增加环境变量
ENV  GOARCH amd64
ENV GOOS linux
ENV  GOBIN $GOROOT/bin/
ENV GOTOOLS $GOROOT/pkg/tool/
ENV GO111MODULE on
ENV GOPROXY https://goproxy.cn,direct
#执行命令
#RUN  sed -i s@/archive.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.list
RUN  apt-get clean
RUN apt-get update
RUN apt-get -y install ca-certificates
RUN sed -i "s@http://.*archive.ubuntu.com@https://mirrors.tuna.tsinghua.edu.cn@g" /etc/apt/sources.list
RUN sed -i "s@http://.*security.ubuntu.com@https://mirrors.tuna.tsinghua.edu.cn@g" /etc/apt/sources.list
RUN apt-get clean
RUN apt-get update

RUN apt-get -y install tzdata
ENV TZ Asia/Shanghai

RUN apt-get -y install golang
RUN apt-get -y install vim
# useradd going # 创建 going 用户,通过 going 用户登录开发机进行开发
RUN apt-get  -y install make autoconf automake cmake git-lfs telnet 
RUN apt-get -y install git
RUN apt-get -y install autoconf libtool build-essential
RUN git config --global user.name "lingr7"   \
    && git config --global user.email "lingr7@qq.com"  \
    && git config --global credential.helper store   \
    && git config --global core.longpaths true \
    && git config --global core.quotepath off \
    && git lfs install --skip-repo

ARG USERNAME=going
ARG USER_UID=1000
ARG USER_GID=$USER_UID

# Create the user
RUN groupadd --gid $USER_GID $USERNAME \
    && useradd --uid $USER_UID --gid $USER_GID -m -s /bin/bash $USERNAME \
    #
    # [Optional] Add sudo support. Omit if you don't need to install software after connecting.
    && apt-get update \
    && apt-get install -y sudo \
    && echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \
    && chmod 0440 /etc/sudoers.d/$USERNAME

# ********************************************************
# * Anything else you want to do like clean up goes here *
# ********************************************************

# [Optional] Set the default user. Omit if you want to keep the default as root.
USER $USERNAME

COPY ./.bashrc /home/$USERNAME/
RUN mkdir -p /home/$USERNAME/workspace
# 安装vscode go插件相关库
RUN sudo chmod -R 777 /bin/
CMD ["source", "/home/$USERNAME/.bashrc"]
CMD ["top"]


# .bashrc
 
# User specific aliases and functions
 
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
 
# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi
 
# User specific environment
# Basic envs
export LANG="en_US.UTF-8" # 设置系统语言为 en_US.UTF-8,避免终端出现中文乱码
export PS1='[\u@dev \W]\$ ' # 默认的 PS1 设置会展示全部的路径,为了防止过长,这里只展示:"用户名@dev 最后的目录名"
export WORKSPACE="$HOME/workspace" # 设置工作目录
export PATH=$HOME/bin:$PATH # 将 $HOME/bin 目录加入到 PATH 变量中
#export GOVERSION=go1.18.3 # Go 版本设置
#export GO_INSTALL_DIR=$HOME/go # Go 安装目录
#export GOROOT=$GO_INSTALL_DIR/$GOVERSION # GOROOT 设置
export GOPATH=$WORKSPACE/golang # GOPATH 设置
export PATH=$GOROOT/bin:$GOPATH/bin:$PATH # 将 Go 语言自带的和通过 go install 安装的二进制文件加入到 PATH 路径中
#export GO111MODULE="on" # 开启 Go moudles 特性
#export GOPROXY=https://goproxy.cn,direct # 安装 Go 模块时,代理服务器设置
export GOPRIVATE=
export GOSUMDB=off # 关闭校验 Go 依赖包的哈希值

export LD_LIBRARY_PATH=/usr/local/lib

# Default entry folder
cd $WORKSPACE # 登录系统,默认进入 workspace 目录

构建镜像

docker build -t ligong2/ubuntu-net-test:0.42 .

运行容器

docker run -itd --name ubun3 --dns=8.8.8.8  ligong2/ubuntu-net-test:0.42

使用vscode remote container进入开发。

posted @ 2022-09-11 17:57  lingr7  阅读(4)  评论(1编辑  收藏  举报