Docker advanced dockerfile instructions to build docker images

content

Getting to know Dockerfile

Write your own docker image:

Write the Dockerfile:

Convert the Dockerfile to an image:

View your own built docker image

Generate a container using your own built docker image:

DockerFile

Dockerfile instructions:

docker build . command

See how the image is generated

Practical training: Dockerfile builds Nginx image:

Write a Dockerfile to make an image, and the generated image is named my_build:Nginx1. First create the directory dockerfile-nginx1 and save the Dockerfile file. Specific requirements are as follows:

Get started:

Create the dockerfile-nginx1 folder, enter the folder, and write the Dockerfile file

Write the contents of the Dockerfile file:

Write another run.sh in this folder

docker build build image

 docker images to view the mirror:

 Build the container using this image:


Getting to know Dockerfile

Dockerfile is the build file used to build the docker image! Command script!

Through this script, the mirror can be generated. The mirror is layer by layer. The script is a line of commands, and each line of commands is a layer.

Dockerfile is development-oriented. If we want to publish projects and make mirrors in the future, we need to write a dockerfile file. This file is very simple.

Dockerfile images have become the standard for enterprise delivery and must be mastered!

Write your own docker image:

See how an image is generated:

docker history 镜像ID或镜像名

Build steps:

1、编写一个dockerfile文件

2、docker build 构建成为一个镜像

3、docker run 运行镜像

4、docker push 发布镜像

Write the Dockerfile:

#创建一个dockerfile文件,名字可以随机,建议Dockerfile
#文件中的内容:指令(大写) 参数

#实例测试:
#创建目录存放dockerfile文件的目录:
[root@localhost ~]# mkdir docker_test

#进入目录创建dockerfile文件:
[root@localhost ~]# mkdir docker_test
[root@localhost ~]# cd docker_test/
[root@localhost docker_test]# touch dockerfile

#编写dockerfile文件,在文件中写入dockerfile指令:
[root@localhost docker_test]# vim dockerfile
FROM centos
VOLUME ["volume01","volume02"]
CMD echo "----end----"
CMD /bin/bash

Convert the Dockerfile to an image:

#使用docker build . 命令将dockerfile文件转为镜像

[root@localhost docker_test]# docker build -f /root/docker_test/dockerfile -t hzw/centos .
Sending build context to Docker daemon  2.048kB

Step 1/4 : FROM centos
 ---> 5d0da3dc9764
Step 2/4 : VOLUME ["volume01","volume02"]
 ---> Running in d4b23ae86ace
Removing intermediate container d4b23ae86ace
 ---> 8f39792fbef7
Step 3/4 : CMD echo "----end----"
 ---> Running in 1bb4751f04ef
Removing intermediate container 1bb4751f04ef
 ---> 2e2bb5e2a502
Step 4/4 : CMD /bin/bash
 ---> Running in 82a0b5905405
Removing intermediate container 82a0b5905405
 ---> 39a1fd86fd73
Successfully built 39a1fd86fd73
Successfully tagged hzw/centos:latest

#由此可以发现docker创建镜像也是一层层创建的。每个命令都是镜像的一层。

View your own built docker image

[root@localhost docker_test]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED         SIZE
hzw/centos            latest    39a1fd86fd73   2 minutes ago   231MB

Generate a container using your own built docker image:

[root@localhost docker_test]# docker run -it hzw/centos /bin/bash
[root@f62a714bb306 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var  volume01  volume02

#可以看见我们构建的镜像也是能正常进去使用的,并且我们在dockerfile写指令挂载的目录也在这个容器里。

DockerFile

Dockerfile is used to build the docker image file! Command parameter script!
Build steps:

1. Write a dockerfile file

2. docker build. The build is called an image

3. docker run runs the image

4. Docker push publishes images (DockerHub, Alibaba Cloud Image Warehouse)

Dockerfile instructions:

FROM    #指定基础镜像,一切从这里开始构建

MAINTAINER    #镜像是谁写的,姓名+邮箱

RUN    #镜像构建的时候需要运行的命令

ADD    #步骤:tomcat镜像,这个tomcat压缩包!添加内容

WORKDIR    #镜像的工作目录

VOLUME    #挂载的目录

EXPOST    #暴露端口配置

CMD    #指定这个容器启动时候要运行的命令,Dockerfile里有多个CMD指令的话,只有最后一个CMD指令会生效,可被替代

ENTRYPOINT    #指定这个容器其董事会要运行的命令,可以追加命令

ONBUILD    #当构建一个被继承dockerfile,这个时候就会运行ONBUILD的指令。是一个触发指令

COPY    #类似ADD,将我们文件拷贝到镜像

ENV    #构建的时候设置环境变量!

docker build . command

After building the dockerfile file, we use the docker build command to build the file into an image.

#命令使用方法:
docker build -f dockerfile文件路径 -t 取个镜像名 .

#参数详解:

-f  dockerfile的文件路径,可以写绝对路径,也可以写相对路径

-t   给构建的镜像取个名字,名字格式: 名字:标签

.    构建上下文的路径 必须写

See how the image is generated

docker history 镜像ID

 Docker command summary:

Practical training: Dockerfile builds Nginx image:

Write a Dockerfile to make an image, and the generated image is named my_build:Nginx1. First create the directory dockerfile-nginx1 and save the Dockerfile file. Specific requirements are as follows:

(1) Based on the mirror centos:7

(2) Install Nginx

       a. Install related dependencies

       b. Download and unzip Nginx

(3) The working directory is set to: nginx-1.9.7

(4) Compile and install Nginx

(5) Expose ports 80 and 443 to the outside world

(6) Modify the Nginx configuration file and start it in a non-daemon mode

(7) Copy the service startup script and set permissions

(8) Execute the script run.sh when starting the container

The contents of run.sh are as follows:

Get started:

Create the dockerfile-nginx1 folder, enter the folder, and write the Dockerfile file

mkdir dockerfile-nginx1
cd dockerfile-nginx1/
vim Dockerfile

Write the contents of the Dockerfile file:

#该文件时基于centos7镜像为基础创建的
FROM centos:7  

#下载nginx依赖源
RUN yum -y install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel    

#下载wget
RUN yum install -y wget   

#下载nginx
RUN wget http://nginx.org/download/nginx-1.9.7.tar.gz   

#解压nginx
RUN tar -zxvf nginx-1.9.7.tar.gz    

#执行编译命令,通过--prefix=安装目录,指定nginx安装目录
RUN ./configure --prefix=/usr/localinx && make && make install    

#设置工作目录
WORKDIR nginx-1.9.7    

#暴露端口80和443
EXPOSE 80
EXPOSE 443

#将虚拟机文件复制到容器里
ADD run.sh /run.sh

#给run.sh授予可执行权限
RUN chmod 775 /run.sh

#启动容器后执行run.sh脚本
CMD ["/run.sh"]

Write another run.sh in this folder

vim run.sh

run.sh里的内容:
#!/bin/bash
/usr/local/nginx/sbin/nginx

docker build build image

docker build -t my_build:nginx1 .

 docker images to view the mirror:

 Build the container using this image:

 You can see that the image has been successfully started, the things have been downloaded and installed, the working directory has been set, and the files have been copied to the container.

Guess you like

Origin blog.csdn.net/weixin_53466908/article/details/124238064