DOCKER 服务学习二

Docker 镜像于仓库

docker info

查看和删除镜像
docker images [OPTIONS][REPOSITORY]
-a, –all=false Show all images (default hides intermediate images)
–digests=false Show 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 images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ubuntu latest 58597429abbb 2 days ago 120.1 MB
centos latest f3b88ddaed16 9 days ago 192.5 MB
$ docker inspect [OPTIONS] CONTAINER|IMAGE [CONTAINER|IMGAE...]
docker inspect ubuntu
# docker rmi[OPTIONS] IMAGE[IMAGE...]
-f, --force=false Force removal of the image
--no-prune=false Do not delete untagged parents
$ docker rmi ubuntu:14.04
$ docker rmi $(docker images -q ubuntu)

获取和推送镜像
查找镜像
* Docker Hub
https://registry.hub.docker.com
* docker search [OPTIONS] TERM
Search the Docker Hub for images
–automated=false Only show automated builds
–help=false Print usage
–no-trunc=false Don’t truncate output
-s, –stars=0 Only displays with at least x stars

$ docker search -s 10 ubuntu
拉取镜像
* docker pull [OPTIONS] NAME [:TAG]
-a, –all-tags=false Download all tagged images in the repository
$ docker pull ubuntu:14.04
* 使用 –registry-mirror 选项
1.修改:/etc/defalut/docker
2.添加:DOCKER_OPS= “–registry-mirror=http://MIRROR-ADDR”
https://www.daocloud.io
构建镜像
* docker push [OPTIONS] NAME[:TAG]
Push an image or a repository to the registry
–help=false Print usage
* docker commit 通过容器构建
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Create a new image from a container’s changes
-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
$ docker run -it -p 80 –name commit-test ubuntu /bin/bash
$ docker commit -a ‘jasonote’ -m ‘test01’ commit-test jasonote/commit-test01
$ docker run -d –name commit-test02 jasonote/commit-test01 ls al
* docker build 通过Dockerfile 文件构建
1,创建Dockerfile
#File Dockerfile
FROM ubuntu:14.04
MAINTAINER Jasonote “Jasonote@163.com”
RUN apt-get update
RUN apt-get install -y nginx
EXPOSE 80
2,使用$ docker build 命令
docker build [OPTIONS] PATH | URL | –
Build a new image from the source code at PATH
-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

$ docker build -t=”jasonote/test02″ .

Dockerfile 指令

指令格式
#Comment
INSTRUCTION argument

* FROM
FROM<image>
FROM<image:版本>
* MAINTAINER
MAINTAINER<name>
MAINTAINER<name email>
指定镜像的作者信息,包含镜像的所有者和联系信息
* RUN
指定当前镜像中运行的命令
RUN<command> (shell模式)
/bin/sh -c command
RUN [“executable”,”param1″,”param2″] (exec模式)
RUN [“/bin/bash”, “-c”, “echo hello”]
* EXPOSE
指定运行该镜像的容器使用的端口
EXPOSE <port>[<port>…]

* CMD
CMD [“executable”,”param1″,”param2″] (exec模式)
CMD command param1 param2 (shell模式)
$ docker run -p 80 –name cmd_test1 -d jasonote/test_01

* ENTERYPOINT
ENTERYPOINT [“executable”,”param1″,”param2″] (exec模式)
ENTERYPOINT command param1 param2 (shell模式)
可以使用docker run –entrypoint 覆盖
* ADD
ADD<src>…<dest>
ADD[“<src>”…”<dest>”] (适用于文件路径中有空格的情况)
* COPY
COPY<src>…<dest>
COPY[“<src>”…”<dest>”] (适用于文件路径中有空格的情况)

ADD vs COPY
ADD 包含类似tar的解压功能
如果单纯复制文件,Docker推荐使用COPY
* VOLUME
VOLUME[“/data”]
* WORKDIR
WORKDIR /path/to/workdir
* ENV
ENV <key><value>
ENV <key>=<value>…
* USER
USER daemon
USER nginx
USER user
USER user:group
USER user:gid
* ONBUILD
ONBUILD[INSTRUCTION]
镜像触发器
当一个镜像被其他镜像作为基础镜像时执行
会在构建过程中插入指令

Dockerfile 构建过程
1,从基础镜像运行一个容器
2,执行一个指令,对容器作出修改
3,执行类似docker commit 的操作,提交一个新的镜像层
4,再基于刚提交的镜像运行一个新容器
5,执行Dockerfile中的下一条指令,直到所有执行完毕

不使用缓存
$ docker build –no-cache
查看镜像构建的过程
$ docker history [image]

Jason.wang

When you find your talent can't afford to be ambitious, be quiet and study !

You may also like...