docker file

docker file簡(jiǎn)介

Dockerfile 是由一個(gè)個(gè)的指令組成,是用于表示創(chuàng)建一個(gè)鏡像文件的過(guò)程。

docker file 詳解

環(huán)境變量env

echo ${NAME:-tom}? //沒(méi)值顯示默認(rèn)值

tom

NAME=test

echo ${NAME:-tom} //有值顯示設(shè)置的值

test

echo ${NAME:+tom}? //只要有值則顯示tom

tom

NAME=test

tom

unset NAME

echo ${NAME:+tom} //沒(méi)值顯示空


Dockerfile 指令

FROM命令用法說(shuō)明

FROM <image>:tag

第一個(gè)指令必須是FROM,其指定一個(gè)構(gòu)建鏡像的基礎(chǔ)源鏡像,如果本地沒(méi)有就會(huì)從公共庫(kù)中拉取,沒(méi)有指定鏡像的標(biāo)簽會(huì)使用默認(rèn)的latest標(biāo)簽,可以出現(xiàn)多次,如果需要在一個(gè)Dockerfile中構(gòu)建多個(gè)鏡像。

MAINTAINER命令用法說(shuō)明

MAINTAINER <name> <email>

描述鏡像的創(chuàng)建者,名稱和郵箱

RUN命令用法說(shuō)明

RUN "command" "param1" "param2"

RUN命令是一個(gè)常用的命令,執(zhí)行完成之后會(huì)成為一個(gè)新的鏡像,這里也是指鏡像的分層構(gòu)建。一句RUN就是一層,也相當(dāng)于一個(gè)版本。可以通過(guò)&符號(hào)連接多個(gè)RUN語(yǔ)句。RUN后面的必須是雙引號(hào)不能是單引號(hào)(沒(méi)引號(hào)貌似也不要緊),command是不會(huì)調(diào)用shell的,所以也不會(huì)繼承相應(yīng)變量,要查看輸入RUN?"sh" "-c" "echo" "$HOME",而不是RUN? "echo" "$HOME"

CMD命令用法說(shuō)明

CMD command param1 param2

CMD在Dockerfile中只能出現(xiàn)一次,有多個(gè),只有最后一個(gè)會(huì)有效。其作用是在啟動(dòng)容器的時(shí)候提供一個(gè)默認(rèn)的命令項(xiàng)。如果用戶執(zhí)行docker? run的時(shí)候提供了命令項(xiàng),就會(huì)覆蓋掉這個(gè)命令。沒(méi)提供就會(huì)使用構(gòu)建時(shí)的命令

EXPOSE命令用法說(shuō)明

EXPOSE <port> [<port>...]

Docker服務(wù)器容器對(duì)外映射的容器端口號(hào),在docker? ?run -p的時(shí)候生效

ENV命令用法說(shuō)明

EVN <key> <value> 只能設(shè)置一個(gè)

EVN <key>=<value>允許一次設(shè)置多

設(shè)置容器的環(huán)境變量,可以讓其后面的RUN命令使用,容器運(yùn)行的時(shí)候這個(gè)變量也會(huì)保留

ADD命令用法說(shuō)明

ADD <src> <dest>?

復(fù)制本機(jī)文件或目錄或遠(yuǎn)程文件,添加到指定的容器目錄,支持GO的正則模糊匹配。路徑是絕對(duì)路徑,不存在會(huì)自動(dòng)創(chuàng)建。如果源是一個(gè)目錄,只會(huì)復(fù)制目錄下的內(nèi)容,目錄本身不會(huì)復(fù)制。ADD命令會(huì)將復(fù)制的壓縮文件夾自動(dòng)解壓,這也是與COPY命令最大的不同

COPY命令用法說(shuō)明

COPY <src> <dest>

COPY除了不能自動(dòng)解壓,也不能復(fù)制網(wǎng)絡(luò)文件。其它功能和ADD相同

ENTRYPOINT命令用法說(shuō)明

ENTRYPOINT "command" "param1" "param2"

這個(gè)命令和CMD命令一樣,唯一的區(qū)別是不能被docker? run命令的執(zhí)行命令覆蓋,如果要覆蓋需要帶上選項(xiàng)--entrypoint,如果有多個(gè)選項(xiàng),只有最后一個(gè)會(huì)生效

VOLUME命令用法說(shuō)明

VOLUME ["path"]

在主機(jī)上創(chuàng)建一個(gè)掛載,掛載到容器的指定路徑。docker run? -v命令也能完成這個(gè)操作,而且更強(qiáng)大。這個(gè)命令不能指定主機(jī)的需要掛載到容器的文件夾路徑。但docker run -v可以,而且其還可以掛載數(shù)據(jù)容器

USER命令用法說(shuō)明

USER daemon

指定運(yùn)行容器時(shí)的用戶名或UID,后續(xù)的RUN、CMD、ENTRYPOINT也會(huì)使用指定的用戶運(yùn)行命令

WORKDIR命令用法說(shuō)明

WORKDIR path

為RUN、CMD、ENTRYPOINT指令配置工作目錄??梢允褂枚鄠€(gè)WORKDIR指令,后續(xù)參數(shù)如果是相對(duì)路徑,則會(huì)基于之前的命令指定的路徑。如:WORKDIR? /home  WORKDIR test? 。最終的路徑就是/home/test。path路徑也可以是環(huán)境變量,比如有環(huán)境變量HOME=/home,WORKDIR? $HOME/test也就是/home/test

docker file基于tomcat例子

FROM davidcaste/alpine-tomcat:jdk8tomcat8

MAINTAINER sknife <sknife123@google.com>

#RUN mkdir /webapps

ADD *.war /opt/tomcat/webapps/app.war

CMD ["/opt/tomcat/bin/catalina.sh", "run"]

鏡像構(gòu)建實(shí)踐

提供文件index.html

<h1>busybox test</h1>

編寫Dockerfile

每一條指令都會(huì)生成一個(gè)鏡像層

#Description: test image

FROM busybox:latest

MAINTAINER "sknife <sknife666@gmail.com>"

COPY index.html /data/web/html/

執(zhí)行構(gòu)建命令

docker build -h

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 [])

docker build -t testhttpd:v1 ./

Sending build context to Docker daemon 3.072kB

Step 1/3 : FROM busybox:latest

---> 6d5fcfe5ff17

Step 2/3 : MAINTAINER "sknife <sknife666@gmail.com>"

---> Running in 7cfd8081a9a2

Removing intermediate container 7cfd8081a9a2

---> 01ae95e85baf

Step 3/3 : COPY index.html /data/web/html/

---> 355adbec9342

Successfully built 355adbec9342

Successfully tagged testhttpd:v1

docker images

REPOSITORY TAG? ? ? ? ? ? ? IMAGE ID? ? ? ? ? ? ? ?CREATED? ? ? ? ? ? ? ?SIZE

testhttpd? ? ? ? ? ? v1? ? ? ? ? ? ? ? 355adbec9342? ? ? ? 35 seconds ago? ? ? 1.22MB

docker run --name testhttpd --rm testhttpd:v1 cat /data/web/html/index.html

<h1>busybox test</h1>

使用COPY指令

cp -r /etc/yum.repos.d/ .

vi Dockerfile? //在原有文件后加一行

COPY yum.repos.d /etc/yum.repos.d/

docker build -t testhttpd:v2 ./? ? //重新構(gòu)建

Sending build context to Docker daemon 13.31kB

Step 1/4 : FROM busybox:latest

---> 6d5fcfe5ff17

Step 2/4 : MAINTAINER "sknife <sknife666@gmail.com>"

---> Using cache

---> 01ae95e85baf

Step 3/4 : COPY index.html /data/web/html/

---> Using cache

---> 355adbec9342

Step 4/4 : COPY yum.repos.d /etc/yum.repos.d/? ?//新加了一層

---> 77dbb0dcb097

Successfully built 77dbb0dcb097

Successfully tagged testhttpd:v2

docker run --name testhttpd --rm testhttpd:v2 ls /etc/yum.repos.d/

CentOS-Base.repo

CentOS-Epel.repo

docker-ce.repo

epel-testing.repo

epel.repo

對(duì)比一下:v1版本鏡像無(wú)yum.repos.d目錄

docker run --name testhttpd --rm testhttpd:v1 ls /etc/

group

hostname

hosts

localtime

mtab

network

passwd

resolv.conf

shadow

將最后一行改一下目錄名:不存在的目錄將新建目錄,并將目錄中的文件放入新創(chuàng)建的目錄中

COPY yum.repos.d /etc/testyum/

docker build -t testhttpd:v3 ./

docker run --name testhttpd --rm testhttpd:v3 ls /etc/

testyum

將最后一行改成已存在的目錄名:將目錄中的文件放入已存在的目錄中

COPY yum.repos.d /etc/nework/

docker build -t testhttpd:v4 ./

docker run --name testhttpd --rm testhttpd:v4 ls /etc/

group

hostname

hosts

localtime

mtab

network

passwd

resolv.conf

shadow

docker run --name testhttpd --rm testhttpd:v4 ls -al /etc/network

CentOS-Base.repo

CentOS-Epel.repo

docker-ce.repo

epel-testing.repo

epel.repo

if-down.d

if-post-down.d

if-pre-up.d

if-up.d

使用ADD指令

ADD http://nginx.org/download/nginx-1.15.2.tar.gz /usr/local/src

docker build -t testhttpd:v5 ./

Sending build context to Docker daemon 13.31kB

Step 1/5 : FROM busybox:latest

---> 6d5fcfe5ff17

Step 2/5 : MAINTAINER "sknife <sknife666@gmail.com>"

---> Using cache

---> 01ae95e85baf

Step 3/5 : COPY index.html /data/web/html/

---> Using cache

---> 355adbec9342

Step 4/5 : COPY yum.repos.d /etc/yum.repos.d

---> fcf0ad0662ce

Step 5/5 : ADD http://nginx.org/download/nginx-1.15.2.tar.gz /usr/local/src

Downloading [==================================================>]? 1.026MB/1.026MB

---> d281e4321278

Successfully built d281e4321278

Successfully tagged testhttpd:v5

docker run --name testhttpd --rm testhttpd:v5 ls -al /usr/local/src

drwxr-xr-x 8 1001 1001 4096 Jul 24 2018 nginx-1.15.2

手工下載nginx壓縮包

wget?http://nginx.org/download/nginx-1.15.2.tar.gz

vi Dockerfile? //改一下最后一行,從本地拷貝

ADD nginx-1.15.2.tar.gz /usr/local/src

docker build -t testhttpd:v6 ./

docker run --name testhttpd --rm testhttpd:v6 ls -al /usr/local/src

使用WORKDIR

vi Dockerfile

WORKDIR /usr/local

ADD nginx-1.15.2.tar.gz ./src

docker build -t testhttpd:v7 ./

docker run --name testhttpd --rm testhttpd:v7 ls -al /usr/local/src

drwxr-xr-x 8 1001 1001 4096 Jul 24 2018 nginx-1.15.2

vi Dockerfile

WORKDIR /usr/local

WORKDIR src

ADD nginx-1.15.2.tar.gz ./

docker build -t testhttpd:v7 ./

Sending build context to Docker daemon 1.04MB

Step 1/7 : FROM busybox:latest

---> 6d5fcfe5ff17

Step 2/7 : MAINTAINER "sknife <sknife666@gmail.com>"

---> Using cache

---> 01ae95e85baf

Step 3/7 : COPY index.html /data/web/html/

---> Using cache

---> 355adbec9342

Step 4/7 : COPY yum.repos.d /etc/yum.repos.d

---> Using cache

---> fcf0ad0662ce

Step 5/7 : WORKDIR /usr/local

---> Using cache

---> d67f61741867

Step 6/7 : WORKDIR src

---> Running in ef90feec6651

Removing intermediate container ef90feec6651

---> 8ae879880c1a

Step 7/7 : ADD nginx-1.15.2.tar.gz ./

---> 73c03474cc0b

Successfully built 73c03474cc0b

Successfully tagged testhttpd:v7

docker run --name testhttpd --rm testhttpd:v7 ls -al /usr/local/src

drwxr-xr-x 8 1001 1001 4096 Jul 24 2018 nginx-1.15.2

docker exec -it testhttpd sh? //直接進(jìn)入容器的話,就是設(shè)置的當(dāng)前工作目錄

/usr/local/src # ls

nginx-1.15.2

使用VOLUME指令

VOLUME /data/mysql

docker build -t testhttpd:v8 ./

Sending build context to Docker daemon 1.04MB

Step 1/8 : FROM busybox:latest

---> 6d5fcfe5ff17

Step 2/8 : MAINTAINER "sknife <sknife666@gmail.com>"

---> Using cache

---> 01ae95e85baf

Step 3/8 : COPY index.html /data/web/html/

---> Using cache

---> 355adbec9342

Step 4/8 : COPY yum.repos.d /etc/yum.repos.d

---> Using cache

---> fcf0ad0662ce

Step 5/8 : WORKDIR /usr/local

---> Using cache

---> d67f61741867

Step 6/8 : WORKDIR src

---> Using cache

---> 8ae879880c1a

Step 7/8 : ADD nginx-1.15.2.tar.gz ./

---> Using cache

---> 73c03474cc0b

Step 8/8 : VOLUME /data/mysql

---> Running in 513715511e75

Removing intermediate container 513715511e75

---> 88e4b4860698

Successfully built 88e4b4860698

Successfully tagged testhttpd:v8

docker run --name testhttpd --rm testhttpd:v8 mount

/dev/vda1 on /data/mysql type ext4 (rw,noatime,data=ordered)

[root@VM_0_10_centos docker-file]# docker run --name testhttpd --rm testhttpd:v8 mount

rootfs on / type rootfs (rw)

overlay on / type overlay (rw,relatime,lowerdir=/var/lib/docker/overlay2/l/EWVRZUPD5C7AFBN3KLNP4TYDTB:/var/lib/docker/overlay2/l/XKLV3O2S26Z5XALZAUWBQN5LG6:/var/lib/docker/overlay2/l/6L3Q45LGX5T5QVF7GFC7MWCJ2K:/var/lib/docker/overlay2/l/QNLV4SQZFQSZKBUMNT3RH4EQ2A:/var/lib/docker/overlay2/l/KBG2I3G2X5RKJFB7M7KM7W3T2Q:/var/lib/docker/overlay2/l/ZOE5NVIM2BBPIJOXWGD73U6KDW:/var/lib/docker/overlay2/l/T7FN54GAW364KH4I6WCMED27BT,upperdir=/var/lib/docker/overlay2/08d53cf05e2480d0013fb8b15008960c5be63aadb54365194de04c16e63fe9aa/diff,workdir=/var/lib/docker/overlay2/08d53cf05e2480d0013fb8b15008960c5be63aadb54365194de04c16e63fe9aa/work)

proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)

tmpfs on /dev type tmpfs (rw,nosuid,size=65536k,mode=755)

devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=666)

sysfs on /sys type sysfs (ro,nosuid,nodev,noexec,relatime)

tmpfs on /sys/fs/cgroup type tmpfs (ro,nosuid,nodev,noexec,relatime,mode=755)

cgroup on /sys/fs/cgroup/systemd type cgroup (ro,nosuid,nodev,noexec,relatime,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd)

cgroup on /sys/fs/cgroup/perf_event type cgroup (ro,nosuid,nodev,noexec,relatime,perf_event)

cgroup on /sys/fs/cgroup/freezer type cgroup (ro,nosuid,nodev,noexec,relatime,freezer)

cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (ro,nosuid,nodev,noexec,relatime,cpuacct,cpu)

cgroup on /sys/fs/cgroup/memory type cgroup (ro,nosuid,nodev,noexec,relatime,memory)

cgroup on /sys/fs/cgroup/pids type cgroup (ro,nosuid,nodev,noexec,relatime,pids)

cgroup on /sys/fs/cgroup/devices type cgroup (ro,nosuid,nodev,noexec,relatime,devices)

cgroup on /sys/fs/cgroup/hugetlb type cgroup (ro,nosuid,nodev,noexec,relatime,hugetlb)

cgroup on /sys/fs/cgroup/cpuset type cgroup (ro,nosuid,nodev,noexec,relatime,cpuset)

cgroup on /sys/fs/cgroup/net_cls,net_prio type cgroup (ro,nosuid,nodev,noexec,relatime,net_prio,net_cls)

cgroup on /sys/fs/cgroup/blkio type cgroup (ro,nosuid,nodev,noexec,relatime,blkio)

mqueue on /dev/mqueue type mqueue (rw,nosuid,nodev,noexec,relatime)

shm on /dev/shm type tmpfs (rw,nosuid,nodev,noexec,relatime,size=65536k)

/dev/vda1 on /data/mysql type ext4 (rw,noatime,data=ordered)

/dev/vda1 on /etc/resolv.conf type ext4 (rw,noatime,data=ordered)

/dev/vda1 on /etc/hostname type ext4 (rw,noatime,data=ordered)

/dev/vda1 on /etc/hosts type ext4 (rw,noatime,data=ordered)

proc on /proc/bus type proc (ro,relatime)

proc on /proc/fs type proc (ro,relatime)

proc on /proc/irq type proc (ro,relatime)

proc on /proc/sys type proc (ro,relatime)

proc on /proc/sysrq-trigger type proc (ro,relatime)

tmpfs on /proc/acpi type tmpfs (ro,relatime)

tmpfs on /proc/kcore type tmpfs (rw,nosuid,size=65536k,mode=755)

tmpfs on /proc/keys type tmpfs (rw,nosuid,size=65536k,mode=755)

tmpfs on /proc/timer_list type tmpfs (rw,nosuid,size=65536k,mode=755)

tmpfs on /proc/timer_stats type tmpfs (rw,nosuid,size=65536k,mode=755)

tmpfs on /proc/sched_debug type tmpfs (rw,nosuid,size=65536k,mode=755)

tmpfs on /proc/scsi type tmpfs (ro,relatime)

tmpfs on /sys/firmware type tmpfs (ro,relatime)

可以直接使用grep mysql過(guò)濾出來(lái)

docker run --name testhttpd --rm testhttpd:v8 mount|grep mysql

/dev/vda1 on /data/mysql type ext4 (rw,noatime,data=ordered)

docker run --name testhttpd --rm testhttpd:v8 sleep 3600

docker inspect testhttpd

"Mounts": [

? ? ? ? ? ? {

? ? ? ? ? ? ? ? "Type": "volume",

? ? ? ? ? ? ? ? "Name": "0faa2c9f49db5e15eb96878c9f60d7ce50c5f4ce8f5a668993dee9297ff7d939",

? ? ? ? ? ? ? ? "Source": "/var/lib/docker/volumes/0faa2c9f49db5e15eb96878c9f60d7ce50c5f4ce8f5a668993dee9297ff7d939/_data",

? ? ? ? ? ? ? ? "Destination": "/data/mysql",

? ? ? ? ? ? ? ? "Driver": "local",

? ? ? ? ? ? ? ? "Mode": "",

? ? ? ? ? ? ? ? "RW": true,

? ? ? ? ? ? ? ? "Propagation": ""

? ? ? ? ? ? }

? ? ? ? ],

使用EXPOSE指令

動(dòng)態(tài)綁定宿主機(jī)的端口

EXPOSE 80/tcp

docker build -t testhttpd:v9 ./

Sending build context to Docker daemon 1.04MB

Step 1/9 : FROM busybox:latest

---> 6d5fcfe5ff17

Step 2/9 : MAINTAINER "sknife <sknife666@gmail.com>"

---> Using cache

---> 01ae95e85baf

Step 3/9 : COPY index.html /data/web/html/

---> Using cache

---> 355adbec9342

Step 4/9 : COPY yum.repos.d /etc/yum.repos.d

---> Using cache

---> fcf0ad0662ce

Step 5/9 : WORKDIR /usr/local

---> Using cache

---> d67f61741867

Step 6/9 : WORKDIR src

---> Using cache

---> 8ae879880c1a

Step 7/9 : ADD nginx-1.15.2.tar.gz ./

---> Using cache

---> 73c03474cc0b

Step 8/9 : VOLUME /data/mysql

---> Using cache

---> 88e4b4860698

Step 9/9 : EXPOSE 80/tcp

---> Running in 602273e7a272

Removing intermediate container 602273e7a272

---> 8059c8690a95

Successfully built 8059c8690a95

Successfully tagged testhttpd:v9

docker run --name testhttpd --rm testhttpd:v9 /bin/httpd -f -h /data/web/html

curl 172.18.0.2

<h1>busybox test</h1>

docker port testhttpd //沒(méi)有暴露端口


加-P暴露任何端口

docker run --name testhttpd --rm -P testhttpd:v9 /bin/httpd -f -h /data/web/html

docker port testhttpd

80/tcp -> 0.0.0.0:32768

curl localhost:32768

<h1>busybox test</h1>

使用ENV指令

vi Dockerfile? ?//只配置一個(gè)ENV,用空格分隔

ENV DOC_ROOT /data/web/html/

COPY index.html $DOC_ROOT

docker build -t?testhttpd:v10 ./

Sending build context to Docker daemon 1.04MB

Step 1/10 : FROM busybox:latest

---> 6d5fcfe5ff17

Step 2/10 : MAINTAINER "sknife <sknife666@gmail.com>"

---> Using cache

---> 01ae95e85baf

Step 3/10 : ENV DOC_ROOT /data/web/html/

---> Running in 1827e66be015

Removing intermediate container 1827e66be015

---> 2153578b27c9

Step 4/10 : COPY index.html ${DOC_ROOT:-/data/web/html/}

---> 400df82a8a20

Step 5/10 : COPY yum.repos.d /etc/yum.repos.d

---> 117c6b72904f

Step 6/10 : WORKDIR /usr/local

---> Running in 0995fa6e6614

Removing intermediate container 0995fa6e6614

---> 9233ae06b56d

Step 7/10 : WORKDIR src

---> Running in 4c6eb91114d2

Removing intermediate container 4c6eb91114d2

---> 5723299ea4f1

Step 8/10 : ADD nginx-1.15.2.tar.gz ./

---> da05e6a56d41

Step 9/10 : VOLUME /data/mysql

---> Running in 2e1560259713

Removing intermediate container 2e1560259713

---> a77d85552a35

Step 10/10 : EXPOSE 80/tcp

---> Running in 0dc390381a32

Removing intermediate container 0dc390381a32

---> d83da711f924

Successfully built d83da711f924

Successfully tagged testhttpd:v10

vi Dockerfile? //?//配置多個(gè)ENV,用=分隔;多行用\分隔

ENV DOC_ROOT=/data/web/html/ \

? ? DOC_SERVER="nginx-1.15.2.tar.gz" \

? ? DOC_DB=/data/mysql

COPY index.html ${DOC_ROOT:-/data/web/html/}

ADD $DOC_SERVER ./

VOLUME $DOC_DB

docker build -t?testhttpd:v10 ./

Sending build context to Docker daemon 1.04MB

Step 1/10 : FROM busybox:latest

---> 6d5fcfe5ff17

Step 2/10 : MAINTAINER "sknife <sknife666@gmail.com>"

---> Using cache

---> 01ae95e85baf

Step 3/10 : ENV DOC_ROOT=/data/web/html/? ? DOC_SERVER="nginx-1.15.2.tar.gz"? ? DOC_DB=/data/mysql

---> Running in 217f5ebeb2ea

Removing intermediate container 217f5ebeb2ea

---> 1b8bd42dc527

Step 4/10 : COPY index.html ${DOC_ROOT:-/data/web/html/}

---> cf9999541535

Step 5/10 : COPY yum.repos.d /etc/yum.repos.d

---> 996e589f1b9c

Step 6/10 : WORKDIR /usr/local

---> Running in 2655b2f31921

Removing intermediate container 2655b2f31921

---> fd6b2bf3e69b

Step 7/10 : WORKDIR src

---> Running in 08105fa7812a

Removing intermediate container 08105fa7812a

---> 56061794d6ac

Step 8/10 : ADD $DOC_SERVER ./

---> d6118eff4822

Step 9/10 : VOLUME $DOC_DB

---> Running in fca10268efff

Removing intermediate container fca10268efff

---> 9d3fba25bb6f

Step 10/10 : EXPOSE 80/tcp

---> Running in 177b68d5ebef

Removing intermediate container 177b68d5ebef

---> 692fd83869b6

Successfully built 692fd83869b6

Successfully tagged testhttpd:v10

docker run --name testhttpd --rm -P testhttpd:v10 ls /usr/local/src

nginx-1.15.2

docker run --name testhttpd --rm -P testhttpd:v10 ls /data/web/html

index.html

docker run --name testhttpd --rm -P testhttpd:v10 printenv

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

HOSTNAME=8e8058a20817

DOC_ROOT=/data/web/html/

DOC_SERVER=nginx-1.15.2.tar.gz

DOC_DB=/data/mysql

HOME=/root

docker run 參數(shù)設(shè)置

docker run --help

Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Run a command in a new container

Options:

? ? ? --add-host list? ? ? ? ? ? ? ? ? Add a custom host-to-IP mapping (host:ip)

? -a, --attach list? ? ? ? ? ? ? ? ? ? Attach to STDIN, STDOUT or STDERR

? ? ? --blkio-weight uint16? ? ? ? ? ? Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)

? ? ? --blkio-weight-device list? ? ? Block IO weight (relative device weight) (default [])

? ? ? --cap-add list? ? ? ? ? ? ? ? ? Add Linux capabilities

? ? ? --cap-drop list? ? ? ? ? ? ? ? ? Drop Linux capabilities

? ? ? --cgroup-parent string? ? ? ? ? Optional parent cgroup for the container

? ? ? --cidfile string? ? ? ? ? ? ? ? Write the container ID to the file

? ? ? --cpu-period int? ? ? ? ? ? ? ? Limit CPU CFS (Completely Fair Scheduler) period

? ? ? --cpu-quota int? ? ? ? ? ? ? ? ? Limit CPU CFS (Completely Fair Scheduler) quota

? ? ? --cpu-rt-period int? ? ? ? ? ? ? Limit CPU real-time period in microseconds

? ? ? --cpu-rt-runtime int? ? ? ? ? ? Limit CPU real-time runtime in microseconds

? -c, --cpu-shares int? ? ? ? ? ? ? ? CPU shares (relative weight)

? ? ? --cpus decimal? ? ? ? ? ? ? ? ? Number of CPUs

? ? ? --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)

? -d, --detach? ? ? ? ? ? ? ? ? ? ? ? Run container in background and print container ID

? ? ? --detach-keys string? ? ? ? ? ? Override the key sequence for detaching a container

? ? ? --device list? ? ? ? ? ? ? ? ? ? Add a host device to the container

? ? ? --device-cgroup-rule list? ? ? ? Add a rule to the cgroup allowed devices list

? ? ? --device-read-bps list? ? ? ? ? Limit read rate (bytes per second) from a device (default [])

? ? ? --device-read-iops list? ? ? ? ? Limit read rate (IO per second) from a device (default [])

? ? ? --device-write-bps list? ? ? ? ? Limit write rate (bytes per second) to a device (default [])

? ? ? --device-write-iops list? ? ? ? Limit write rate (IO per second) to a device (default [])

? ? ? --disable-content-trust? ? ? ? ? Skip image verification (default true)

? ? ? --dns list? ? ? ? ? ? ? ? ? ? ? Set custom DNS servers

? ? ? --dns-option list? ? ? ? ? ? ? ? Set DNS options

? ? ? --dns-search list? ? ? ? ? ? ? ? Set custom DNS search domains

? ? ? --domainname string? ? ? ? ? ? ? Container NIS domain name

? ? ? --entrypoint string? ? ? ? ? ? ? Overwrite the default ENTRYPOINT of the image

? -e, --env list? ? ? ? ? ? ? ? ? ? ? Set environment variables

? ? ? --env-file list? ? ? ? ? ? ? ? ? Read in a file of environment variables

? ? ? --expose list? ? ? ? ? ? ? ? ? ? Expose a port or a range of ports

? ? ? --gpus gpu-request? ? ? ? ? ? ? GPU devices to add to the container ('all' to pass all GPUs)

? ? ? --group-add list? ? ? ? ? ? ? ? Add additional groups to join

? ? ? --health-cmd string? ? ? ? ? ? ? Command to run to check health

? ? ? --health-interval duration? ? ? Time between running the check (ms|s|m|h) (default 0s)

? ? ? --health-retries int? ? ? ? ? ? Consecutive failures needed to report unhealthy

? ? ? --health-start-period duration? Start period for the container to initialize before starting health-retries countdown

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? (ms|s|m|h) (default 0s)

? ? ? --health-timeout duration? ? ? ? Maximum time to allow one check to run (ms|s|m|h) (default 0s)

? ? ? --help? ? ? ? ? ? ? ? ? ? ? ? ? Print usage

? -h, --hostname string? ? ? ? ? ? ? ? Container host name

? ? ? --init? ? ? ? ? ? ? ? ? ? ? ? ? Run an init inside the container that forwards signals and reaps processes

? -i, --interactive? ? ? ? ? ? ? ? ? ? Keep STDIN open even if not attached

? ? ? --ip string? ? ? ? ? ? ? ? ? ? ? IPv4 address (e.g., 172.30.100.104)

? ? ? --ip6 string? ? ? ? ? ? ? ? ? ? IPv6 address (e.g., 2001:db8::33)

? ? ? --ipc string? ? ? ? ? ? ? ? ? ? IPC mode to use

? ? ? --isolation string? ? ? ? ? ? ? Container isolation technology

? ? ? --kernel-memory bytes? ? ? ? ? ? Kernel memory limit

? -l, --label list? ? ? ? ? ? ? ? ? ? Set meta data on a container

? ? ? --label-file list? ? ? ? ? ? ? ? Read in a line delimited file of labels

? ? ? --link list? ? ? ? ? ? ? ? ? ? ? Add link to another container

? ? ? --link-local-ip list? ? ? ? ? ? Container IPv4/IPv6 link-local addresses

? ? ? --log-driver string? ? ? ? ? ? ? Logging driver for the container

? ? ? --log-opt list? ? ? ? ? ? ? ? ? Log driver options

? ? ? --mac-address string? ? ? ? ? ? Container MAC address (e.g., 92:d0:c6:0a:29:33)

? -m, --memory bytes? ? ? ? ? ? ? ? ? Memory limit

? ? ? --memory-reservation bytes? ? ? Memory soft limit

? ? ? --memory-swap bytes? ? ? ? ? ? ? Swap limit equal to memory plus swap: '-1' to enable unlimited swap

? ? ? --memory-swappiness int? ? ? ? ? Tune container memory swappiness (0 to 100) (default -1)

? ? ? --mount mount? ? ? ? ? ? ? ? ? ? Attach a filesystem mount to the container

? ? ? --name string? ? ? ? ? ? ? ? ? ? Assign a name to the container

? ? ? --network network? ? ? ? ? ? ? ? Connect a container to a network

? ? ? --network-alias list? ? ? ? ? ? Add network-scoped alias for the container

? ? ? --no-healthcheck? ? ? ? ? ? ? ? Disable any container-specified HEALTHCHECK

? ? ? --oom-kill-disable? ? ? ? ? ? ? Disable OOM Killer

? ? ? --oom-score-adj int? ? ? ? ? ? ? Tune host's OOM preferences (-1000 to 1000)

? ? ? --pid string? ? ? ? ? ? ? ? ? ? PID namespace to use

? ? ? --pids-limit int? ? ? ? ? ? ? ? Tune container pids limit (set -1 for unlimited)

? ? ? --privileged? ? ? ? ? ? ? ? ? ? Give extended privileges to this container

? -p, --publish list? ? ? ? ? ? ? ? ? Publish a container's port(s) to the host

? -P, --publish-all? ? ? ? ? ? ? ? ? ? Publish all exposed ports to random ports

? ? ? --read-only? ? ? ? ? ? ? ? ? ? ? Mount the container's root filesystem as read only

? ? ? --restart string? ? ? ? ? ? ? ? Restart policy to apply when a container exits (default "no")

? ? ? --rm? ? ? ? ? ? ? ? ? ? ? ? ? ? Automatically remove the container when it exits

? ? ? --runtime string? ? ? ? ? ? ? ? Runtime to use for this container

? ? ? --security-opt list? ? ? ? ? ? ? Security Options

? ? ? --shm-size bytes? ? ? ? ? ? ? ? Size of /dev/shm

? ? ? --sig-proxy? ? ? ? ? ? ? ? ? ? ? Proxy received signals to the process (default true)

? ? ? --stop-signal string? ? ? ? ? ? Signal to stop a container (default "SIGTERM")

? ? ? --stop-timeout int? ? ? ? ? ? ? Timeout (in seconds) to stop a container

? ? ? --storage-opt list? ? ? ? ? ? ? Storage driver options for the container

? ? ? --sysctl map? ? ? ? ? ? ? ? ? ? Sysctl options (default map[])

? ? ? --tmpfs list? ? ? ? ? ? ? ? ? ? Mount a tmpfs directory

? -t, --tty? ? ? ? ? ? ? ? ? ? ? ? ? ? Allocate a pseudo-TTY

? ? ? --ulimit ulimit? ? ? ? ? ? ? ? ? Ulimit options (default [])

? -u, --user string? ? ? ? ? ? ? ? ? ? Username or UID (format: <name|uid>[:<group|gid>])

? ? ? --userns string? ? ? ? ? ? ? ? ? User namespace to use

? ? ? --uts string? ? ? ? ? ? ? ? ? ? UTS namespace to use

? -v, --volume list? ? ? ? ? ? ? ? ? ? Bind mount a volume

? ? ? --volume-driver string? ? ? ? ? Optional volume driver for the container

? ? ? --volumes-from list? ? ? ? ? ? ? Mount volumes from the specified container(s)

? -w, --workdir string? ? ? ? ? ? ? ? Working directory inside the container

docker run --name testhttpd --rm -P -e DOC_SERVER="tomcat" testhttpd:v10 printenv

DOC_SERVER=tomcat

使用RUN指令

vi Dockerfile

#Description: test image

FROM busybox:latest

MAINTAINER "sknife <sknife666@gmail.com>"

ENV DOC_ROOT=/data/web/html/ \

? ? DOC_SERVER="nginx-1.15.2.tar.gz" \

? ? DOC_DB=/data/mysql

COPY index.html ${DOC_ROOT:-/data/web/html/}

COPY yum.repos.d /etc/yum.repos.d

ADD http://nginx.org/download/${DOC_SERVER} /usr/local/src/

WORKDIR /usr/local/

#WORKDIR src

#ADD $DOC_SERVER ./

VOLUME $DOC_DB

EXPOSE 80/tcp

RUN cd /usr/local/src && \

? ? tar xf ${DOC_SERVER}

docker build -t?testhttpd:v11 ./

Sending build context to Docker daemon 1.04MB

Step 1/10 : FROM busybox:latest

---> 6d5fcfe5ff17

Step 2/10 : MAINTAINER "sknife <sknife666@gmail.com>"

---> Using cache

---> 01ae95e85baf

Step 3/10 : ENV DOC_ROOT=/data/web/html/? ? DOC_SERVER="nginx-1.15.2.tar.gz"? ? DOC_DB=/data/mysql

---> Using cache

---> 1b8bd42dc527

Step 4/10 : COPY index.html ${DOC_ROOT:-/data/web/html/}

---> Using cache

---> cf9999541535

Step 5/10 : COPY yum.repos.d /etc/yum.repos.d

---> Using cache

---> 996e589f1b9c

Step 6/10 : ADD http://nginx.org/download/${DOC_SERVER} /usr/local/src/

Downloading [==================================================>]? 1.026MB/1.026MB

---> Using cache

---> 530ac4541918

Step 7/10 : WORKDIR /usr/local/

---> Using cache

---> bfae03feb06d

Step 8/10 : VOLUME $DOC_DB

---> Using cache

---> b9805a6eadb0

Step 9/10 : EXPOSE 80/tcp

---> Using cache

---> 6b9045e83464

Step 10/10 : RUN cd /usr/local/src &&? ? tar xf ${DOC_SERVER}

---> Running in 350c175de9ac

Removing intermediate container 350c175de9ac

---> b21080b9b20b

Successfully built b21080b9b20b

Successfully tagged testhttpd:v11

docker run --name testhttpd --rm -P -e DOC_SERVER="tomcat" -it testhttpd:v11 ls /usr/local/src

nginx-1.15.2 nginx-1.15.2.tar.gz

vi Dockerfile

RUN cd /usr/local/src && \

? ? tar xf ${DOC_SERVER} && \

? ? mv nginx-1.15.2 webserver

docker build -t testhttpd:v12 ./

docker run --name testhttpd --rm -P -e DOC_SERVER="webserver" -it testhttpd:v12 ls /usr/local/src

nginx-1.15.2.tar.gz webserver

使用CMD指令

vi Dockerfile

#Description: test image

FROM busybox:latest

MAINTAINER "sknife <sknife666@gmail.com>"

ENV DOC_ROOT=/data/web/html/

RUN mkdir -p $DOC_ROOT && \

? ? echo '<h1>busybox test</h1>' > $DOC_ROOT/index.html

CMD /bin/httpd -f -h ${DOC_ROOT}

docker build -t testhttpd:v12 ./

docker image inspect testhttpd:v12

"Cmd": [

? ? ? ? ? ? ? ? "/bin/sh",

? ? ? ? ? ? ? ? "-c",

? ? ? ? ? ? ? ? "#(nop) ",

? ? ? ? ? ? ? ? "CMD [\"/bin/sh\" \"-c\" \"/bin/httpd -f -h ${DOC_ROOT}\"]"

? ? ? ? ? ? ],

docker run --name testhttpd --rm -P? -it testhttpd:v12?

docker exec -it testhttpd sh

/ # ps

PID? USER? ? TIME? COMMAND

? ? 1 root? ? ? 0:00 /bin/httpd -f -h /data/web/html/

? 11 root? ? ? 0:00 sh

? 16 root? ? ? 0:00 ps

vi Dockerfile

#CMD /bin/httpd -f -h ${DOC_ROOT}

CMD ["/bin/httpd", "-f", "-h ${DOC_ROOT}"]

docker build -t testhttpd:v13 ./

docker image inspect testhttpd:v13

"Cmd": [

? ? ? ? ? ? ? ? "/bin/sh",

? ? ? ? ? ? ? ? "-c",

? ? ? ? ? ? ? ? "{\"/bin/httpd\", \"-f\", \"-h ${DOC_ROOT}\"}"

? ? ? ? ? ? ],

docker run --name testhttpd --rm -P? -it testhttpd:v13

/bin/sh: {/bin/httpd,: not found

vi Dockerfile

#CMD /bin/httpd -f -h ${DOC_ROOT}

CMD ["/bin/sh", "-c", "/bin/httpd", "-f", "-h ${DOC_ROOT}"]

docker build -t testhttpd:v13 ./

docker run --name testhttpd --rm -P -it testhttpd:v13

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 五、Docker 端口映射 無(wú)論如何,這些 ip 是基于本地系統(tǒng)的并且容器的端口非本地主機(jī)是訪問(wèn)不到的。此外,除了...
    R_X閱讀 1,961評(píng)論 0 7
  • 1. docker file是什么 定義:Docker file是用來(lái)構(gòu)建Docker鏡像的構(gòu)建文件,是由一系列的...
    Minority閱讀 771評(píng)論 0 5
  • 五月八日 又是陪床季,同事衣冠整齊,兢業(yè)盡責(zé)。一會(huì)做核磁共振,一會(huì)做胃鏡檢查,步走不離,警惕性高,午飯換著吃,互相...
    書海探源閱讀 627評(píng)論 0 0
  • 學(xué)攝影,你需要知道這10個(gè)攝影知識(shí)! 攝影零基礎(chǔ) 發(fā)布時(shí)間:17-12-2014:02優(yōu)質(zhì)原創(chuàng)作者 很多初學(xué)攝影的...
    王家二少2閱讀 1,354評(píng)論 11 20
  • 這是假貨嗎?
    都不懂我閱讀 214評(píng)論 0 0

友情鏈接更多精彩內(nèi)容