Dockerfile文件
mkdir /root/nginx
cd /root/nginx #拉入软件包(nginx-1.12.0.tar.gz)
vim Dockerfile
FROM centos:7
MAINTAINER wt
RUN yum -y update
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
CMD ["/usr/local/nginx/sbin/nginx"]
Dockerfile文件里有几个ADD就说明需要几个文件(Dockerfile文件不包含在内)
生成镜像
docker build -t nginx:nginxweb .
启动容器
docker run -d -P nginx:nginxweb
mkdir tomcat
cd tomcat/ #拉入软件包(jdk-8u201-linux-x64.rpm apache-tomcat-9.0.16.tar.gz)
vim Dockerfile
FROM centos:7
MAINTAINER wt
ADD jdk-8u201-linux-x64.rpm /usr/local/
WORKDIR /usr/local/
RUN rpm -ivh jdk-8u201-linux-x64.rpm
ENV JAVA_HOME=/usr/java/jdk1.8.0_201-amd64
ENV CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
ENV PATH=$JAVA_HOME/bin:$PATH
ADD apache-tomcat-9.0.16.tar.gz /usr/local/
WORKDIR /usr/local/
RUN mv apache-tomcat-9.0.16 /usr/local/tomcat
EXPOSE 8080
CMD ["/usr/local/tomcat/bin/catalina.sh","run"]
#ENTRYPOINT ["/usr/local/tomcat/bin/catalina.sh","run"]
生成镜像
docker build -t tomcat:tomcatweb .
启动容器
docker run -d --name tomcatweb -p 1234:8080 tomcat:tomcatweb
mkdir sshd
cd sshd/
vim Dockerfile
FROM centos:7
MAINTAINER wt
RUN yum -y install openssh* net-tools lsof telnet passwd
RUN echo "123456" | 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"]
生成镜像
docker build -t sshd:test .
启动容器
docker run -d -P sshd:test
ssh localhost -p 端口号
mkdir systemctl
cd systemctl
vim Dockerfile
FROM sshd:test
MAINTAINER wt
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"]
生成镜像
docker build -t systemctl:systemctl .
启动容器
docker run --privateged -it -v /sys/fs/cgroup:/sys/fs/cgroup:ro systemd:systemctl /sbin/init &
登录容器进行测试
docker exec -it systemctl /bin/bash
systemctl status sshd
systemctl start sshd
systemctl status sshd
注:--privateged 使container内的root拥有真正的root权限,不进行降权处理。否则,container内的用户只是外部的一个普通用户
Dockerfile 文件
mkdir mysql
cd mysql/
vim Dockerfile
FROM centos:7
MAINTAINER wt
EXPOSE 3306
ADD mysql-boost-5.7.20.tar.gz /usr/local/src
WORKDIR /usr/local/src/mysql-5.7.20
RUN useradd mysql -M -s /sbin/nologin
RUN yum -y install gcc \
gcc-c++ \
make \
ncurses \
ncurses-devel \
bison \
cmake
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 -j 4 && make install
RUN chown -R mysql:mysql /usr/local/mysql/
RUN rm -rf /etc/my.cnf
WORKDIR /etc/
ADD my.cnf /etc/
RUN chown mysql:mysql /etc/my.cnf
ENV PATH $PATH:/usr/local/mysql/bin:/usr/local/mysql/lib
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/
RUN echo -e "#!/bin/bash \nsystemctl enable mysqld" > /run.sh
RUN chmod 755 /run.sh
RUN sh /run.sh
CMD ["init"]
MySQL配置文件
vim /mysql/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
运行脚本
vim run.sh
#!/bin/bash
/usr/local/mysql/bin/mysqld
docker build -t mysql:mysql00 .
创建容器
docker run -d --name mysql -P --privileged mysql:mysql00
重启容器
docker start 容器 id
进入容器授权
docker exec -it 容器id /bin/bash
mysql -u root -p #登录MySQL
grant all privileges on *.* to 'root'@'%' identified by '123456';
grant all privileges on *.* to 'root'@'localhost' identified by '123456';
flush privileges;
exit
在宿主机yum安装msyql
yum -y install mysql
客户端连接MySQL容器
mysql -uroot -p123456 -h 192.168.153.40 -P3306