docker搭建c++开发环境

利用Docker搭载C/C++开发环境
docker搭建c++开发环境
1.Dockerfile 文件内容

#基础镜像
FROM centos:centos7

# 设置时区
ARG TZ=Asia/Shanghai
ENV TZ ${TZ}
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

RUN yum -y update

#安装 gcc11
RUN yum -y install centos-release-scl && yum -y install devtoolset-11-toolchain
RUN echo "source /opt/rh/devtoolset-11/enable" >> ~/.bashrc

#安装基本的网络调试工具,和tmxu,git
RUN yum -y install wget \
&& yum -y install net-tools nc telnet \
&& yum -y install tmux \
&& yum -y install vim

#安装git
RUN  yum install -y \
https://repo.ius.io/ius-release-el7.rpm \
https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm \
&& yum install -y git236


#开启ssh服务
RUN yum -y install passwd openssh-server openssl-devel
#设置root用户密码为root
RUN /bin/echo "root" | passwd --stdin root
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key \
&& ssh-keygen -t rsa -f /etc/ssh/ssh_host_ecdsa_key \
&& ssh-keygen -t rsa -f /etc/ssh/ssh_host_ed25519_key

#安装llvm相关工具集:clang, lldb, clang-tools-extra
RUN yum install epel-release -y
RUN yum install llvm-toolset-7.0-clang.x86_64 -y \
&& yum install llvm-toolset-7.0-lldb.x86_64 -y \
&& yum install llvm-toolset-7.0-clang-tools-extra.x86_64 -y \
&& yum install llvm-toolset-7.0-llvm.x86_64 -y
RUN echo "source /opt/rh/llvm-toolset-7.0/enable" >> ~/.bashrc


ENV http_proxy=http://127.0.0.1:33210
ENV https_proxy=http://127.0.0.1:33210
#安装cmake 
WORKDIR /root
RUN wget https://github.com/Kitware/CMake/releases/download/v3.19.8/cmake-3.19.8-Linux-x86_64.tar.gz
RUN tar -xzvf cmake-3.19.8-Linux-x86_64.tar.gz
RUN rm -rf cmake-3.19.8-Linux-x86_64.tar.gz
RUN ln -s /root/cmake-3.19.8-Linux-x86_64/bin/cmake /usr/bin/cmake \
&& ln -s /root/cmake-3.19.8-Linux-x86_64/bin/ccmake /usr/bin/ccmake \
&& ln -s /root/cmake-3.19.8-Linux-x86_64/bin/cmake-gui /usr/bin/cmake-gui \
&& ln -s /root/cmake-3.19.8-Linux-x86_64/bin/cpack /usr/bin/cpack \
&& ln -s /root/cmake-3.19.8-Linux-x86_64/bin/ctest /usr/bin/ctest

#更改shell为zsh,安装on-my-zsh
RUN yum -y install zsh \
&& mkdir -p ~/.oh-my-zsh \
&& git clone https://github.com/ohmyzsh/ohmyzsh.git ~/.oh-my-zsh \
&& cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc \
&& chsh -s /bin/zsh
RUN sed -i 's/robbyrussell/crunch/g' /root/.zshrc
RUN echo "source /opt/rh/devtoolset-11/enable" >> ~/.zshrc

#安装on-my-zsh插件
RUN git clone https://github.com/zsh-users/zsh-syntax-highlighting.git \
${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting \
&& git clone https://github.com/zsh-users/zsh-autosuggestions \
${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions \
&& sed -i 's/plugins=(git)/plugins=(git zsh-syntax-highlighting zsh-autosuggestions)/g' /root/.zshrc

ENV http_proxy=
ENV https_proxy=

#启动 ssh 服务
CMD ["/usr/sbin/sshd","-D"]

2.根据Dockerfile创建docker镜像
注:使用宿主机网络,有时是为了使用本地的代理

#devcplus 是要创建镜像的名字, .指定Dockerfile的位置
docker build --platform linux/amd64 --network=host -t devcentos7920231209 .

3.运行镜像
注:使用宿主机网络,有时是为了使用本地的代理。

docker run --network=host --name devcpluscentos20231206 -d devcpluscentos79


#创建docker volume
docker volume create myvolume
#挂在docker volume启动容器
docker run -v GithubCpp:/root --network=host --name devcentos7920231209 -d devcentos7920231209:latest

4.如果需要高版本llvm,只能如下源码编译安装,十分耗时

wget https://github.com/llvm/llvm-project/archive/refs/tags/llvmorg-13.0.1.tar.gz
tar -zxf llvmorg-13.0.1.tar.gz
cd llvm-project-llvmorg-13.0.1
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_PROJECTS="clang;lldb;clang-tools-extra" -G "Unix Makefiles" ../llvm
make    # 这一步十分耗时,建议放后台跑
make install
Logo

一站式 AI 云服务平台

更多推荐