部署SSH镜像
[root@docker01 ~]# mkdir /opt/sshd
[root@docker01 ~]# cd /opt/sshd
[root@docker01 sshd]# vim Dockerfile
#第一行必须指明基于的基础镜像
FROM centos:7
#作者信息
MAINTAINER this is ssh image <ly>
#镜像的操作指令
RUN yum -y update
RUN yum -y install openssh* net-tools lsof telnet passwd
RUN echo 'abc123' | passwd --stdin root
RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config #不使用PAM认证
RUN sed -ri '/^session\s+required\s+pam_loginuid.so/s/^/#/' /etc/pam.d/sshd #取消pam限制
RUN ssh-keygen -t rsa -A #生成密钥认证文件
RUN mkdir -p /root/.ssh && chown root.root /root && chmod 700 /root/.ssh
EXPOSE 22
CMD ["/usr/sbin/sshd" , "-D"]
[root@docker01 sshd]# docker build -t sshd:centos .
## 生成镜像
[root@docker01 sshd]# docker run -d -P sshd:centos
7d36be5293dd34e8b36ec874ecd7af4ed7bd56577693bedc5ea4351fb46a6d1e
[root@docker01 sshd]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7d36be5293dd sshd:centos "/usr/sbin/sshd -D" 29 seconds ago Up 29 seconds 0.0.0.0:32768->22/tcp, :::32768->22/tcp jolly_kepler
[root@docker01 sshd]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7d36be5293dd sshd:centos "/usr/sbin/sshd -D" About a minute ago Up About a minute 0.0.0.0:32768->22/tcp, :::32768->22/tcp jolly_kepler
[root@docker01 sshd]# ssh localhost -p 32768
The authenticity of host '[localhost]:32768 ([::1]:32768)' can't be established.
ECDSA key fingerprint is SHA256:WAUvkLF8ahArOSCRAmVHrBqjQMtyetaTPF4F2MgA50Q.
ECDSA key fingerprint is MD5:53:70:6a:be:9f:93:44:0d:ca:38:df:96:25:81:a8:5d.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[localhost]:32768' (ECDSA) to the list of known hosts.
root@localhost's password:
[root@7d36be5293dd ~]# ls
anaconda-ks.cfg
注意:
yum -y update(所有都升级和改变)
升级所有包,系统版本和内核,改变软件设置和系统设置
部署Systemctl镜像
[root@docker01 ~]# mkdir /opt/systemctl
[root@docker01 ~]# cd /opt/systemctl
[root@docker01 systemctl]# vim Dockerfile
FROM sshd:centos
MAINTAINER this is systemctl image <ly>
ENV container docker
#除了systemd-tmpfiles-setup.service,删除其它所有文件
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"]
[root@docker01 systemctl]# docker build -t systemd:centos .
[root@docker01 systemctl]# docker run --privileged -it -v /sys/fs/cgroup:/sys/fs/cgroup:ro systemd:centos /sbin/init &
//启动容器,并挂载宿主机目录挂载到容器中,和进行初始化
[1] 7249
#--privileged:使container内的root拥有真正的root权限。否则,container内的root只是外部的一个普通用户权限。
[root@docker01 systemctl]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
87c87e354631 systemd:centos "/sbin/init" 44 seconds ago Up 40 seconds 22/tcp awesome_bartik
7d36be5293dd sshd:centos "/usr/sbin/sshd -D" 14 minutes ago Up 14 minutes 0.0.0.0:32768->22/tcp, :::32768->22/tcp jolly_kepler
[root@docker01 systemctl]# docker exec -it 87c87e354631 bash
[root@87c87e354631 /]# systemctl status sshd
● 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)
方法二:
docker run -d -P --privileged sshd:centos /usr/sbin/init &
docker run中-v参数的用法解释
宿主机绑定: -v::[rw|ro]
表示宿主目录
表示容器目录
部署Nginx镜像
[root@docker01 ~]# mkdir /opt/nginx
[root@docker01 ~]# cd /opt/nginx/
[root@docker01 ~]# cd /opt/nginx/
[root@docker01 nginx]# rz -E
rz waiting to receive.
[root@docker01 nginx]# ls
nginx-1.12.0.tar.gz
[root@docker01 nginx]# vim Dockerfile
#基于基础镜像
FROM centos:7
#用户信息
MAINTAINER this is nginx image <ly>
#添加环境包
RUN yum -y update
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make
RUN useradd -M -s /sbin/nologin nginx
#上传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
#指定http和https端口
EXPOSE 80
EXPOSE 443
RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf #关闭 nginx 在后台运行
#添加宿主机中run.sh到容器中
ADD run.sh /run.sh
RUN chmod 755 /run.sh
CMD ["/run.sh"]
[root@docker01 nginx]# vim run.sh
#!/bin/bash
/usr/local/nginx/sbin/nginx
[root@docker01 nginx]# docker build -t nginx:centos .
##创建新镜像
注意:出现此问题需重启docker服务
[root@docker01 nginx]# docker run -d -P nginx:centos
2d66e57d821933b0354039a37dfba2cea3cdbb0ff0034db626b9921cc454df71
[root@docker01 nginx]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2d66e57d8219 nginx:centos "/run.sh" 6 seconds ago Up 5 seconds 0.0.0.0:32769->80/tcp, :::32769->80/tcp, 0.0.0.0:32768->443/tcp, :::32768->443/tcp objective_cray
浏览器测试输入:http://20.0.0.100:32769/
部署tomcat 镜像
[root@docker01 ~]# mkdir /opt/tomcat
[root@docker01 ~]# cd /opt/tomcat
[root@docker01 tomcat]# rz -E
rz waiting to receive.
[root@docker01 tomcat]# ls
apache-tomcat-8.5.16.tar.gz jdk-8u91-linux-x64.tar.gz
[root@docker01 tomcat]# vim Dockerfile
FROM centos:7
MAINTAINER this is tomcat image <tomcat>
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 JRE_HOME ${JAVA_HOME}/jre
ENV CLASSPATH .:${JAVA_HOME}/lib:${JRE_HOME}/lib
ENV PATH $JAVA_HOME/bin:$PATH
ADD apache-tomcat-8.5.16.tar.gz /usr/local/
WORKDIR /usr/local/
RUN mv apache-tomcat-8.5.16 /usr/local/tomcat
EXPOSE 8080
#CMD ["/usr/local/tomcat/bin/catalina.sh","run"]
ENTRYPOINT ["/usr/local/tomcat/bin/catalina.sh","run"]
[root@docker01 tomcat]# docker build -t tomcat:centos .
## 创建新镜像
[root@docker01 tomcat]# docker run -d --name tomcat1 -p 1315:8080 tomcat:centos
814c907ba1312eddf1b7750b8ce16c5451ff9077b90a470d0b28dc69106b79ce
浏览器测试输入:http://20.0.0.100:1315/
部署mysql镜像
[root@docker01 ~]# mkdir /opt/mysqld
[root@docker01 ~]# cd /opt/mysqld
[root@docker01 mysqld]# ls
Dockerfile my.cnf mysql-boost-5.7.20.tar.gz run.sh
[root@docker01 mysqld]# vim Dockerfile
FROM centos:7
MAINTAINER this is mysql image <mysql>
RUN yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake 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_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 && 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
ADD run.sh /usr/local/src
RUN chmod 755 /usr/local/src/run.sh
RUN sh /usr/local/src/run.sh
#CMD ["/usr/sbin/init"]
[root@docker01 mysqld]# vim my.cnf
[client]
port = 3306
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
[root@docker01 mysqld]# vim run.sh
#!/bin/bash
/usr/local/mysql/bin/mysqld
systemctl enable mysqld
[root@docker01 mysqld]# docker build -t mysql:centos .
## 创建新镜像
[root@docker01 mysqld]# docker run --name=mysql_server -d -P --privileged mysql:centos /usr/sbin/init &
## 启动容器,并进行初始化
[root@docker01 mysqld]# docker ps -a
## 进容器给权限
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bfa8b848fef7 mysql:centos "/usr/sbin/init" 44 seconds ago Up 42 seconds 0.0.0.0:32768->3306/tcp, :::32768->3306/tcp mysql_server
[root@docker01 mysqld]# docker exec -it bfa8b848fef7 /bin/bash
[root@bfa8b848fef7 mysql]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20 Source distribution
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> grant all privileges on *.* to 'root'@'%' identified by 'abc123';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> grant all privileges on *.* to 'root'@'localhost' identified by 'abc123';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
在客户端连接mysql容器
[root@localhost ~]# yum install mariadb mariadb-server -y
## 安装mariadb
[root@localhost ~]# systemctl start mariadb.service
## 开启mariadb
[root@localhost ~]# mysql -h 20.0.0.100 -u root -P 32768 -pabc123
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.20 Source distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]>
## 远程登录mysql成功