dockerfile

dockerfile基本结构
Dockerfile 是一个文本格式的配置文件,用户可以使用 Dockerfile 快速创建自定义镜像。

Dockerfile 由一行行命令语句组成,并且支持以 # 开头的注释行。

Dockerfile分为四部分:

  • 基础镜像信息
  • 维护者信息
  • 镜像操作指令
  • 容器启动时默认要执行的指令
[root@localhost ~]# mkdir  nginx
[root@localhost ~]# ls
anaconda-ks.cfg  nginx
[root@localhost ~]# cd nginx/
[root@localhost nginx]# cat Dockerfile 
# 第一行必须指定基于的基础镜像
FROM centos

# 维护者信息
LABEL MAINTAINER='xiaoyang 1@2.com '

# 镜像操作指令
RUN yum clean all && yum -y install epel-release
RUN yum -y install nginx
RUN echo "ndaemon off;" >> /etc/nginx/nginx.conf

# 容器启动时默认要执行的指令
CMD /usr/sbin/nginx



//镜像
[root@localhost ~]# docker build -t nginx:v0.3 nginx

[root@localhost ~]# docker run -d --name web3 nginx:v0.3

[root@localhost ~]# docker inspect web3
],
            "Cmd": [
                "/bin/sh",
                "-c",
                "/usr/sbin/nginx"
            ],

//改名字
[root@localhost ~]# docker tag nginx:v0.3 3091530433/nginx:v0.3

//登录
[root@localhost ~]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: 3091530433
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

//上传镜像到仓库
[root@localhost ~]# docker push 3091530433/nginx:v0.3
4b55844b2f31: Pushing   5.12kB
3602dcd8229e: Pushing  25.52MB/96.61MB
15c1af6616e6: Pushing  8.538MB/31.86MB
2653d992f4ef: Retrying in 11 seconds 
dial tcp 104.18.122.25:443: i/o timeout

cmd

CMD支持三种格式:

  • CMD [“executable”,“param1”,“param2”]使用exec执行,推荐方式
  • CMD command param1param2在/bin/sh中执行,提供给需要交互的应用
  • CMD [“param1”,“param2”]提供给ENTRYPOINT的默认参数
    CMD用于指定启动容器时默认要执行的命令,每个Dockerfile只能有一条CMD命令。如果指定了多条命令,只有最后一条会被执行。
    如果用户启动容器时指定了运行的命令,则会覆盖掉CMD指定的命令。
MAINTAINER

格式为[LABEL MAINTAINER] ,指定维护者信息

RUN

格式为RUN 或RUN [“executable”,“param1”,“param2”]
前者将在shell终端中运行命令,即/bin/sh -c;后者则使用exec执行。指定使用其他终端可以通过第二种方式实现
RUN ["/bin/bash",echo nihao]

每条RUN指令将在当前镜像基础上执行指定命令,并提交为新的镜像。当命令较长时可以使用 \ 来换行

RUN echo "hello world\nhello nihao" > /tmp/abcd && \
    cat /tmp/abcd
[root@localhost ~]# docker pull alpine
Using default tag: latest
latest: Pulling from library/alpine
801bfaa63ef2: Pull complete 
Digest: sha256:3c7497bf0c7af93428242d6176e8f
Status: Downloaded newer image for alpine:la
docker.io/library/alpine:latest


[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
busybox      latest    a77dce18d0ec   12 days ago   1.24MB
alpine       latest    389fef711851   3 weeks ago   5.58MB
nginx        latest    ae2feff98a0c   3 weeks ago   133MB
httpd        latest    dd85cdbb9987   4 weeks ago   138MB
//dockerfile制作Nginx镜像
[root@localhost ~]# vim  nginx/Dockerfile 

FROM alpine
  
LABEL MAINTAINER="yt 3091530433<@qq.com>"

ADD repositories /etc/apk/repositories
RUN apk add --no-cache nginx && \
    echo "pid  /run/nginx/nginx.pid;" >> /etc/nginx/nginx.conf && \
    echo "daemon off;" >> /etc/nginx/nginx.conf && \
    mkdir /run/nginx
CMD ["nginx"]


[root@localhost ~]# docker build -t nginx:1.10 nginx
Sending build context to Docker daemon  2.048kB
Step 1/5 : FROM alpine
 ---> 389fef711851
Step 2/5 : LABEL MAINTAINER="yt 3091530433<@qq.com>"
 ---> Running in 15efdee925cd
Removing intermediate container 15efdee925cd
 ---> f7f99b9bbd05
Step 3/5 : ADD repositories /etc/apk/repositories
ADD failed: file not found in build context or excluded by .dockerignore: stat repositories: file does not exist

[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                NAMES
67471242ce92   nginx     "/docker-entrypoint.…"   9 seconds ago   Up 8 seconds   0.0.0.0:80->80/tcp   thirsty_taussig

[root@localhost ~]# docker run -d -p 80:80 nginx
67471242ce923d8e38bbf0b09cef5f1720c067a739993b140c2ce7347c82bded

[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
<none>       <none>    374e75b29b56   5 minutes ago   5.58MB
busybox      latest    a77dce18d0ec   12 days ago     1.24MB
alpine       latest    389fef711851   3 weeks ago     5.58MB
nginx        latest    ae2feff98a0c   3 weeks ago     133MB
httpd        latest    dd85cdbb9987   4 weeks ago     138MB
centos       latest    300e315adb2f   4 weeks ago     209MB
ubuntu       latest    f643c72bc252   6 weeks ago     72.9MB

//采用国内阿里云的源
[root@localhost ~]# vi /etc/apk/repositories
https://mirrors.aliyun.com/alpine/v3.6/main/                       
https://mirrors.aliyun.com/alpine/v3.6/community/ 
[root@localhost ~]# curl 192.168.236.135
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值