Docker学习(二)
November 19, 2019 Docker 访问: 23 次
Dockerfile
Dockerfile是用来构建镜像的文本文件,其中包含一条一条的指令
格式
FROM
用来指定基础镜像,必须放在Dockerfile的第一行,如:FROM ubuntu:16.04
服务类镜像:nginx、redis、mongo、mysql、httpd、php、tomcat 等
操作系统镜像:ubuntu、debian、centos、fedora、alpine 等
空镜像:scratch
RUN
RUN是用来执行系统命令的
两种格式:
- shell命令格式。直接在RUN后加上要执行的命令即可
eg: RUN touch radish.txt
- exec命令格式。不推荐使用。
eg: EXEC ['touch',"radish.txt"]
需要注意的是,Dockerfile中的每一条指令都会建立一层,所以在写Dockerfile的时候尽量合并
eg:
FROM ubuntu:16.04
RUN touch radish.txt \
&& rm radish.txt
注释
在dockerfile中注释符用#
初次通过使用Dockerfile
FROM ubuntu:16.04
RUN echo "Hacked by Radish..." > /var/www/html/index.php
在dockerfile的目录下执行docker build -t docker_test:this_is_test .
执行后会发现多了一个新的docker镜像,也就是你所建立的
镜像构建上下文(context)
上面利用dockerfile来创建镜像的时候用的命令后面有一个.
,不要以为这个代表的是dockerfile的目录,其实这个是上下文的目录
当你执行docker build的时候,docker程序会把你指定的上下文目录下的文件全部打包发送到docker中,供docker使用,如,复制本机文件到docker里面
在dockerfile中复制文件的时候,只能是上下文目录下的,不能超越上下文目录,否则会找不到
如果上下文目录下存在不想让docker打包的文件,可以利用.dockerignore
来进行限制,格式如下
filename.txt
filename.php
...
docker build
➜ ~ docker build --help
Usage: docker build [OPTIONS] PATH | URL | -
Build an image from a Dockerfile
Options:
--add-host list Add a custom host-to-IP mapping (host:ip)
--build-arg list Set build-time variables
--cache-from strings Images to consider as cache sources
--cgroup-parent string Optional parent cgroup for the container
--compress Compress the build context using gzip
--cpu-period int Limit the CPU CFS (Completely Fair Scheduler) period
--cpu-quota int Limit the CPU CFS (Completely Fair Scheduler) quota
-c, --cpu-shares int CPU shares (relative weight)
--cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)
--cpuset-mems string MEMs in which to allow execution (0-3, 0,1)
--disable-content-trust Skip image verification (default true)
-f, --file string Name of the Dockerfile (Default is 'PATH/Dockerfile')
--force-rm Always remove intermediate containers
--iidfile string Write the image ID to the file
--isolation string Container isolation technology
--label list Set metadata for an image
-m, --memory bytes Memory limit
--memory-swap bytes Swap limit equal to memory plus swap: '-1' to enable unlimited swap
--network string Set the networking mode for the RUN instructions during build (default "default")
--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 Remove intermediate containers after a successful build (default true)
--security-opt strings Security options
--shm-size bytes Size of /dev/shm
-t, --tag list Name and optionally a tag in the 'name:tag' format
--target string Set the target build stage to build.
--ulimit ulimit Ulimit options (default [])
➜ ~
-f
:指定dockerfile的位置-q
:只输出ID-t
:指定docker name和tag,格式:name:tag
默认情况下会在上下文的目录下找Dockerfile文件