Docker build and using it.

本文介绍了Docker的安装、使用方法,包括如何获取、构建和拉取镜像,以及在Windows主机上运行容器的步骤。同时提供了Dockerfile示例,展示了如何基于现有镜像构建自定义容器。






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-filesmydockerbuildcd mydockerbuild 
sudo docker build -t fedora-docker_test . # Build the image and tag it with name "fedora-docker_test" 
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="--insecure-registry 10.204.135.177:5000" # 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 "echo $'EXTRA_ARGS=\"--insecure-registry 10.204.135.17:5000\"' | sudo tee -a /var/lib/boot2docker/profile && sudo /etc/init.d/docker restart" 

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-idhttp://svn.ea.mot.com/dev/bsg/trunk # Checkout trunk 
cd trunk/makesystem 
make platforms.mk 
... # Edit platforms.mk 
cd <some dir you want to buildmake 

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 




[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++ \
  compat-libstdc++-33 \
  esound-tools \
  graphviz \
  gtk+-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++ \
  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++-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++-33.i686 \
  libstdc++.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+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+rwx /extra/componentcache

# Setup toolchain
RUN mkdir /usr/local/docker_test

RUN chmod a+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 "docker_test:docker_test" | 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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值