2016年7月4日星期一

.NET Core 1.0学习(3)-做了个靠谱点的docker image)

本来想进一步做aspnet的练习,结果在docker里练到"Building Projects with Yeoman"的地方就碰到了yeomon那些鸟人故意搞的"去root化"问题,用root执行它就唧唧歪歪爆无聊的错,一个个硬chmod是可以,但是不爽,干脆就自己做个Dockerfile生成一个能够在里面开发的docker image吧,全当练习Docker Image制作了。

碰到的错误就是那permission denied '/root/.config/configstore/insight-yo.yml'呀,Attempt To Unlock * Which Hasn't Been LockedNuGet cache之类的错误。前者是yemon的,后者是nodejs安装的不够新(apt-get install nodejs的是0.10级别的版本,谁想的到啊)
做好的docker image叫osexp2000/dnetcore_docker_image (或者osexp2000/dnetcore,更新快些)。
docker run -it osexp2000/dnetcore_docker_image
进去之后就是bash,以dev_user用户运行在/home/dev_user,这个用户能够sudo(无需密码)。
进去就可以顺利的运行aspnet模版生成工具yeomen了。
yo aspnet
里面什么npm和yeomen之类的东西都准备好了,甚至基本的vim,lsofnetstat之类的都准备好了,不然真不方便查看环境。
运行的样子是这样的:
$ docker run -it osexp2000/dnetcore_docker_image
dev_user@640be8c28a81:~$ id
uid=1000(dev_user) gid=1000(dev_user) groups=1000(dev_user),27(sudo)
dev_user@640be8c28a81:~$ yo aspnet
? ==========================================================================
We're constantly looking for ways to make yo better! 
May we anonymously report usage statistics to improve the tool over time? 
More info: https://github.com/yeoman/insight & http://yeoman.io
========================================================================== Yes

     _-----_     ╭──────────────────────────╮
    |       |    │      Welcome to the      │
    |--(o)--|    │  marvellous ASP.NET Core │
   `---------´   │        generator!        │
    ( _´U`_ )    ╰──────────────────────────╯
    /___A___\   /
     |  ~  |     
   __'.___.'__   
 ´   `  |° ´ Y ` 

? What type of application do you want to create? (Use arrow keys)
❯ Empty Web Application 
  Console Application 
  Web Application 
  Web Application Basic [without Membership and Authorization] 
  Web API Application 
  Class Library 
  Unit test project (xUnit.net)

Dockerfile放在这儿了。很少,干脆贴下来。
FROM microsoft/dotnet

RUN apt-get update

#install nodejs 6.x
RUN curl -sL https://deb.nodesource.com/setup_6.x | bash -
RUN apt-get install -y nodejs


#install aspnet generator
RUN npm install -g yo bower grunt-cli gulp
RUN npm install -g generator-aspnet




#this must be installed
RUN apt-get install -y sudo


#these are optional utilities
RUN apt-get install -y vim
RUN apt-get install -y bash-completion
RUN apt-get install -y lsof
RUN apt-get install -y net-tools
RUN apt-get install -y man
RUN apt-get install -y less
RUN apt-get install -y netcat



#add developer user
RUN adduser --disabled-password --gecos "Developer" dev_user
RUN adduser dev_user sudo
RUN echo "dev_user ALL=(ALL:ALL) NOPASSWD: ALL" | (EDITOR="tee -a" visudo)


#optional aliases
RUN echo 'alias ll="ls -lFa"' >> ~/.bashrc
RUN echo 'alias ll="ls -lFa"' >> /home/dev_user/.bashrc

#this is also important so yomen will not complains
RUN chown -R root:sudo /usr/local

RUN rm -f /core
RUN rm -fr /tmp/NuGetScratch

USER dev_user
WORKDIR /home/dev_user
可以自己存起来build成docker image
docker build -t IMAGE_NAME_HERE --rm - < Dockerfile

下次再沿着Your First ASP.NET Core Application on a Mac Using Visual Studio Code — ASP.NET documentation继续学。不过我想看看如何通过Mac OS X里的Visual Studio Code来直接把代码发布到docker里。

没有评论:

发表评论