docker搭建c++开发环境

docker搭建c++开发环境

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

#基础镜像
FROM centos:7

RUN yum -y update

#安装 gcc11 gdb11 等
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

#更改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

#开启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

#安装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

#安装llvm,版本问题,目前无法直接使用,需要自己后续编译
# WORKDIR /root
# RUN  wget https://github.com/llvm/llvm-project/archive/refs/tags/llvmorg-13.0.0.tar.gz
#启动 ssh 服务
CMD ["/usr/sbin/sshd","-D"]

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

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

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

docker run --network=host --name cplusdev -d devcplus

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

你可能感兴趣的:(docker,c++,容器)