DOCKER学习之(一) 镜像部分常用命令

来源:互联网 发布:linux maxdepth 编辑:程序博客网 时间:2024/06/11 19:57

一、 列出镜像

docker images [OPTIONS] [REPOSITORY]参数-a, --all=falseShow all images (default hides intermediate images)--digests=falseShow digests//摘要-f, --filter=[]Filter output based on conditions provided--help=false Print usage--no-trunc=false     Don't truncate output-q, --quiet=false    Only show numeric IDs

二、拉取镜像

docker pull [OPTIONS] NAME[:TAG|@DIGEST]  例如: docker pull  ubuntu:latestdocker pull  ubuntu:12.04参数-a, --all-tags=false    Download all tagged images in the repository//拉取所有 tagged 镜像 --help=false    Print usage

三、查找镜像

docker search [OPTIONS] TERM/NAME--automated=false    Only show automated builds--help=false Print usage--no-trunc=false     Don't truncate output-s, --stars=0Only displays with at least x stars

例如找所有mysql镜像

wind@wind:~/static_web$ docker search  mysql
NAME               DESCRIPTION                          STARS     OFFICIAL   AUTOmysql              MySQL is a widely used, open-source  2695      [OK]       mysql/mysql-server Optimized MySQL Server Docker images 173                  [OK]centurylink/mysql  Image containing mysql. Optimized to 46                   [OK]sameersbn/mysql                                         36                   [OK]google/mysql       MySQL server for Google Compute Eng  16                   [OK]

其中OFFICIAL= [OK] 是官方的。

四、构建镜像(一) docker commit

第一步: 创建一个新容器
docker run –name xxx -it ubuntu /bin/bash
第二步:安装和配置需要的内容(这里已经在容器内部操作)
例如:安装apache
apt-get -yqq update
apt-get -y install apache2
exit //退出
第三步:docker commit
docker commit name/id 目标镜像库和镜像名称

Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]参数  -a, --author=       Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")  -c, --change=[]     Apply Dockerfile instruction to the created image  --help=false        Print usage  -m, --message=      Commit message  -p, --pause=true    Pause container during commit

五、构建镜像(二) Dockerfile

第一步:建立Dockerfile

FROM ubuntuMAINTAINER zxp huo "likewind@gmail.com"RUN apt-get updateRUN apt-get install -y nginxRUN echo 'hi ,i am in container'>/usr/share/nginx/html/index.htmlEXPOSE 80

第二步:用build建立镜像

Usage: docker build [OPTIONS] PATH | URL | -参数:  -c, --cpu-shares=0    CPU shares (relative weight)      --cpuset-cpus=        CPUs in which to allow execution (0-3, 0,1)      -f, --file=           Name of the Dockerfile (Default is 'PATH/Dockerfile')      --force-rm=false      Always remove intermediate containers      --help=false          Print usage      -m, --memory=         Memory limit      --memory-swap=        Total memory (memory + swap), '-1' to disable swap      --no-cache=false      Do not use cache when building the image      --pull=false          Always attempt to pull a newer version of the image      -q, --quiet=false     Suppress the verbose output generated by the containers      --rm=true             Remove intermediate containers after a successful build      -t, --tag=            Repository name (and optionally a tag) for the image(镜像名:标签)

例如:

sudo docker build -t="zxp/one-image-1" ./

在执行这个命令下由一个Dockerfile文件。

六、Dockerfile 中的指令

(1)CMD

容器启动时要运行的命令。下面的配置是等效的

 docker run -it ubuntu /bin/true CMD ["/bin/true"] CMD ["/bin/true","-1"]//带参数

注意:docker run的命令会覆盖Dockerfile中的CMD指令

(2)ENTRYPOINT

与CMD功能一样,docker run的命令不会覆盖Dockerfile中的ENTRYPOINT指令(除非run 时用–entrypoint参数)

 例如:ENTRYPOINT ["/usr/sbin/nginx","-g","daemon off"]

利用(1)和(2)构建默认参数

    ENTRYPOINT ["/usr/sbin/nginx"]    CMD ["-h"]

(3) WORKDIR

在容器内部设置一个工作目录,ENTRYPOINT和CMD的程序会在这个目录下执行

   WORKDIR /opt/webapp/db   RUN bundel install   WORKDIR /opt/webapp   ENTRYPOINT ["rackup"]

可通过-w参数覆盖工作目录

(4)ENV

在镜像构建过程中设置环境变量
例如:

   ENV RVM_PAHT /home/rvm   RUN gem install unicorn 等效RVM_PAHT=/home/rvm gem install unicorn

在如:

 ENV TARGET_DIR /opt/app WORKDIR $TARGET_DIR   WORKDIR指令的值被设置成/opt/app

也可在docker run 命令中通过-e参数覆盖值

(5)USER

该镜像会以什么样的用户去运行  USER nginx

我们可以指定用户名和UID以及组或GID,甚至是两者的组合

    USER user    USER user:group    USER uid    USER uid:gid     USER user:gid    USER uid:group 

也可在docker run 命令中通过-u参数覆盖值

(6)VOLUME

用来向镜像创建添加卷,提供如共享数据或对数据进行持久化操作

    VOLUME ["/opt/project"]    VOLUME ["/opt/project","/data"]

(7) ADD

用来将构建环境下的文件和目录复制到镜像中
ADD software.lic /opt/application/software.lic
这里复制构建目录下的software.lic到镜像的/opt/application/software.lic中。
这里的文件源可以是URL和压缩文件(合法的归档文件gzip,bzip2,xz),会直接解压缩。

(8)COPY

和ADD非常类似,COPY只关心在构建上下文中复制本地文件,不会去做文件提取和解压工作,本地文件必须和Dockerfile在同一个目录下。目的是容器的相对路径。

(9) ONBULID

给镜像添加触发器,可以认为这些指令是紧跟在FROM之后

     ONBULID ADD . /app/src     ONBULID RUN cd /app/src && make

七、查看镜像构建过程

sudo docker history id/name

八、从新镜像启动容器

详见docker run

九、删除镜像

sudo docker  rmi imageidUsage: docker rmi [OPTIONS] IMAGE [IMAGE...]Remove one or more images
参数:      -f, --force=false    Force removal of the image      --help=false Print usage      --no-prune=false     Do not delete untagged parents
0 0