编写Dockerfile制作镜像,生成镜像名为my_build:Nginx1。首先创建目录dockerfile-nginx1,保存Dockerfile文件。具体要求如下:
(1)基于镜像centos:7
(2)安装Nginx
a.安装相关依赖
b.下载并解压Nginx
(3)工作目录设为:nginx-1.9.7
(4)编译安装Nginx
(5)对外暴露80,443端口
(6)修改Nginx配置文件,以非daemon方式启动
(7)复制服务启动脚本并设置权限
(8)启动容器时执行脚本run.sh
run.sh内容如下所示:
题目要求如上,可先自行尝试。下面开始操作:
mkdir dockerfile_nginx1
cd dockerfile_nginx1
vim Dockerfile
cat Dockerfile
FROM centos:7
RUN yum install -y proc-devel gcc gcc-c++ zlib zlid-devel make openssl-devel wget
RUN wget http://nginx.org/download/nginx-1.9.7.tar.gz
RUN tar -zxvf nginx-1.9.7.tar.gz
WORKDIR nginx-1.9.7
RUN ./configure --prefix=/usr/local/nginx && make && make install
EXPOSE 80
EXPOSE 443
RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf
COPY run.sh /
RUN chmod 777 /run.sh
CMD ["sh","-c","/run.sh"]
vim run.sh
cat run.sh
#!/bin/bash
/usr/local/nginx/sbin/nginx
docker build -t my_build:nginx .
docker run -d -P --name my_nginx my_build:nginx
docker ps
我们根据随机分配到的端口,在浏览器访问nginx,访问方法是 虚拟机IP地址:端口号。
编写Dockerfile制作镜像,生成镜像名为my_build:Nginx2。首先创建目录dockerfile-nginx2,保存Dockerfile文件。具体要求如下:
(1)基于镜像centos:7
(2)将nginx.repo复制到容器中的yum源定义文件位置/etc/yum.repo
(3)安装Nginx
(4)修改Nginx首页信息为“Hello!This is nginx server”
(5)对外暴露80端口
(6)启动Nignx,启动命令:Nginx -g daemon off;
yum.repo文件内容如下所示:
mkdir dockerfile_nginx2
cd dockerfile_nginx2
vim Dockerfile
FROM centos:7
COPY ./nginx.repo /etc/yum.repos.d/nginx.repo
RUN yum install -y nginx
RUN sed -i 's/Welcome to nginx!/Hello!This is nginx server/g' /usr/share/nginx/html/index.html
EXPOSE 80
CMD ["nginx","-g","daemon off;"]
vim nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
docker build -t my_build:Nginx2 .
docker run -d -P my_build:Nginx2
docker ps
编写Dockerfile制作mysql镜像,生成镜像名为my_build:mysql,具体要求如下:
(1)基于镜像guyton/centos6
(2)开放端口3306
(3)设置root密码为123456
mkdir mysql
cd mysql
vim mysql_file
FROM centos:7
RUN yum localinstall -y https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm
RUN yum install -y yum-utils
RUN sed -i 's/gpgcheck=1/gpgcheck=0/g' /etc/yum.repos.d/mysql-community.repo && sed -i 's/gpgcheck=1/gpgcheck=0/g' /etc/yum.repos.d/mysql-community-source.repo
RUN yum install -y mysql-server mysql mysql-devel
COPY run.sh /run.sh
RUN chmod 777 /run.sh && /run.sh
EXPOSE 3386
CMD ["mysqld","--user=root"]
vim run.sh
cat run.sh
#! /bin/bash
rm -rf /var/lib/mysql
mysqld --initialize-insecure
chown -R mysql:mysql /var/lib/mysql
mysqld --user root &
sleep 5
mysqladmin -u root password 123456
docker build -f ./mysql_file -t my_build:mysql .
docker run -it -P my_build:mysql /bin/bash
方式二:
创建容器并以后台的方式运行:
docker run -d my_build:mysql
docker exec -it 07e4b4e9d9f5 /bin/bash
mysql -u root -p123456
show databases;