Docker构建开发环境

        以往在新的机器上构建开发环境时,往往需要安装比较多的工具软件,这个过程比较耗时。有人会说,那可以把这些命令固化在shell脚本中。当然这也是一个可行的方法。
        随着Docker的发展,容器与容器云大行其道。本着熟悉Docker的想法,加之Docker具有简化配置、快速部署的特点,这里尝试使用Docker来构建开发环境。
        原始镜像为Ubuntu,常用的开发工具如GCC、G++、GIT等,并安装SSH,支持用户密码登陆以进行开发。由于Ubuntu默认的源的速度较慢,可以考虑更换为国内的,这里选择的是http://mirrors.tuna.tsinghua.edu.cn/ubuntu/。下面是详细的步骤记录。
        sources.list:
# deb cdrom:[Ubuntu 16.04 LTS _Xenial Xerus_ - Release amd64 (20160420.1)]/ xenial main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security multiverse
        Dockerfile:
# Pull base image  
FROM ubuntu

MAINTAINER TAKCHICHAN "[email protected]"  
# backup sources file
RUN cp /etc/apt/sources.list /etc/apt/sources.list.bak

# update sources
ADD sources.list /tmp/sources.list
RUN cp -f /tmp/sources.list /etc/apt/sources.list
RUN apt-get update  
  
# Install curl  
RUN apt-get update
RUN apt-get -y install python
RUN apt-get -y install gcc
RUN apt-get -y install g++
RUN apt-get -y install automake
RUN apt-get -y install autoconf
RUN apt-get -y install libtool
RUN apt-get -y install git
RUN apt-get -y install golang
RUN apt-get -y install ssh
RUN apt-get -y install subversion
RUN apt-get -y install cmake
RUN apt-get -y install lighttpd
RUN apt-get -y install vim
RUN apt-get -y install wget
RUN apt-get -y install curl
RUN apt-get -y install net-tools
RUN apt-get -y install inetutils-ping
RUN apt-get -y install telnet

RUN mkdir /var/run/sshd

# change sshd listen port
RUN sed -i 's/Port[ ]*22/Port 36000/' /etc/ssh/sshd_config
RUN echo "PasswordAuthentication yes" >> /etc/ssh/sshd_config

# add user
RUN mkdir /home/takchi
RUN useradd -s /bin/bash takchi
RUN echo "takchi:123456" | chpasswd

# Container should expose ports.  
EXPOSE 36000
CMD ["/usr/sbin/sshd", "-D"]

        构建镜像:

docker build -t ubuntu:devel ./

        启动容器:

docker run -it -p 36000:36000 --privileged=true -v /opt/projects:/home/takchi/ ubuntu:devel

        ssh登录:

ssh -p 36000 takchi@${HOST_IP}    

        最后附上代码路径,https://github.com/UsrNameOccupied/Docker/tree/master/Docker/dev_env

你可能感兴趣的:(Linux)