Docker image production method and Dockerfile simple analytical Enterprise

Since the Docker's official website public warehouse mirroring mostly incomplete, can not really meet the company's production system, then we need to customize their own mirror image or repackaged. Docker mirroring is one of the essential work of the administrator, mirroring the method Docker There are two main production methods are as follows:

  • Docker commit | export will submit a new container to Images list;
  • Write Dockerfile, bulid new mirror to the mirror list;

A, commit mode

  • Download Docker base image from the warehouse;
    docker pull docker.io/jdeathe/centos-ssh

  • Docker run base image generating vessel;
    docker run -itd -p 6022:22 --privileged -name aliyun-server docker.io/jdeathe/centos-ssh

  • Into the container - install software, add features, create directories, files;

#这里可以将你这个虚拟机需要的基础环境安装上,比如部署一个LNMP架构,或者我只想用一个web服务器,只用安装一个nginx即可!
yum install wget zlib-devel pcre-devel gcc-c++ -y
wget -O /usr/local/nginx-1.16.0.tar.gz http://nginx.org/download/nginx-1.16.0.tar.gz
tar xvf /usr/local/nginx-1.16.0.tar.gz
cd /usr/local/nginx-1.16.0
./configure --prefix=/usr/local
make -j4&make install
wget -O /usr/local/nginx/html/index.html http://www.baidu.com
echo "PATH=$PATH:/usr/local/nginx/sbin/" >>/etc/profile
source /etc/profile
  • Docker commit container ID, the container will be submitted to the new image;
    docker commit c432b9da999c
    Here Insert Picture DescriptionHere Insert Picture Description
  • Docker save the image list image is introduced into the tar package;
    docker save centos-v2:latest -o /root/centos-v2.tar
  • Mirror tar package Docker load, import mirror list.
    Note: The first scp to the target server, and then perform the following operation
    docker load -i /root/centos-v2.tar

Two, Export mode

  • Download Docker base image from the warehouse;
  • Docker run base image generating vessel;
  • Into the container - install software, add features, create directories, files;
  • Docker export container ID, the container is exported as image tar package (loss of memory in a data file, the enterprise is not recommended for the program);
docker ps
docker export c432b9da999c(容器ID) >/root/centos-v3.tar
  • Docker import the image tar package, introduced mirror list.
 cat /root/centos-v3.tar|docker import - centos-v3
 -:占位符
 centos-v3:指定REPOSITORY

Here Insert Picture DescriptionNote: the differences between the above two methods, commit to a multi vessel be submitted to a step mirror, the Export directly into the vessel and then deriving image, the synthesis step. But this is convenient, but there may be loss of data, so generally do not use the Export method!

Three, Dockerfile enterprise-class production method (recommended)

1, Dockerfile production principle

将基于一个基础镜像,通过编写Dockerfile方式,将各个功能进行叠加,最终形成新的Docker镜像,是目前互联网企业中打包镜像最为推荐的方式。

Dockerfile representation is a mirror, the mirror is a raw material, can be described by Dockerfile constructed image, and automatically build a container.

2, as follows mirrored DockerFile, Detailed instructions and parameters necessary:

FROM	 		指定所创建镜像的基础镜像;
MAINTAINER		指定维护者信息;
RUN				运行命令;
CMD				指定启动容器时默认执行的命令;
LABEL			指定生成镜像的元数据标签信息;
EXPOSE			声明镜像内服务所监听的端口;
ENV				指定环境变量;
ADD				赋值指定的<src>路径下的内容到容器中的<dest>路径下,<src>可以为URL;如果为tar文件,会自动解压到<dest>路径下
COPY			赋值本地主机的<scr>路径下的内容到容器中的<dest>路径下;一般情况下推荐使用COPY而不是ADD;
ENTRYPOINT		指定镜像的默认入口;
VOLUME			创建数据挂载点;
USER			指定运行容器时的用户名或UID;
WORKDIR			配置工作目录;
ARG				指定镜像内使用的参数(例如版本号信息等);
ONBUILD			配置当前所创建的镜像作为其他镜像的基础镜像时,所执行的创建操作的命令;
STOPSIGNAL		容器退出的信号;
HEALTHCHECK		如何进行健康检查;
SHELL			指定使用SHELL时的默认SHELL类型;

3, Dockerfile mirrored specifications and techniques are as follows:

  • Compact Mirror uses: Try to make use of each image are more concentrated, single, avoid large and complex structure, multi-function mirror;
  • Appropriate choice of base image: too much can cause build a base image bloated mirror, the mirror is generally recommended as a relatively small base image;
  • Detailed comments and maintainer information: Dockerfile also a code to facilitate the subsequent expansion and use by others to be considered;
  • Proper use of version numbers: use explicit version number information specific digital information, rather than the latest, to avoid not confirm the specific version number, unified environment;
  • Reducing the number of layers of the mirror: Mirror layers recommended to reduce the combined RUN command, the content may be a plurality of instruction RUN is connected by &&;
  • Promptly delete temporary files and cache: This avoids constructed mirror too bloated, and these cache files and no practical use;
  • Increase production speed: the rational use of cache, reducing the use of files in a directory, use .dockeringore documents;
  • Reasonable adjustment instruction sequence: in the case of caching enabled, the contents of the instruction as far as possible in front of the same, which can improve reusability instructions;
  • Reduce interference from external sources: If you really want to introduce external data, address the need to develop long-lasting, and with version information, so that others can be reused without error.

4, DockerFile a business case

Business needs: Start Docker container, while open external monitor port 22 Docker containers, achieved through CRT or Xshell login.
Write Dockerfile file:

# 设置基本的镜像,后续命令都以这个镜像为基础
FROM centos:v1
# 作者信息
MAINTAINER  wujincheng@163.com

# RUN命令会在上面指定的镜像里执行任何命令
RUN rpm --rebuilddb;yum install passwd openssl openssh-server -y
RUN echo '123456' | passwd --stdin root
RUN mkdir -p /root/.ssh && chown root.root /root && chmod 700 /root/.ssh

# 暴露ssh端口22
EXPOSE  22

# 设定运行镜像时的默认命令:输出ip,-D并以daemon方式启动sshd
CMD /usr/sbin/sshd -D;/bin/bash

5, DockerFile enterprise Case II

Business needs: open SSH 6379 port, allowing access to the ports of Redis, Dockerfile as follows:
write Dockerfile file:

# 设置基本的镜像,后续命令都以这个镜像为基础
FROM centos_lamp:v1 
# 作者信息
MAINTAINER  wujincheng@163.com

# RUN命令会在上面指定的镜像里执行任何命令
RUN rpm --rebuilddb;yum install redis* -y
RUN sed -i '/bind/127.0.0.1/0.0.0.0/g' /etc/redis.conf 

#暴露ssh端口6379
EXPOSE  6379

#-D设定运行以daemon方式启动sshd
CMD /usr/sbin/redis -D

6, DockerFile enterprise Case III

Business needs: based on Dockerfile open port 80 Apache, and remotely connect to the server
to write Dockerfile file:

# 设置基本的镜像,后续命令都以这个镜像为基础
FROM centos_lamp:v1
# 作者信息
MAINTAINER  wujincheng@163.com

# RUN命令会在上面指定的镜像里执行任何命令
RUN rpm --rebuilddb;yum install pcre-devel -y
RUN rpm --rebuilddb;yum install httpd httpd-devel –y
RUN  echo “<h1>The Test Page JFEDU</h1>” >>/var/www/html/index.html

#暴露ssh端口80
EXPOSE 80

#启动httpd
CMD ["/usr/sbin/apachectl", "-D", "FOREGROUND"]

7, DockerFile enterprise Case Four

Business needs: Docker virtualization, how to build our MYSQL database server? The answer is simple, we can generate a dockerfile mysql mirror and start the run.
Write Dockerfile file:

FROM centos:v1
RUN groupadd -r mysql && useradd -r -g mysql mysql
RUN rpm --rebuilddb;yum install -y gcc zlib-devel gd-devel
ENV MYSQL_MAJOR 5.6
ENV MYSQL_VERSION 5.6.20
RUN 
	&& curl -SL "http://dev.mysql.com/get/Downloads/MySQL-$MYSQL_MAJOR/mysql-$MYSQL_VERSION-linux-glibc2.5-x86_64.tar.gz" -o mysql.tar.gz \
	&& curl -SL "http://mysql.he.net/Downloads/MySQL-$MYSQL_MAJOR/mysql-$MYSQL_VERSION-linux-glibc2.5-x86_64.tar.gz.asc" -o mysql.tar.gz.asc \
	&& mkdir /usr/local/mysql \
	&& tar -xzf mysql.tar.gz -C /usr/local/mysql \
	&& rm mysql.tar.gz* \
ENV PATH $PATH:/usr/local/mysql/bin:/usr/local/mysql/scripts
WORKDIR /usr/local/mysql
VOLUME /var/lib/mysql
EXPOSE 3306
CMD ["mysqld", "--datadir=/var/lib/mysql", "--user=mysql"]

8. The image generating dockerfile

Create a mirror (centos: ssh) with docker build according to Dockerfile:

#这种生成方式是用来指定名字不是Dockerfile的创建方式,-为占位符
docker  build  -t  [REPOSITORY]:[TAG]  -  <  /etc/dockerfile
#文件名默认为Dockerfile,所以不用指定,指定位置即可
docker  build  -t  [REPOSITORY]:[TAG]  .(指定位置为当前)
#注:-t是用来指定 Name and optionally a tag in the 'name:tag' format (default [])

Here Insert Picture Description

He published 188 original articles · won praise 150 · views 30000 +

Guess you like

Origin blog.csdn.net/weixin_44571270/article/details/104358577