文章目录
- 一:使用Dockerfile构建镜像
- 1.1:Dockerfile常用命令解释
- 1.2:构建SSHD镜像
- 1.3:构建systemctl镜像
- 1.4:构建nginx镜像
- 1.5:构建Tomcat镜像
- 1.6:构建MySQL镜像
一:使用Dockerfile构建镜像
1.1:Dockerfile常用命令解释
指令 | 含义 |
FROM 镜像 | 指定新镜像所基于的镜像,第一条指令必须为FROM指令, 每创建一个镜像就需要一条FROM指令 |
MAINTAINER 名字 | 说明新镜像的维护人信息 |
RUN 命令 | 在所基于的镜像执行命令,并提交到新的镜像中 |
CMD [ “要运行的程序”,“参数1”,“参数2”] | 指令启动容器时要运行的命令或者脚本,Dockerfile只能 有一条CMD命令,如果指定多条则只能执行最后一条 |
EXPOSE 端口号 | 指定新镜像加载到Docker时要开启的端口 |
ENV 环境变量 变量值 | 设置一个环境变量的值,会被后面的RUN使用 |
ADD 源文件/目录 目标文件/目录 | 将源文件复制到目标文件,源文件要与Dockerfile位于 相同目录中,或者是一个URL |
COPY 源文件/目录 目标文件/目录 | 将本地主机上的文件/目录复制到目标地点,源文件/目录 要与Dockerfile在相同的目录中 |
VOLUME [“目录”] | 在容器中创建一个挂载点 |
USER 用户名/UID | 指定运行容器时的用户 |
WORKDIR 路径 | 为后续的RUN、CMD、ENTRYPOINT指定工作目录 |
ONBUILD 命令 | 指定所生成的镜像作为一个基础镜像时所要运行的命令 |
HEALTHCHECK | 健康检查 |
1.2:构建SSHD镜像
我们下载一个centos:7的镜像,进去发现无法使用sshd服务,我们需要手工添加
1、创建目录,编写Dockerfile文件
[root@docker ~]# mkdir /sshd
[root@docker ~]# cd /sshd
[root@docker sshd]# vim Dockerfile
FROM centos:7 '指定基础镜像'
MAINTAINER CHEN '描述信息'
RUN yum -y update '更新容器yum源'
RUN yum -y install openssh* net-tools lsof telnet passwd '部署环境工具'
RUN echo "123123" | passwd --stdin root '设置root登录密码'
RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config '禁用ssh中的pam验证'
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 '禁用pam的ssh的pam会话模块'
RUN mkdir -p /root/.ssh && chown root.root /root && chmod 700 /root/.ssh '创建ssh工作目录和权限设置'
EXPOSE 22 '开放22端口'
CMD ["/usr/sbin/sshd","-D"] '容器加载时启动sshd服务'
2、生成镜像,取名为sshd:new
[root@localhost sshd]# docker build -t sshd:new .
[root@localhost sshd]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
sshd new 7029ffb7629c 15 seconds ago 481MB
nginx centos7 9946f31b2ac5 4 hours ago 480MB
centos 7 7e6257c9f8d8 6 weeks ago 203MB
3、启动容器,并指定端口号为333
[root@localhost sshd]# docker run -d -p 333:22 sshd:new
1fc9de23d17d422c5a1ef61799e7e768b977c5aea00013413a9d46ce68baaa15
[root@localhost sshd]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1fc9de23d17d sshd:new "/usr/sbin/sshd -D" 27 seconds ago Up 26 seconds 0.0.0.0:333->22/tcp upbeat_bartik
4、测试登陆
[root@localhost sshd]# ssh localhost -p 333
The authenticity of host '[localhost]:333 ([::1]:333)' can't be established.
RSA key fingerprint is SHA256:3sg3lp+Uuf5cZCSnEWDPS16LYjPoRp8huULLYMQ2p9I.
RSA key fingerprint is MD5:e6:ca:37:48:08:b4:b7:99:79:c4:25:79:55:10:ce:4a.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[localhost]:333' (RSA) to the list of known hosts.
root@localhost's password:
[root@1fc9de23d17d ~]#
[root@1fc9de23d17d ~]# systemctl status sshd
Failed to get D-Bus connection: Operation not permitted '发现无法使用systemctl命令,我们需要继续构建systemctl镜像'
[root@1fc9de23d17d ~]# exit
logout
Connection to localhost closed.
[root@localhost sshd]#
1.3:构建systemctl镜像
基于SSHD镜像继续构建
1、创建目录,编写Dockerfile文件
[root@docker sshd]# mkdir /systemctl
[root@docker sshd]# cd /systemctl
[root@docker systemctl]# vim Dockerfile
FROM sshd:test
MAINTAINER built image systemctl <tang>
ENV container docker '设置环境变量,container和docker'
RUN yum install -y vim
RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *;do [ $i == \
systemd-tmpfiles-setup.service ] || rm -f $i; done); \ '执行先到指定目录,进行for循环遍历目录下所有文件并删除指定的文件。之后进行一系列的删除'
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"] 'init指执行初始化'
2、生成镜像
[root@localhost systemctl]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
systemctl new 3b610325c839 2 minutes ago 592MB
sshd new 7029ffb7629c 36 minutes ago 481MB
nginx centos7 9946f31b2ac5 5 hours ago 480MB
3、创建容器
[root@docker systemctl]# docker run --privileged -it --name systemctl -v /sys/fs/cgroup:/sys/fs/cgroup:ro systemctl:test sbin/init &
'--privateged 使container内的root拥有真正的root权限,不进行降权处理。否则,container内的用户只是外部的一个普通用户'
4、登陆检测
[root@localhost systemctl]# docker exec -it 66f098cd541c /bin/bash
[root@66f098cd541c /]# systemctl status sshd '可以使用systemctl的命令了'
● sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:sshd(8)
man:sshd_config(5)
1.4:构建nginx镜像
1、创建目录并编写Dockerfile文件
[root@docker ~]# mkdir /nginx
[root@docker ~]# cd /nginx/
[root@docker nginx]# vim Dockerfile
FROM systemctl:test
MAINTAINER build image nginx <tang>
RUN yum -y update
RUN yum -y install gcc gcc-c++ pcre* make cmake zlib-devel openssh* net-tools lsof telnet passwd vim
ADD nginx-1.12.2.tar.gz /usr/local/src
RUN useradd -M -s /sbin/nologin nginx
WORKDIR /usr/local/src/nginx-1.12.2
RUN (./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module)
RUN make && make install
ENV PATH /usr/local/nginx/sbin/:$PATH
#RUN ln -s /usr/local/nginx/sbin/ * /usr/local/sbin/
EXPOSE 80
EXPOSE 443
RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf '指关闭守护进程启动'
CMD ["/usr/local/nginx/sbin/nginx"]
[root@docker nginx]# rz -E '上传nginx源码包'
rz waiting to receive.
[root@docker nginx]# ls
Dockerfile nginx-1.12.2.tar.gz
2、生成镜像
生成镜像取名为nginx:centos7
3、启动容器,映射端口号为1314
06823a8a9623 nginx:centos7 "/run.sh" 18 hours ago Exited (255) 8 minutes ago 443/tcp, 0.0.0.0:1314->80/tcp nginx01
4、登录验证
1.5:构建Tomcat镜像
1、创建目录并编写Dockerfile文件,上传相关文件
[root@docker tomcat]# ls
apache-tomcat-9.0.16.tar.gz Dockerfile jdk-8u91-linux-x64.tar.gz
[root@docker tomcat]# vim Dockerfile
FROM centos:7
MAINTAINER this is tomcat-centos
ADD jdk-8u91-linux-x64.tar.gz /usr/local
WORKDIR /usr/local/
RUN mv jdk1.8.0_91 /usr/local/java
ENV JAVA_HOME /usr/local/java
ENV JAVA_BIN /usr/local/java/bin
ENV JRE_HOME /usr/local/java/jre
ENV PATH $PATH:/usr/local/java/bin:/usr/local/java/jre/bin
ENV CLASSPATH /usr/lcoal/java/jre/bin:/usr/local/java/lib:/usr/local/java/jre/lib/charsets.jar
ADD apache-tomcat-9.0.16.tar.gz /usr/local
WORKDIR /usr/local
RUN mv apache-tomcat-9.0.16 tomcat9
EXPOSE 8080
ENTRYPOINT ["/usr/local/tomcat9/bin/catalina.sh","run"]
2、生成镜像
[root@docker tomcat]# docker build -t tomcat:new .
3、创建容器,指定端口号
[root@localhost tomcat]# docker run -d --name tomcat01 -p 1111:8080 tomcat:new
03eb2bbb5dff2064ae4873a478887e3eb9ac98ea598b190ddbaf49c14b018e6f
4、登录验证
1.6:构建MySQL镜像
[root@docker mysql]# ls
mysql-boost-5.7.20.tar.gz
1、创建目录并编写Dockerfile文件
[root@docker mysql]# vim Dockerfile
FROM centos:7
MAINTAINER this is mysql-centos
RUN yum -y install ncurses bison cmake gcc ncurses-devel gcc-c++ make
RUN useradd mysql -M -s /sbin/nologin
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_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
RUN make && 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
RUN /usr/local/mysql/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 /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"]
2、编写配置文件
[root@docker mysql]# vim my.cnf
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock
[mysql]
port = 3306
default-character-set=utf8
socket=/usr/local/mysql/mysql.sock
[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
3、生成镜像
[root@docker mysql]# docker run -d -P --privileged mysqld:new
6860174a05539e34521ef0e30d2dd3ac557f6f5fe550fa27f02ecb731489003c
[root@docker mysql]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6860174a0553 mysqld:new "/run.sh" 8 seconds ago Up 8 seconds 0.0.0.0:32768->3306/tcp cranky_herschel
4、进入镜像中mysql数据库,进行验证
[root@docker mysql]# docker exec -it 6860174a0553 /bin/bash
[root@6860174a0553 mysql-5.7.20]# mysql -uroot -p
mysql> grant all privileges on *.* to 'root'@'%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> grant all privileges on *.* to 'root'@'localhost' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> create database school; '添加school数据库'
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| school |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
5、其它节点安装mariadb进行测试
[root@docker ~]# yum install mariadb* -y
[root@docker ~]# systemctl start mariadb.service
6、连接数据库
[root@docker ~]# mysql -h 192.168.100.129 -uroot -P 32768 -p
MySQL [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
7、数据库数据同步
MySQL [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| cllt |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)