Dockerfile basic combat: building a basic ubuntu14.04 image

We can download the image in the official repository from Docker Hub. I downloaded the ubuntu image myself, only 188Mabout the left and right, very small. But after reading it, the software source inside is still official, and it is not installed vim, so I plan to write one myself Dockerfileand use it to build a basic ubuntu image suitable for myself.

deb http://archive.ubuntu.com/ubuntu/ trusty main restricted
deb-src http://archive.ubuntu.com/ubuntu/ trusty main restricted
...................

build context

build context, a custom folder where the Dockerfile and some required files are placed. For example mine:

.
├── baseimage
│   ├── Dockerfile
│   ├── README.md
│   ├── sources.list
│   └── vimrc

Dokerfile

There are two ways to make an image:

  • Create from an existing container via commitcommand

    • Inconvenient operations in dockerfile can be operated in the container and then submitted
    • No need to start containers in batches
    • Learn and practice by yourself, no need to transplant
  • Build with Dockerfile

    • Convenient, flexible and portable
    • Suitable for deploying a large number of images and containers

Dockerfile Basics

  • '#' means a comment. Generally, the first line of the Dockerfile annotates the basic information and version of the container.
  • 命令:参数Dockerfile is the basic construction statement, the command is all uppercase, and the following parameters depend on the command
  • FROM, must be the first command item, indicating which image my image is built on

    FROM ubuntu
    
  • MAINTAINER, followed by the name and email address of the construction, for easy contact

    MAINTAINER adolphlwq <kenan3015@gmail.com>
    
  • LABEL, use key-value pairs to specify image metadata

    LABEL Description="it is used as a basic image for DuoHuoStudio and my study.I will update and install vim." Vendor="Basic image"
    
  • ADD, pass files to Docker daemon at build time

    ADD sources.list /etc/apt/
    
  • RUN, receive operations and commands sudo apt-get install -y vim, etc.

    ADD sources.list /etc/apt/ 
    
  • CMD, the command that starts by default when a successfully built image is started for the first time

    • There is only one CMD, usually at the end of the Dockerfile by default
    • If there are multiple CMDs, only the last one works
    • CMD会被docker run ..后面的命令覆盖
    CMD ["/bin/bash"]
    
  • ENV,设置环境变量

    ENV REFRESHED_AT 2015-05-18
    

构建命令

cd baseimage(构建上下文文件夹)
docker build -t="duohuosrudio/ubuntu:14.04_64_base_image" .

docker build-t表示容器的名字
duohuosrudio/ubuntuduohuostudio表示仓库名(不允许大写),ubuntu表示镜像名。
ubuntu:14.04_64_base_image后的14.04_64_base_image是标签,如果没有指定,默认的是latest

构建过程:

实践中遇到的错误

  • apt-get upgradeapt-get install vim都要加上** -y**选项,不然会报错
  • ADD后面必须接两个参数,ADD <src>... <dest>表示要添加的文件,表示文件添加到哪里。
  • ADD添加的文件必须以构建上下文为根目录来找,不能超出构建上下文的范围。

如果除错停止构建了也不要担心,Docker会把构建过程中的文件都缓存起来,再次构建时会从缓存的地方开始,节省时间。

除错停止后docker images会出现一个只有IMAGE ID的镜像,这个就是构建失败后留下的缓存,我们可以通过image id来运行这个镜像,然后执行除错的命令来检查为什么出错!(下图的最后1行)

adolph@geek:~/programs/DockerWorkspace/dockerfile/baseimage$ docker images
REPOSITORY               TAG                   IMAGE ID            CREATED             VIRTUAL SIZE
test/ubuntu              14.04_64_base_image   e9390454465c        14 hours ago        269.1 MB
test2/ubuntu             14.04_64_base_image   e9390454465c        14 hours ago        269.1 MB
duohuostudio/ubuntu      14.04_64_base_image   e9390454465c        14 hours ago        269.1 MB
<none>                   <none>                f6efc4dac25a        16 hours ago        269.1 MB

总结

docker build -t="duohuostudio/ubuntu:14.04_64_base_image" .

这条命令的最后一个参数是用来指定Dockerfile的路径,千万不要忘记。

dockerfile已经上传到 github 地址

镜像也已经上传到Docker Hub上了,可以通过下列命令下载镜像

docker pull adolphlwq/ubuntu

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327071218&siteId=291194637