Docker for MacOS
- 安裝可以直接在 github 上找到 installer, 當前 version 1.8
- 本部落格參考書主要為 Docker基礎與實戰, 並結合網上的常見問題以及解決方案.
,
基本操作
檢查版本
bash-3.2$ docker version
Client:
Version: 1.8.0
API version: 1.20
Go version: go1.4.2
Git commit: XXXXXX
Built: Tue Aug 11 17:17:40 UTC 2015
OS/Arch: darwin/amd64
Server:
Version: 1.8 .0
API version: 1.20
Go version: go1.4.2
XXXX
使用 search 命令搜尋映象
bash-3.2$ docker search ubuntu
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
ubuntu Ubuntu is a Debian-based Linux operating s... 6276 [OK]
rastasheep/ubuntu-sshd Dockerized SSH service, built on top of of... 91 [OK]
ubuntu-upstart Upstart is an event-based replacement for ... 74 [OK]
neurodebian NeuroDebian provides neuroscience research... 37 [OK]
32bit/ubuntu Ubuntu for i386 (32bit) 30
...
使用 pull 命令下載 Ubuntu Linux 映象
bash-3.2$ docker pull ubuntu:latest
latest: Pulling from library/ubuntu
02658b5e0e10: Pull complete
5a70b1a02339: Pull complete
0f89582aebaa: Pull complete
ffa5309fe008: Pull complete
7510bd34aee9: Pull complete
dc8dd8718e57: Pull complete
Digest: sha256:8e6b67faf44a036a18b9a3987e826fe3accbd1742e219145a461a3210a9d8142
Status: Downloaded newer image for ubuntu:latest
使用 images 命令列出映象目錄
bash-3.2$docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ubuntu latest dc8dd8718e57 3 weeks ago 119.2 MB
hello-world latest f054dc87ed76 4 weeks ago 1.84 kB
使用 run 命令建立一個容器進入系統
bash-3.2$ docker run -i -t --name hello ubuntu /bin/bash
root@81bbf07ca806:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@81bbf07ca806:/# exit
exit
使用 ps 命令檢視容器列表
bash-3.2$ docker ps # 檢視活躍的
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9ce00dac39bd nginx "nginx -g 'daemon off" 18 minutes ago Up 18 minutes 0.0.0.0:80->80/tcp webserver
81bbf07ca806 ubuntu "/bin/bash" 28 minutes ago Up 16 minutes ubuntu
bash-3.2$ docker ps -a # 檢視所有的
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9ce00dac39bd nginx "nginx -g 'daemon off" 18 minutes ago Up 18 minutes 0.0.0.0:80->80/tcp webserver
81bbf07ca806 ubuntu "/bin/bash" 28 minutes ago Up 16 minutes ubuntu
b4c7ba6a7bf7 hello-world "/hello" 39 minutes ago Exited (0) 39 minutes ago desperate_stallman
35ca3df8f038 hello-world "/hello" 39 minutes ago Exited (0) 39 minutes ago cranky_pasteur
使用 stop 和 start 命令關閉和啟動容器
bash-3.2$ docker stop ubuntu
ubuntu
bash-3.2$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9ce00dac39bd nginx "nginx -g 'daemon off" 21 minutes ago Up 21 minutes 0.0.0.0:80->80/tcp webserver
bash-3.2$ docker start ubuntu
ubuntu
bash-3.2$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9ce00dac39bd nginx "nginx -g 'daemon off" 22 minutes ago Up 22 minutes 0.0.0.0:80->80/tcp webserver
81bbf07ca806 ubuntu "/bin/bash" 32 minutes ago Up 3 seconds ubuntu
bash-3.2$
使用 attach 命令連線容器
bash-3.2$ docker start ubuntu
ubuntu
bash-3.2$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
81bbf07ca806 ubuntu "/bin/bash" 37 minutes ago Up 5 seconds ubuntu
bash-3.2$ docker attach ubuntu
root@81bbf07ca806:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@81bbf07ca806:/# exit
exit
使用 exec 命令從外部執行容器內的命令
- 當前容器正以/bin/bash 形式處於執行狀態,也可以不通過/bin/bash 而從外部執行容器內的命令
bash-3.2$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bash-3.2$ docker exec ubuntu echo "Hello ubuntu"
Error response from daemon: Container ubuntu is not running
bash-3.2$ docker start ubuntu # 必須處於執行狀態
ubuntu
bash-3.2$ docker exec ubuntu echo "Hello ubuntu"
Hello ubuntu
比較有用的,
attach方式有比較大的侷限性,如果容器中有程式正在執行,通過docker attach進入之後是不能執行操作的,而且一個終端退出之後整個container就終止了。所以還是直接用 exec吧, ^_^.
bash-3.2$ docker ps | awk '{print $1, $2, $3, $4}'
CONTAINER ID IMAGE COMMAND
80b67ff7a440 hello:0.1 "nginx" About
bash-3.2$ docker exec -i -t 80b67ff7a440 bash
root@80b67ff7a440:/etc/nginx# ls
conf.d fastcgi.conf fastcgi_params koi-utf koi-win mime.types nginx.conf proxy_params scgi_params site-enable sites-available sites-enabled snippets uwsgi_params win-utf
root@80b67ff7a440:/etc/nginx#
引數說明:
Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Run a command in a running container
-d, --detach Detached mode: run command in the background
--detach-keys Specify the escape key sequence used to detach a container
--help Print usage
-i, --interactive Keep STDIN open even if not attached
--privileged Give extended Linux capabilities to the command
-t, --tty Allocate a pseudo-TTY
-u, --user= Username or UID (format: <name|uid>[:<group|gid>])
bash-3.2$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
80b67ff7a440 hello:0.1 "nginx" About an hour ago Up 3 seconds 443/tcp, 0.0.0.0:8080->80/tcp hello-nginx
使用 rm 命令刪除容器
bash-3.2$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9ce00dac39bd nginx "nginx -g 'daemon off" 34 minutes ago Exited (0) 9 minutes ago webserver
81bbf07ca806 ubuntu "/bin/bash" 44 minutes ago Up About a minute ubuntu
b4c7ba6a7bf7 hello-world "/hello" 55 minutes ago Exited (0) 55 minutes ago desperate_stallman
35ca3df8f038 hello-world "/hello" 55 minutes ago Exited (0) 55 minutes ago cranky_pasteur
bash-3.2$ docker rm b4c7ba6a7bf7
b4c7ba6a7bf7
bash-3.2$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9ce00dac39bd nginx "nginx -g 'daemon off" 34 minutes ago Exited (0) 10 minutes ago webserver
81bbf07ca806 ubuntu "/bin/bash" 44 minutes ago Up About a minute ubuntu
35ca3df8f038 hello-world "/hello" 55 minutes ago Exited (0) 55 minutes ago cranky_pasteur
使用 rmi 命令刪除映象 (容器和映象的關聯)
bash-3.2$ docker rmi hello-world
Error response from daemon: Conflict, cannot delete f054dc87ed76 because the container 35ca3df8f038 is using it, use -f to force
Error: failed to remove images: [hello-world]
bash-3.2$ docker rmi -f hello-world
Untagged: hello-world:latest
Deleted: f054dc87ed76d853247ffefeaeac52ccbb07d1444949ca324d04365b29c94323
Deleted: fc882b159a63d6da4646a4c54194c32fad6c9b682655993c5c39ac021c0fd367
bash-3.2$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
nginx latest 7b5ba97f8d15 6 days ago 107.5 MB
ubuntu latest dc8dd8718e57 3 weeks ago 119.2 MB
bash-3.2$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9ce00dac39bd nginx "nginx -g 'daemon off" 36 minutes ago Exited (0) 11 minutes ago webserver
81bbf07ca806 ubuntu "/bin/bash" 46 minutes ago Up 3 minutes ubuntu
35ca3df8f038 hello-world "/hello" 57 minutes ago Exited (0) 57 minutes ago cranky_pasteur
bash-3.2$ docker start 35ca3df8f038
Error response from daemon: Cannot start container 35ca3df8f038: Error getting container 35ca3df8f038f1d1d0139e72e299b9cd9497b3f0c166d627aae1f9f018d1c68b from driver aufs: error creating aufs mount to /mnt/sda1/var/lib/docker/aufs/mnt/35ca3df8f038f1d1d0139e72e299b9cd9497b3f0c166d627aae1f9f018d1c68b: invalid argument
Error: failed to start containers: [35ca3df8f038]
bash-3.2$ docker rm 35ca3df8f038
35ca3df8f038
bash-3.2$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9ce00dac39bd nginx "nginx -g 'daemon off" 38 minutes ago Exited (0) 13 minutes ago webserver
81bbf07ca806 ubuntu "/bin/bash" 48 minutes ago Up 5 minutes ubuntu
bash-3.2$
建立 Docker 映象
Bash 指令
- $() 將命令的執行結果量化
bash-3.2$ echo $(date)
Tue Jul 18 16:01:03 CST 2017
- ${} 變數替換
$str ="World"
$echo "Hello ${str}"
Hello World
也用於指令碼中設定預設值, 下列示例中,若有 HELLO 變數,則直接使用,否則代入預設值 abcd
$HELLO=
$HELLO=${HeLLO-"abcd"}
$echo $HELLO
由於已經有值為 NULL 的 HELLO 變數,故不會代入預設值.
下列示例中,若變數中有值,則直接使用,若值為 NULL, 則代入預設值 abcd
$HELLO=
$HELLO=${HeLLO:-"abcd"}
$echo $HELLO
abcd
由於變數值為 NULL ,故代入預設值.
- if
if [$a -eq $b]; then
echo $a
fi
- for
for i in $(ls)
do echo $i
done
for ((i=0;i<10;++i))
do echo $i
done
NUM=(1 2 3)
for i in ${NUM[@]}
do echo $i
done
編寫 Dockerfile
- Dockerfile 是 Docker映象設定檔案. Dockerfile中設定的內容會用於建立映象
- 建立並轉移到 example 目錄
$mkdir example
$cd example
將如下檔案儲存為 Dockerfile 檔案
FROM ubuntu:latest
MAINTAINER Foo Bar <[email protected].com>
RUN apt-get update
RUN apt-get install -y nginx
RUN echo "\ndaemon off;" >> /etc/nginx/nginx.conf
RUN chmod -R 777 /var/lib/nginx
VOLUME ["/data", "/etc/nginx/site-enable", "/var/log/nginx"]
WORKDIR /etc/nginx
CMD ["nginx"]
EXPOSE 80
EXPOSE 443
上述示例基於 Ubuntu 最新版本建立 Docker 映象,且安裝 nginx 伺服器
引數說明:
FROM:指定基於的基礎映象. Docker 映象基於已建立的映象.指令格式為<映象名稱>:<標籤>
MAINTAINER: 維護者資訊
RUN : 執行 shell 指令碼
>> VOLUME:要與主機共享的目錄.也可以在 docker run 命令中使用-v選項進行設定(後面會出現).
CMD:指定容器啟動時執行的檔案或 shell 指令碼.
WORKDIR: 為 CMD中的可執行檔案設定執行目錄
EXPOSE:與主機相連的埠號
使用 build 命令建立映象
bash-3.2$ cd example/
bash-3.2$ ls
Dockerfile
bash-3.2$ docker build --tag hello:0.1 . # 最後一個. 表示路徑
Sending build context to Docker daemon 2.048 kB
Step 0 : FROM ubuntu:latest
---> dc8dd8718e57
Step 1 : MAINTAINER Foo Bar <[email protected]>
---> Using cache
---> a5741076e53d
Step 2 : RUN echo -e "begin to build"
---> Using cache
---> 14d3a847a112
Step 3 : RUN apt-get update
---> Using cache
---> 674165c71560
Step 4 : RUN apt-get install -y nginx
...
Removing intermediate container 50bc5e7e0ec7
Successfully built 04147bc0060f
bash-3.2$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
hello 0.1 04147bc0060f 59 seconds ago 214.3 MB
nginx latest 7b5ba97f8d15 6 days ago 107.5 MB
ubuntu latest dc8dd8718e57 3 weeks ago 119.2 MB
bash-3.2$
至此, hello:0.1 映象已經建立,下面嘗試執行
bash-3.2$ mkdir macData
bash-3.2$ docker run --name hello-nginx -d -p 8080:80 -v ~/macData:/data hello:0.1
80b67ff7a44091ce1c79914a40c2736a6fae63ddb8d42499a557da6d180c40b4
bash-3.2$
引數說明:
-d選項在後臺執行容器
-p 8080:80 選項將主機的8080號埠與容器的80號埠連線起來,並暴露給外部.這樣設定後,連線 http:<主機 IP>:8080就可以連線到容器的80號埠
-v ~/macData:/data 選項將主機的~/macData目錄連線到容器的/ data 目錄.若將檔案放入~/macData目錄,則能從容器讀取相應檔案.
bash-3.2$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
80b67ff7a440 hello:0.1 "nginx" 5 minutes ago Up 5 minutes 443/tcp, 0.0.0.0:8080->80/tcp hello-nginx
一開始總是出不來,以為配置出錯了,檢查了好多遍還是沒作用,後來在
極客學院找到了解決方案,是因為 docker_host 的主機 ip 不是127.0.0.1, 應該使用 boot2docker 虛擬機器的 ip 來訪問(如果是 linux 的系統可能會由於沒有安裝boot2docker虛擬機器, IP 就是localhost).
bash-3.2$ boot2docker ip
192.168.59.103
然後使用 192.168.59.103 作為 ip 來訪問即可.
OK.
檢視 Docker
分清映象和容器,
hello:0.1==映象 - - - - > run - - - - > hello-nginx == 容器
hello-nginx == 容器 - - - - > commit - - - - > hello:0.2==映象
使用 history命令檢視映象歷史
bash-3.2$ docker history hello:0.1
IMAGE CREATED CREATED BY SIZE COMMENT
04147bc0060f 17 hours ago /bin/sh -c echo -e "\nBUILD OK." 0 B
175bd963d3ce 17 hours ago /bin/sh -c #(nop) EXPOSE 443/tcp 0 B
010962f92677 17 hours ago /bin/sh -c #(nop) EXPOSE 80/tcp 0 B
d040b08abba1 17 hours ago /bin/sh -c #(nop) CMD ["nginx"] 0 B
5626faf4ad82 17 hours ago /bin/sh -c #(nop) WORKDIR /etc/nginx 0 B
1313be469632 17 hours ago /bin/sh -c #(nop) VOLUME [/data /etc/nginx/si 0 B
ce55d8eeb168 17 hours ago /bin/sh -c chmod -R 777 /var/lib/nginx 0 B
5eee21f14ebe 17 hours ago /bin/sh -c echo "\ndaemon off;" >> /etc/nginx 1.475 kB
5a32862e5c34 17 hours ago /bin/sh -c apt-get install -y nginx 56.53 MB
674165c71560 17 hours ago /bin/sh -c apt-get update 38.64 MB
14d3a847a112 17 hours ago /bin/sh -c echo -e "begin to build" 0 B
a5741076e53d 17 hours ago /bin/sh -c #(nop) MAINTAINER Foo Bar <[email protected] 0 B
dc8dd8718e57 4 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0 B
7510bd34aee9 4 weeks ago /bin/sh -c mkdir -p /run/systemd && echo 'doc 7 B
ffa5309fe008 4 weeks ago /bin/sh -c sed -i 's/^#\s*\(deb.*universe\)$/ 2.759 kB
0f89582aebaa 4 weeks ago /bin/sh -c rm -rf /var/lib/apt/lists/* 0 B
5a70b1a02339 4 weeks ago /bin/sh -c set -xe && echo '#!/bin/sh' > /u 745 B
02658b5e0e10 4 weeks ago /bin/sh -c #(nop) ADD file:c251a21fbe3a651352 119.2 MB
bash-3.2$
使用 cp 命令複製檔案
docker cp <容器名稱>:<路徑> <主機路徑>
和 scp 很像的
bash-3.2$ docker cp hello-nginx:/etc/nginx/nginx.conf ./
bash-3.2$ ls
Applications Documents Library Music Pictures VirtualBox VMs learngit nginx.conf
Desktop Downloads Movies MyJabberFiles Public example macData test
bash-3.2$ ls | grep nginx.conf
nginx.conf
使用 commit 命令從容器的修改中建立映象
- 試試新裝一個 python3
bash-3.2$ docker exec -it hello-nginx bash
root@80b67ff7a440:/etc/nginx# apt-get install python3
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
dh-python file libmagic1 libmpdec2 libpython3-stdlib libpython3.5-minimal libpython3.5-stdlib libsqlite3-0 mime-support python3-minimal python3.5 python3.5-minimal
Suggested packages:
libdpkg-perl python3-doc python3-tk python3-venv python3.5-venv python3.5-doc binutils binfmt-support
The following NEW packages will be installed:
dh-python file libmagic1 libmpdec2 libpython3-stdlib libpython3.5-minimal libpython3.5-stdlib libsqlite3-0 mime-support python3 python3-minimal python3.5 python3.5-minimal
0 upgraded, 13 newly installed, 0 to remove and 10 not upgraded.
Need to get 5278 kB of archives.
After this operation, 29.4 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 libpython3.5-minimal amd64 3.5.2-2ubuntu0~16.04.1 [526 kB]
root@80b67ff7a440:/etc/nginx# exit
- 容器內新加了 python3, 現在使用 commit 建立新的映象
可以使用docker diff hello-nginx
檢視檔案修改 - 映象名稱 hello:0.2, 然後正常使用 run 建立容器,一切都是這麼自然.感覺和git 很像啊.
bash-3.2$ docker commit -a "Foo Bar <[email protected]>" -m "add python3" hello-nginx hello:0.2
36d837ee87cc95b79c35aa3343edc3adbe7888769ef030eb07333eef3e9e5f2e
bash-3.2$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
hello 0.2 36d837ee87cc 11 seconds ago 251 MB
hello 0.1 04147bc0060f 19 hours ago 214.3 MB
ubuntu latest dc8dd8718e57 4 weeks ago 119.2 MB
bash-3.2$ docker run --name hello-python3 -d -p 82:80 hello:0.2
e8127484c447db55223a255c451a5f7e635fdf12e83781d93058cf812e5914af
bash-3.2$ docker exec -it hello-python3 bash
''root@e8127484c447:/etc/nginx# python3
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello,python3")
hello,python3
>>>
訪問主機82埠也是正常的.
使用 inspect 命令檢視詳細資訊
- 返回json 格式資料
bash-3.2$ docker inspect 80b67ff7a440
[
{
"Id": "80b67ff7a44091ce1c79914a40c2736a6fae63ddb8d42499a557da6d180c40b4",
"Created": "2017-07-19T01:26:29.362419085Z",
"Path": "nginx",
"Args": [],
"State": {
"Running": false,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 0,
...
}]
靈活使用 Docker
- 該部分搞不懂啊,才說了 git 差不多,但是怎麼煩,先按書上講的走一次流程吧
私有倉庫
bash-3.2$ docker run -d -p 5000:5000 --name hello-registry -v ~/registry:/tmp/registry registry
d56699303f7f3cfc90cf0aae494297b4208d98443bd341ac958d436c701279c8
bash-3.2$ docker tag hello:0.1 localhost:5000/hello:0.1
bash-3.2$ docker push localhost:5000/hello:0.1
bash-3.2$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
hello 0.2 9138326c41e2 56 minutes ago 251 MB
hello 0.1 04147bc0060f 23 hours ago 214.3 MB
localhost:5000/hello 0.1 04147bc0060f 23 hours ago 214.3 MB
registry latest dd399a13752f 2 weeks ago 33.2 MB
ubuntu latest dc8dd8718e57 4 weeks ago 119.2 MB
但是 ~/registry 目錄為空,我也不知怎麼回事,感覺加入倉庫應該有檔案啊,搞不懂????
bash-3.2$ tree registry/
registry/
0 directories, 0 files
儲存映象資料到 Amazon S3
bash-3.2$ docker run -d -p 5000:5000 --name s3-registry -e SETTINGS_FLAVOR=s3 -e AWS_BUCKET=examplebucket10 -e STOPAGE_PATH=/registry -e AWS_KEY=AKIABCDEFGHIJKLMNOPQ -e AWS_SECRET=sF4321ABCDEFGHIJKLMNOPqrstuvwxyz21345Afc registry
1bb079f113f0d5b4accc88e5a54351a69c4784690d0a1b3efec67c52491a3e43
使用預設認證
etc/hosts 追加
#add for docker registry
192.168.59.103 registry.example.com
- 建立私鑰密碼
openssl genrsa -out server.key 2048
- 建立證書籤名申請
- 建立伺服器證書檔案
root# openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
Signature ok
subject=/C=KO/ST=Some-State/L=Seoul/O=Example Company/OU=Example Company/CN=registry.example.com/[email protected].com
Getting Private key
- 將 server.crt證書檔案安裝到系統
...
- 不將證書安裝到系統
先跳過這一章了, 看看怎麼從官方的倉庫來拉映象
詳細瞭解 Dockerfile
- 命令不區分大小寫,一般採用大寫;
- # 是註釋
- 順序執行
- 命令總是從 From開始,若無 From 命令或者 From 之前有其他命令命令,則無法建立映象
要在 Dockerfile所在的目錄使用 docker build命令
- 若想把映象上傳到 Docker Hub,只要在/之前新增使用者名稱即可
$docker build --tag example .
$docket build -t username/imagename .
.dockerignore
- 所有位於 Dockerfile目錄下的檔案都稱為”上下文”(context).特別是在建立映象時,由於所有上下文都會傳送 Docker守護程序.
- 需要從上下文忽略某些檔案時,只要採用.dockerignore檔案即可.Docker 採用 Go語言編寫,檔案匹配也遵循 Go 語言規則
example/hello.txt
example/*.cpp
wo*
*.cpp
.git
.svn
忽略上述路徑下的滿足匹配的檔案.
From
- From <映象> 或者 FROM<映象>:<標籤>
- 預設 latest 標籤
FROM ubuntn
FROM ubuntu:14.04
MAINTAINER
- MAINTAINER<建立者資訊> 可省略
RUN
RUN用於在 FROM中設定的映象上執行指令碼或命令, RUN的執行結果會生成新的映象,執行的詳細資訊會紀錄到映象歷史(history)
使用 shell(bash/sh)執行命令
RUN apt-get insatll -y nginx RUN echo "hello DOcker" > /temp/hello
無 shell 直接執行
- 使用格式為 RUN[“<可執行檔案>”, “<形式引數1>”, “<形式引數2>”, …].
RUN ["apt-get", "install", "-y", "gcc"]
RUN ["/user/local/bin/hello", "--help"]
CMD && ENTRYPOINT
- CMD 和 RUN 差不多
- ENTRYPOINT 用於設定容器啟動時執行的指令碼或命令, 即使用 docker run 命令建立容器或使用 docker start 命令啟動停止的容器時執行, 如果每次啟動執行,可以考慮加入 welcome 之類的標誌開機啦.(但是我的測試ENTRYPOINT並沒有每次 start 都執行)
EXPOSE
- EXPOSE 用於設定與主機相連的埠號
EXPOSE 80
EXPOSE 443
===#一行搞定
EXPOSE 80 443
ENV
- ENV用於設定環境變數.使用 ENV 設定的環境變數應用於 RUN, CMD, ENTRYPOINT.
- 使用格式 ENV <環境變數> <值>
ENV GOPATH /go
>Dockerfile
ENV HELLO 1234
CMD echo $HELLO
建立 Dockfile,使用 docker run 命令執行
1234
但是我的測試, 有點例外
> Dockerfile
FROM ubuntu:latest
MAINTAINER icesongqiang
RUN echo -e "begin to build"
RUN apt-get update
RUN ["apt-get", "install", "-y", "gcc"]
CMD ["apt-get", "install", "-y", "python3"]
EXPOSE 80 443
RUN echo -e "\nBUILD OK."
ENV author icesongqiang
CMD echo "cmd" $author
ENTRYPOINT echo "entrypoint" $author
bash-3.2$docker build -t icesongqiang/docker-test .
Sending build context to Docker daemon 2.048 kB
Step 0 : M
Unknown instruction: M
bash-3.2$ vi Dockerfile
bash-3.2$ docker build -t icesongqiang/docker-test .
Sending build context to Docker daemon 2.048 kB
Step 0 : FROM ubuntu:latest
---> dc8dd8718e57
Step 1 : MAINTAINER icesongqiang
---> Running in 71bf3917c5bb
---> c570c14320d2
bash-3.2$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
icesongqiang/docker-test latest 6187194a41e1 15 hours ago 256.1 MB
icesongqiang/hello-nginx-gcc latest a74121a1faf7 15 hours ago 312.7 MB
bash-3.2$ docker run --name test icesongqiang/docker-test
# cmd 未執行??
entrypoint icesongqiang
bash-3.2$ docker stop test
test
bash-3.2$ docker start test
test
bash-3.2$ docker start -i test #使用-i entrypoint列印
entrypoint icesongqiang
ADD && COPY
- ADD /COPY <要複製檔案的路徑> <