设为首页 收藏本站
查看: 998|回复: 0

[经验分享] Docker build and using it.

[复制链接]

尚未签到

发表于 2015-10-13 10:32:29 | 显示全部楼层 |阅读模式
  


  


  


  


  


  

Introduction

Docker is an open-sourced project providing containers on Linux. It supports Windows as well by running a Linux VM inside
VirtualBox.

It is possible to use Docker as a virtual machine to get a unified environment for software development and deployment.  
  



Preparation

Follow Docker's
Installation Guide to install Docker on your host PC. It supports almost all modern operating systems.

It is better to get familiar with Docker's concepts, e.g. Container, Image, Registries, the daemon and client, etc. A simple guide is here:
https://docs.docker.com/introduction/understanding-docker/  
  

Get the image

There're two ways to get a Docker image, build it yourself or pull it from others' registry.  

Build image

Docker builds a image based on a Dockerfile that describes what base image to use and what software to be installed.

An example
Dockerfile is attached for your reference, it is based on Fedora, installs a lot of softwares, creates a user
docker_test with password docker_test and grant sudo permission.


  • FROM defines the base image. Here we use fedora. By 2015.06.30, it's Fedora 22
  • RUN defines the command to run during building image. Here we install all the necessary packages for building
    docker_test by
    yum command. It also create some directories, setup permissions and create users, etc.
  • USER, WORKDIR and CMD is self-explained.

Create an empty dir, put the dockerfile and necessary files (docker_test.repo) for the image,
we can build the image.
mkdir mydockerbuild
cp <dockerfile> <other-files> mydockerbuild/
cd mydockerbuild
sudo docker build -t fedora-docker_test . # Build the image and tag it with name &quot;fedora-docker_test&quot;

Wait for a while, the image shall be built. Check it.
sudo docker images
It should show something like below:
REPOSITORY                         TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
fedora-docker_test                      latest              55832f3cdef9        3 hours ago         1.331 GB

  

Pull image

If you don't want to build the image by yourself, it's possible to directly pull a image from a registry.

The above image is served at 10.204.135.177:5000/fedora-docker_test on my PC.

Before pulling a image, you need to config your docker daemon to use insecure mode because I didn't setup a certification on my PC.

Edit the file /etc/default/docker on your host, add below line and restart your docker daemon.
DOCKER_OPTS=&quot;--insecure-registry 10.204.135.177:5000&quot; # Add this line in /etc/default/docker
sudo service docker restart

Then you can pull the image
sudo docker pull 10.204.135.177:5000/docker_test

  

Windows issue

If you're running Windows host and encounter problems, you may need to check
this and
this for certificate related issues.You need to run below command to use insecure mode of docker daemon.
boot2docker ssh &quot;echo $'EXTRA_ARGS=\&quot;--insecure-registry 10.204.135.17:5000\&quot;' | sudo tee -a /var/lib/boot2docker/profile && sudo /etc/init.d/docker restart&quot;

  

Run the image

Now you have got the image, let's run it. It's better to create a shared folder for your host and Docker container as well.
sudo mkdir /mnt/docker # Create the dir as shared folder to Docker
sudo docker run -i -t -v /mnt/docker:/mnt 10.204.135.17:5000/fedora-docker_test # Run the image, and mount /mnt to host's /mnt/docker

If everything is working fine, you should now be in container's shell.
[docker_test@c712118ed4ca ~]$
  

Windows issue

If you're running Windows host, you need a slightly different command to enter container's shell.
docker run -t -i 10.204.135.17:5000/fedora-docker_test //bin/bash

You can refer to
https://github.com/boot2docker/boot2docker for how to get folder sharing on Windows.  

Use the container

In container's shell, the user is docker_test, password
docker_test, and you have
sudo permission.

You can consider it as a Fedora OS, and run whatever commands you want, e.g.
svn co --username <your-core-id> http://svn.ea.mot.com/dev/bsg/trunk # Checkout trunk
cd trunk/makesystem
make platforms.mk
... # Edit platforms.mk
cd <some dir you want to build>
make

  

If you exit the container and want to re-attach into it, check the container ID or name, and start it.
sudo docker ps -a  # check all the containers
sudo docker start -ai <continaer id or name>  # start the container and attach to it

  




  


  • Dockerfile: Dockerfile for images that builds
    docker_test
  


  • docker_testtools.repo:
    docker_test repo for Fedora


  


  

[docker_test]
name=docker_test tools
failovermethod=priority
baseurl=http://devapp.ea.yaya.com/yum/repo/
enabled=1
gpgcheck=0
proxy=_none_


  
  


  


  

FROM fedora
# Build env
RUN yum install -y \
time \
svn \
openssh-clients \
compat-flex \
compat-flex-2.5.4a \
compat-gcc-34 \
compat-gcc-34-c&#43;&#43; \
compat-libstdc&#43;&#43;-33 \
esound-tools \
graphviz \
gtk&#43;-devel \
gtk2-devel \
libxkbfile \
libxkbfile-devel \
minicom \
mtools \
ORBit \
ORBit-devel \
perl-XML-DOM \
php \
postfix \
pulseaudio-esound-compat \
python-simplejson \
squashfs-tools \
syslinux \
tftp-server \
nss-mdns \
gperf \
perl-Test-Simple \
ttmkfdir \
glibc-static \
openssl-devel \
expat-devel \
flex \
bison \
libjpeg-devel \
freetype-devel \
libpng-devel \
glibc-devel \
gcc-c&#43;&#43; \
gcc \
ncurses-devel \
rpm-build \
pexpect \
quilt \
libblkid-devel \
e2fsprogs-devel \
perl-Regexp-Common \
subversion \
texinfo \
gettext \
libtool \
intltool \
flex-static \
module-init-tools \
python-cheetah \
python-devel \
libstdc&#43;&#43;-static \
libtirpc-devel \
perl-Test-MockModule \
flex-devel
RUN yum install -y \
libxml2-devel.i686 \
glib-devel.i686 \
ORBit-devel.i686 \
glibc-static.i686 \
glibc-devel.i686 \
compat-libstdc&#43;&#43;-33.i686 \
libstdc&#43;&#43;.i686
# Net-tools for ifconfig
RUN yum install -y net-tools
# Create hostname softlink
RUN ln -s /usr/lib64/gettext/hostname /usr/bin/hostname
# Setup ccache
RUN yum install -y ccache
RUN mkdir -p /extra/ccache
RUN chmod a&#43;rwx /extra/ccache
RUN CCACHE_DIR=/extra/ccache /usr/bin/ccache -M 50G
# Setup componentcache
COPY docker_testtools.repo /etc/yum.repos.d/
RUN yum install -y docker_test-componentcache
RUN mkdir -p /extra/componentcache
RUN chmod a&#43;rwx /extra/componentcache
# Setup toolchain
RUN mkdir /usr/local/docker_test
RUN chmod a&#43;rwx /usr/local/docker_test

# Add docker_test user and add as sudoer
RUN yum install -y sudo
RUN useradd -ms /bin/bash docker_test && echo &quot;docker_test:docker_test&quot; | chpasswd && usermod docker_test -a -G wheel
RUN mkdir -p /home/docker_test && chown -R docker_test:docker_test /home/docker_test
USER docker_test
WORKDIR /home/docker_test
CMD /bin/bash


  
  



版权声明:本文为博主原创文章,未经博主允许不得转载。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-126188-1-1.html 上篇帖子: 4.1.2、你的Docker Hub账户 下篇帖子: Docker的安装和镜像创建
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表