Take you in-depth understanding of Docker mirroring. After reading it, you will be as proficient as Pao Ding Jie Niu

First, the layering of Docker images

Insert picture description here

Second, the creation of Docker image

1. Docker image

(1) Standard format for application release
(2) Support the operation of a Docker container

2. How to create a Docker image

(1) Create based on existing image
(2) Create based on local template
(3) Create based on Dockerfile

Three, create based on the existing image

1. Package the program and operating environment running in the container to generate a new image
docker commit [Options] Container ID/Name Warehouse name: [label]
-m Description information
-a Author information
-p Stop the operation of the container during the generation process

2. Specific experimental operation

创建容器
docker create -it centos:7 /bin/bash
docker commit -m "new" -a "daoke" be93f7652231 daoke:test
docker images

Insert picture description here

Fourth, create based on local modules

1. Generate a new image by importing the operating system template file
2. Use the wget command to import as a local image

wget
http://download.openvz.org/template/precreated/debian-7.0-x86-minimal.tar.gz

3. After the import is successful, you can view the local mirror message

docker imagges |grep new

Five, create based on Dockerfile

1. Dockerfile is a file composed of a set of instructions

2. Four parts of Dockerfile structure

(1) Basic image information
(2) Maintainer information
(3) Mirror operation instructions
(4) Execution instructions when the container is started
Note: Each line of Dockerfile supports one instruction, each instruction can carry multiple parameters, and supports the use of a "#" The comment at the beginning of the sign.

3. Dockerfile operation instructions

instruction meaning
FROM mirror Specify the image on which the new image is based. The first instruction must be the FROM instruction. Every time you create an image, you need a FROM instruction.
MAINTAINER name Describe the maintainer information of the new image
RUN command Execute the command on the mirror based on it and submit it to the new mirror
CMD["program to run", "parameter 1", "parameter 2"] The command or script to be run when the instruction starts the container, the Dockerfile can only have one CMD command, if more than one is specified, only the last one can be executed
EXPOSE port number Specify the port to be opened when the new image is loaded into Docker
ENV environment variable variable value Set the value of an environment variable, which will be used by RUN later
ADD source file/directory target file/directory Copy the source file to the target file, the source file must be located in the same directory as the Dockerfile, or a URL
COPY source file/directory target file/directory Copy the file/directory on the local host to the target location, the source file/directory should be in the same directory as the Dockerfile
VOLUME ["Directory"] Create a mount point in the container
USER user/UID Specify the user when running the container
WORKDIR path Specify the working directory for subsequent RUN, CMD, ENTRYPOINT
ONBUILD command Specify the command to run when the generated image is used as a base image
HEALTHCHECK health examination

4. Use dockerfile to create a mirror

(1)apache

Create apache directory
mkdir apache
cd apache

Insert picture description here

Write dockerfile file
vim dockerfile
#基于基础镜像
From centos:7
#维护镜像的用户信息
MAINTAINER this is test
#镜像的操作指令:安装apache软件
RUN yum -y install httpd
#开启80端口
EXPOSE 80
#复制网站首页文件
ADD index.html /var/www/html/index.html
#执行脚本并复制到镜像中
ADD run.sh /run.sh
RUN chmod 777 /run.sh
#启动容器时执行脚本
CMD ["/run.sh"]

Insert picture description here

Write run.sh script
vim run.sh
#!/bin/bash
rm -rf /run/httpd/*
exec /usr/sbin/apachectl -D FOREGROUND

Insert picture description here

Prepare the home page
echo "this is 17team web" >index.html
Generate mirror
docker build -t httpd:centos .				#末尾别忘记“.”

Insert picture description here

Run a new image container and check it through the web page
docker run -d -P httpd:centos 					#-P随机端口
docker run -d -p 1213 httpd:centos         #-p指定端口

Insert picture description here

##使用浏览器输入:192.168.177.33:xxxx

Insert picture description here

(2) Build an SSH image

Create SSH directory
mkdir sshd
cd sshd

Insert picture description here

Create a dockerfile file
vim dockerfile
FROM centos:7
MAINTAINER ssh_test
RUN yum -y install openssh* net-tools lsof telnet passwd
RUN echo "123123" | passwd --stdin root
RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
RUN sed -i '/^session\s\+required\s\+pam_loginuid.so/s/^/#/' /etc/pam.d/sshd
RUN mkdir -p /root/.ssh && chown root:root /root && chmod 700 /root/.ssh
EXPOSE 22
CMD ["/usr/sbin/sshd","-D"]

Insert picture description here

Generate mirror
docker build -t sshd:new .

Insert picture description here

Start the container and log in
docker run -d -P sshd:new
ssh localhost -p xxxx

Insert picture description here

(3) Build systemctl image

Create systemctl directory
mkdir systemctl
cd systemctl

Insert picture description here

Create dockerfile
vim Dockerfile
FROM sshd:new
ENV container docker
RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *;do [ $i== \
systemd-tmpfiles-setup.service ] || rm -f $i;done);\
rm -f /lib/systemd/system/multi-user.target.wants/*;\
rm -f /etc/systemd/system/*.wants/*;\
rm -f /lib/systemd/system/local-fs.target.wants/*;\
rm -f /lib/systemd/system/sockets.target.wants/*udev*;\
rm -f /lib/systemd/system/sockets.target.wants/*initctl*;\
rm -f /lib/systemd/system/basic.target.wants/*;\
rm -f /lib/systemd/system/anaconda.target.wants/*;
VOLUME ["/sys/fs/cgroup"]
CMD ["/usr/sbin/init"]

Insert picture description here

Build image
docker build -t systemd:new .
privateged container 内的root拥有真正的root权限,否则,container内的root只是外部的一个普通用户权限
docker run --privateged -it -v /sys/fs/cgroup:/sys/fs/cgroup:ro systemd:new /sbin/init & 
Enter the container
docker exec -it 容器id bash
test
systemctl status sshd

(4) Make nginx mirror

Create nginx directory

mkdir nginx
cd nginx

Create a dockerfile file

vim Dockerfile
FROM centos:7
MAINTAINER test
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make
RUN useradd -M -s /sbin/nologin nginx
ADD nginx-1.12.0.tar.gz /usr/local/src
WORKDIR /usr/local/src/nginx-1.12.0
RUN ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module && make && make install
ENV PATH /usr/local/nginx/sbin:&PATH
EXPOSE 80
EXPOSE 443
RUN echo "daemon off;" >>/usr/local/nginx/conf/nginx.conf
ADD run.sh /run.sh
RUN chmod 777 /run.sh
CMD ["/run.sh"]

Create startup script

vim run.sh
#!/bin/bash
/usr/local/nginx/sbin/nginx

Create a new image and test

docker build -t nginx:new
docker run -d -P nginx:new

192.168.177.8:xxxx

(5) Make tomcat mirror

Create tomcat directory

mkdir tomcat
cd tomcat

Create a dockerfile file

vim Dockerfile
FROM centos:7
MAINTAINER test
ADD jdk-8u91-linux-x64.tar.gz /opt
ADD apache-tomcat-9.0.16.tar.gz /usr/local/src
RUN mv /opt/jdk1.8.0_91 /usr/local/java
RUN mv /usr/local/src/apache-tomcat-9.0.16 /usr/local/tomcat
ENV JAVA_HOME /usr/local/java
ENV CLASSPATH $JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
ENV PATH $JAVA_HOME/bin:$PATH
EXPOSE 8080
ENTRYPOINT ["/usr/local/tomcat/bin/catalina.sh","run"]

Make a mirror

docker buil -t tomcat:test .

Run the container and visit in the browser

docker run -d -P tomcat:test
192.168.177.8:49152

(6) Make mysql mirror

Create mysql directory

mkdir mysql
cd mysql

Create my.cnf file

vim Dockerfile
FROM centos:7
MAINTAINER test
ADD jdk-8u91-linux-x64.tar.gz /opt
ADD apache-tomcat-9.0.16.tar.gz /usr/local/src
RUN mv /opt/jdk1.8.0_91 /usr/local/java
RUN mv /usr/local/src/apache-tomcat-9.0.16 /usr/local/tomcat
ENV JAVA_HOME /usr/local/java
ENV CLASSPATH $JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
ENV PATH $JAVA_HOME/bin:$PATH
EXPOSE 8080
ENTRYPOINT ["/usr/local/tomcat/bin/catalina.sh","run"]

Create a dockerfile file

vim Dockerfile
FROM centos:7
MAINTAINER this is test
RUN yum -y install ncurses ncurses-devel bison cmake gcc gcc-c++ make
RUN useradd -M -s /sbin/nologin mysql
ADD mysql-boost-5.7.20.tar.gz /usr/local/src
WORKDIR /usr/local/src/mysql-5.7.20
RUN cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8  \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1 && make -j2 && make install
RUN chown -R mysql:mysql /usr/local/mysql
RUN rm -rf /etc/my.cnf
ADD my.cnf /etc
RUN chown mysql:mysql /etc/my.cnf
ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
WORKDIR /usr/local/mysql
RUN bin/mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
RUN cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
EXPOSE 3306
RUN echo -e "#!/bin/sh \nsystemctl enable mysqld" > /run.sh
RUN chmod 755 /run.sh
RUN sh /run.sh
CMD ["init"]

Make a mirror

docker build -t mysql:new .

Start the container

docker run --name=mysql_server -d -P --privileged mysql:new

Enter the container to give permission

grant all privileges on *.* 'root' @ '%' identified by 'abc123';
grant all privileges on *.* 'root' @ 'localhost' identified by 'abc123';

Install the mairadb client on the host system to connect to the mysql container

mysql -h 192.168.177.8 -u root -P xxxx -pabc123

Guess you like

Origin blog.csdn.net/tefuiryy/article/details/115177957