Docker for MacOS

  • 安装可以直接在 github 上找到 installer, 当前 version 1.8
  • 本博客参考书主要为 Docker基础与实战, 并结合网上的常见问题以及解决方案.

image,

基本操作

检查版本

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 <foo@bar.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 <foo@bar.com>
 ---> 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

Unable
一开始总是出不来,以为配置出错了,检查了好多遍还是没作用,后来在
极客学院找到了解决方案,是因为 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 <foo@bar   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 <foo@bar.com>" -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/emailAddress=exampleuser@example.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)

    1. 使用 shell(bash/sh)运行命令

      RUN apt-get insatll -y nginx
      RUN echo "hello DOcker" > /temp/hello
    2. 无 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 <要复制文件的路径> < 文件在镜像中的路径>
ADD hello-entrypoint.sh /entrypoint.sh
ADD hello.zip /
ADD http://example.com/hello.txt /hello.txt
ADD *.txt /root/
  • COPY 用于添加压缩文件时,不会解压缩; 也不能使用文件 URL

VOLUME

  • 将目录下的内容存储到主机而非容器
  • VOLUME[“容器目录1”, “容器目录2”, …]
VOLUME /data
VOLUME ["/data", "/var/log/hello"]

若想连接数据卷与主机的特定目录,则必须在 docker run命令中使用-v选项.
- 选项格式 -v <主机目录>:<容器目录>

docker run -v ~/macdata:/root/data

USER, WORKDIR


ONBUILB

  • 将当前镜像作为基础镜像创建其他镜像时,ONBUILD 指定用于设置一些要触发(trigger)的操作.ONBUILD指定的命令在构建时并不执行,而是在其子镜像中执行.换言之,ONBUILD中定义的指令会用于其他镜像的 Dockerfile文件的 FROM 指令之后开始执行.
  • ONBUILD 不会继承
ONBUILD RUN touch /hello.txt

使用 Docker部署应用程序

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值