- Dockerfile
- 1. 结构
- 2. 指令
- 3. 创建镜像
- 4. 创建 nginx 镜像
- 5. 制作 httpd 镜像
- 5.1 创建工作目录
- 5.2 编写dockerfile
- 5.3 制作镜像
- 5.4 修改名字
- 5.5 测试
- 5.6 上传
-
Dockerfile 由一行行命令语句组成,并且支持以 # 开头的注释行。
-
Docker分为四部分:
- 基础镜像信息
- 维护者信息
- 镜像操作指令
- 容器启动时默认要执⾏的指令
# This dockerfile uses the ubuntu image # VERSION 2 - EDITION 1 # Author: seancheng # Command format: Instruction [arguments / command] ... # 第⼀⾏必须指定基于的基础镜像 FROM ubuntu # 维护者信息 LABEL MAINTAINER='seancheng xianshangxian@126.com' # 镜像操作指令 RUN echo "deb http://archive.ubuntu.com/ubuntu/ raring main universe" >> /etc/apt/sources.list RUN apt-get update && apt-get install -y nginx //更新,安装 RUN echo "ndaemon off;" >> /etc/nginx/nginx.conf ndaemon off; 在前台运行 n 换行 # 容器启动时默认要执⾏的指令 CMD /usr/sbin/nginx2. 指令
- FROM
格式为 FROM或 FROM :
- LABEL MAINTAINER
格式为 LABEL MAINTAINER,指定维护者 信息
- RUN
格式为 RUN,RUN ["/bin/bash","-c","echo hello"] 在shell终端中运⾏命令,即/bin/sh -c 或 RUN ["executable","param1","param2"],RUN echo "hello worldnhello tom" > /tmp/abc && cat /tmp/abc 使⽤exec执⾏,每条RUN指令将在当前镜像基础上执⾏指定命令,并提交为新的镜像。当命令较⻓时可以使⽤ 来换⾏
- CMD
CMD ["executable","param1","param2"] 使⽤exec执⾏,推荐⽅式 CMD command param1 param2 在/bin/sh中执⾏,提供给需要交互的应⽤ CMD ["param1","param2"] 提供给ENTRYPOINT的默认参数
- EXPOSE
格式为 EXPOSE[ ...] EXPOSE 22 80 8443
- ENV
格式为 ENV。指定⼀个环境变量,会被后续RUN指令 使⽤,并在容器运⾏时保持 ENV PG_MAJOR 9.3 ENV PG_VERSION 9.3.4 RUN curl -SL http://example.com/postgres-$PG_VERSION.tar.xz | tar -xJC /usr/src/postgress && ... ENV PATH /usr/local/postgres-$PG_MAJOR/bin:$PATH
- ENTRYPOINT
ENTRYPOINT ["executable","param1","param2"] ENTRYPOINT command param1 param2(在shell中执⾏) 每个Dockerfile中只能有⼀个ENTRYPOINT ENTRYPOINT ["/usr/sbin/nginx"]3. 创建镜像
-
通过docker build命令来创建镜像,基本的格式为 docker build [选项] 路径
-
例如,指定Dockerfile所在路径为/tmp/docker_builder/,并且希望⽣成镜像标签为build_repo/first_image,可以使⽤下⾯的命令
docker build -t build_repo/first_image /tmp/docker_builder/4. 创建 nginx 镜像
- 创建目录并且编写dockerfile
[root@SYL4 ~]# mkdir nginx//创建目录
[root@SYL4 ~]# cd nginx/
[root@SYL4 nginx]# touch Dockerfile
[root@SYL4 nginx]# vim Dockerfile //编写
[root@SYL4 nginx]# cat Dockerfile
FROM ubuntu
LABEL MAINTAINER "xiaoyinguhong 3066347695@qq.com"
RUN echo "deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse" > etc/apt/sources.list &&
echo "deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse" >> /etc/apt/sources.list &&
echo "deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse" >> /etc/apt/sources.list &&
echo "deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse" >> /etc/apt/sources.list &&
echo "deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse" >> /etc/apt/sources.list &&
echo "deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse" >> /etc/apt/sources.list &&
echo "deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse" >> /etc/apt/sources.list &&
echo "deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse" >> /etc/apt/sources.list &&
apt-get update && apt-get -y install nginx &&
echo "ndaemon off;" >> /etc/nginx/nginx.conf
CMD ["/usr/sbin/nginx"] //或者/usr/sbin/nginx
[root@SYL4 nginx]#
[root@SYL4 nginx]# docker build -t nginx:2060.1 . //在当前目录下制作镜像
- CMD 和 ENTRYPOINT,创建镜像
[root@SYL4 nginx]# cat Dockerfile
FROM ubuntu
LABEL MAINTAINER "xiaoyinguhong 3066347695@qq.com"
........ 省略
apt-get update && apt-get -y install nginx
# echo "ndaemon off;" >> /etc/nginx/nginx.conf
CMD ["-g","daemon off;"]
ENTRYPOINT ["/usr/sbin/nginx"]
[root@SYL4 nginx]#
[root@SYL4 nginx]# docker build -t nginx:2060.3 .
[root@SYL4 ~]# docker run -d --name web nginx:2060.3
460f7ac5c0133b642f91ac324a2276a880a13438ad4b5cb772f983235c0e9dc8
[root@SYL4 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
460f7ac5c013 nginx:2060.3 "/usr/sbin/nginx -g …" 8 seconds ago Up 7 seconds web
[root@SYL4 ~]#
"Cmd": [
"-g",
"daemon off;"
],
"Image": "nginx:2060.3",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": [
"/usr/sbin/nginx"
- 暴露端口号,制作镜像,并访问
[root@SYL4 nginx]# cat Dockerfile
FROM ubuntu
LABEL MAINTAINER "xiaoyinguhong 3066347695@qq.com"
RUN echo "deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse" > etc/apt/sources.list &&
......
apt-get update && apt-get -y install nginx
# echo "ndaemon off;" >> /etc/nginx/nginx.conf
EXPOSE 80 443
CMD ["-g","daemon off;"]
ENTRYPOINT ["/usr/sbin/nginx"]
[root@SYL4 nginx]#
制作镜像
[root@SYL4 nginx]# docker build -t nginx:2060.4 .
运行
[root@SYL4 ~]# docker run -d --name web1 nginx:2060.4
bdaeaf7f9d0a3f2c1e3c548d1cae9672208456a9a7fc631394e4f41b901915c4
[root@SYL4 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bdaeaf7f9d0a nginx:2060.4 "/usr/sbin/nginx -g …" 2 seconds ago Up 1 second 80/tcp, 443/tcp web1
460f7ac5c013 nginx:2060.3 "/usr/sbin/nginx -g …" 8 minutes ago Up 8 minutes web
[root@SYL4 ~]#
访问
[root@SYL4 ~]# curl 172.17.0.3
Welcome to nginx!
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
For online documentation and support please refer to
nginx.org.
Commercial support is available at
nginx.com.
Thank you for using nginx.
[root@SYL4 ~]#
- 环境变量
[root@SYL4 nginx]# vim Dockerfile
[root@SYL4 nginx]# cat Dockerfile
FROM ubuntu
LABEL MAINTAINER "xiaoyinguhong 3066347695@qq.com"
ENV a=10
RUN echo "deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse" > etc/apt/sources.list &&
.....
apt-get update && apt-get -y install nginx
# echo "ndaemon off;" >> /etc/nginx/nginx.conf
EXPOSE 80 443
CMD ["-g","daemon off;"]
ENTRYPOINT ["/usr/sbin/nginx"]
[root@SYL4 nginx]#
[root@SYL4 nginx]# docker build -t nginx:2060.5 .
运行容器
[root@SYL4 ~]# docker run -d --name web2 nginx:2060.5
5fcbe6f624a5032d4b8dd9cfadac893a66207739c67b8da1a900b8b9e5517d71
[root@SYL4 ~]# docker exec -it web2 /bin/bash
root@5fcbe6f624a5:/# echo $a
10
root@5fcbe6f624a5:/# bash
root@5fcbe6f624a5:/# bash
root@5fcbe6f624a5:/# echo $a
10
root@5fcbe6f624a5:/# exit
exit
root@5fcbe6f624a5:/# exit
exit
root@5fcbe6f624a5:/# exit
exit
[root@SYL4 ~]#
5. 制作 httpd 镜像
5.1 创建工作目录
[root@SYL4 ~]# mkdir httpd [root@SYL4 ~]# cd httpd [root@SYL4 httpd]# touch Dockerfile [root@SYL4 httpd]# [root@SYL4 httpd]# ls Dockerfile apr-1.7.0.tar.gz apr-util-1.6.1.tar.gz httpd-2.4.53.tar.gz [root@SYL4 httpd]# mkdir httpdfile [root@SYL4 httpd]# mv *.gz httpdfile5.2 编写dockerfile
[root@SYL4 httpd]# vim Dockerfile
[root@SYL4 httpd]# cat Dockerfile
FROM centos
LABEL MAINTAINER "xiaoyinguhong 3066347695@qq.com"
ENV apache_apr 1.7.0
ENV apache_apr_util 1.6.1
ENV apache_httpd 2.4.53
ADD httpdfile/* /usr/src/
RUN rm -rf /etc/yum.repos.d/* &&
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo &&
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo &&
yum -y install make gcc gcc-c++ openssl-devel pcre-devel expat-devel libtool &&
useradd -r -M -s /sbin/nolofgin apache &&
cd /usr/src/apr-${apache_apr} &&
sed -i '/$RM "$cfgfile"/d' configure &&
./configure --prefix=/usr/local/apr &&
make && make install &&
cd ../apr-util -${apache_apr_util} &&
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && make && make install &&
cd ../httpd-${apache_httpd} &&
./configure --prefix=/usr/local/apache
--enable-so
--enable-ssl
--enable-cgi
--enable-rewrite
--with-zlib
--with-pcre
--with-apr=/usr/local/apr
--with-apr-util=/usr/local/apr-util/
--enable-modules=most
--enable-mpms-shared=all
--with-mpm=prefork &&
make && make install &&
sed -i '/#ServerName/#//g' /usr/local/apache2/conf/httpd.conf
WORKDIR /usr/local/httpd
EXPOSE 80
CMD ["-D","FOREGROUND"]
ENTRYPOINT ["/usr/local/apache/bin/httpd -DFOREGROUND"]
[root@SYL4 httpd]#
5.3 制作镜像
[root@SYL4 httpd]# docker build -t xiaoyinguhong/httpd:2060.1 .5.4 修改名字
[root@SYL4 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE5.5 测试777c1ec97924 2 minutes ago 282MB nginx 2060.5 caf742479513 3 hours ago 192MB xiaoyinguhong/httpd 2008.2 1fb3fb6f13ac 8 days ago 383MB busybox latest beae173ccac6 4 months ago 1.24MB httpd latest dabbfbe0c57b 4 months ago 144MB ubuntu latest ba6acccedd29 6 months ago 72.8MB centos latest 5d0da3dc9764 7 months ago 231MB [root@SYL4 ~]# docker tag 777c1ec97924 xiaoyinguhong/httpd:2012.1 [root@SYL4 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE xiaoyinguhong/httpd 2012.1 777c1ec97924 13 minutes ago 282MB xiaoyinguhong/httpd 2060.1 777c1ec97924 13 minutes ago 282MB nginx 2060.5 caf742479513 4 hours ago 192MB xiaoyinguhong/httpd 2008.2 1fb3fb6f13ac 8 days ago 383MB busybox latest beae173ccac6 4 months ago 1.24MB httpd latest dabbfbe0c57b 4 months ago 144MB ubuntu latest ba6acccedd29 6 months ago 72.8MB centos latest 5d0da3dc9764 7 months ago 231MB [root@SYL4 ~]#
[root@SYL4 ~]# docker run -it --rm --name web -p 80:80 xiaoyinguhong/httpd:2060.1 [root@f299c605d868 src]# [root@SYL4 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f299c605d868 xiaoyinguhong/httpd:2060.1 "/bin/bash" 19 seconds ago Up 17 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp web [root@SYL4 ~]#5.6 上传
[root@SYL4 ~]# docker push xiaoyinguhong/httpd:2012.1 The push refers to repository [docker.io/xiaoyinguhong/httpd] 463b7392cb1d: Layer already exists 74ddd0ec08fa: Layer already exists 2012.1: digest: sha256:dfbd7e5ee7abb8a16731d8ba99797ef13c431bbc0ec254f94ee6ed4eb8f83843 size: 741 [root@SYL4 ~]#



