首先来看下我的目录结构:
[root@localhost docker]# pwd /soft/docker [root@localhost docker]# ll total 4 -rw-r--r--. 1 root root 541 Aug 15 11:20 Dockerfile [root@localhost docker]# cd .. [root@localhost soft]# pwd /soft [root@localhost soft]# ll total 393332 -rw-r--r--. 1 root root 9271609 Aug 10 17:23 apache-tomcat-8.5.4.tar.gz drwxr-xr-x. 2 root root 23 Aug 15 11:20 docker drwxr-xr-x. 9 10011 10011 4096 Jan 26 2016 hadoop -rw-r--r--. 1 root root 212046774 Aug 8 18:01 hadoop-2.7.2.tar.gz drwxr-xr-x. 8 10 143 4096 Jun 23 09:56 jdk -rw-r--r--. 1 root root 181435897 Aug 8 17:23 jdk-8u102-linux-x64.tar.gz drwxr-xr-x. 9 root root 4096 Aug 10 17:24 tomcat Dockerfile文件创建好了,就可以通过docker build来创建docker镜像。 看下docker build的帮助文件: [root@localhost soft]# docker build --help Usage: docker build [OPTIONS] PATH | URL | - Build an image from a Dockerfile --build-arg=[] Set build-time variables --cpu-shares CPU shares (relative weight) --cgroup-parent Optional parent cgroup for the container --cpu-period Limit the CPU CFS (Completely Fair Scheduler) period --cpu-quota Limit the CPU CFS (Completely Fair Scheduler) quota --cpuset-cpus CPUs in which to allow execution (0-3, 0,1) --cpuset-mems MEMs in which to allow execution (0-3, 0,1) --disable-content-trust=true Skip image verification -f, --file Name of the Dockerfile (Default is 'PATH/Dockerfile') --force-rm Always remove intermediate containers --help Print usage --isolation Container isolation level -m, --memory Memory limit --memory-swap Swap limit equal to memory plus swap: '-1' to enable unlimited swap --no-cache Do not use cache when building the image --pull Always attempt to pull a newer version of the image -q, --quiet Suppress the build output and print image ID on success --rm=true Remove intermediate containers after a successful build --shm-size Size of /dev/shm, default value is 64MB -t, --tag=[] Name and optionally a tag in the 'name:tag' format --ulimit=[] Ulimit options -v, --volume=[] Set build-time bind mounts 生成镜像: [root@localhost soft]# docker build -t centos:base -f /soft/docker/Dockerfile /soft Sending build context to Docker daemon 1.118 GB Step 1 : FROM docker.io/centos Trying to pull repository docker.io/library/centos ... latest: Pulling from docker.io/library/centos 3d8673bd162a: Pull complete Digest: sha256:a66ffcb73930584413de83311ca11a4cb4938c9b2521d331026dad970c19adf4 Status: Downloaded newer image for docker.io/centos:latest ---> 970633036444 Step 2 : MAINTAINER The CentOS Test Images <liuqi@rzport.com> - liuqi ---> Running in 69fd729187f2 ---> 7f61239ff233 Removing intermediate container 69fd729187f2 Step 3 : RUN mkdir -p /usr/app ---> Running in fb5e95a82274 ---> 32826b551857 Removing intermediate container fb5e95a82274 Step 4 : RUN ls ---> Running in 4cbe815e848f anaconda-post.log bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var ---> 8c3b8f7ebc57 Removing intermediate container 4cbe815e848f Step 5 : RUN pwd ---> Running in f226b1caf3e0 / ---> 19379045c11d Removing intermediate container f226b1caf3e0 Step 6 : COPY /jdk /usr/app/jdk/ ---> 3137c3ee72dc Removing intermediate container 6c1e513e964c Step 7 : ADD tomcat/ /usr/app/tomcat/ ---> 8c6d7a52769a Removing intermediate container 98d8178ef560 Step 8 : ADD hadoop/ /usr/app/hadoop/ ---> 34a8ea483a7e Removing intermediate container ee7fdc55dbc9 Step 9 : ENV JAVA_HOME /usr/app/jdk ---> Running in 0a4f8f242ede ---> a297fbddb78a Removing intermediate container 0a4f8f242ede Step 10 : ENV PATH $JAVA_HOME/bin:$PATH ---> Running in 368103f758dd ---> 9d20362732d7 Removing intermediate container 368103f758dd Successfully built 9d20362732d7 [root@localhost soft]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos base 9d20362732d7 4 minutes ago 904.8 MB docker.io/centos latest 970633036444 2 weeks ago 196.7 MB 通过-f来指定Dockerfile文件的位置,后面的/soft及其目录下必须能够找到Dockerfile文件否则就会报上下文环境的错误,MV,COPY,ADD的文件位置都是相对/soft来说的。 [root@localhost soft]# docker build -t centos:bases -f /soft/docker/Dockerfile /usr unable to prepare context: The Dockerfile (/soft/docker/Dockerfile) must be within the build context (/usr) 这样可以解决Dockerfile文件与需要拷贝的文件不在同一个目录下的问题,例如使用ADD,COPY指令出现的 no such file or directory,Forbidden path outside the build context: ../jdk/ ()等类似的错误。 镜像创建完毕后,就可以启动docker run来启动镜像,启动镜像的时候同时会创建一个容器,我们可以简单的把镜像比如成类,容器就是这个类的实例,Image可以理解为一个系统镜像,Container是Image在运行时的一个状态。如果拿虚拟机作一个比喻的话,Image就是关机状态下的磁盘文件,Container就是虚拟机运行时的磁盘文件,包括内存数据。 其中,-t选项让Docker分配一个伪终端(pseudo-tty)并绑定到容器的标准输入上,-i则让容器的标准输入保持打开。 当利用docker run来创建容器时,Docker 在后台运行的标准操作包括: 检查本地是否存在指定的镜像,不存在就从公有仓库下载 利用镜像创建并启动一个容器 分配一个文件系统,并在只读的镜像层外面挂载一层可读写层 从宿主主机配置的网桥接口中桥接一个虚拟接口到容器中去 从地址池配置一个 ip 地址给容器 执行用户指定的应用程序 执行完毕后容器被终止参考:https://www.dwhd.org/20151202_113538.html?lan=cn&lan=cn http://blog.chinaunix.net/uid-10915175-id-4442826.html http://blog.csdn.net/zssureqh/article/details/52009043 http://blog.csdn.net/we_shell/article/details/38445979 http://my.oschina.net/renguijiayi/blog/353835 http://blog.csdn.net/wsscy2004/article/details/25878363 http://www.simapple.com/364.html http://pdfwork.cn/blog/2015/docker/