ENV
[root@docker01 /opt/dockerfile]# mkdir mysql [root@docker01 /opt/dockerfile]# cd mysql [root@docker01 /opt/dockerfile/mysql]# docker run -it -p 3306:3306 centos:6.9 [root@bfd8749c5c32 /]# curl -o /etc/yum.repos.d/epel.repo [root@bfd8749c5c32 /]# curl -o /etc/yum.repos.d/CentOS-Base.repo [root@bfd8749c5c32 /]# yum install mysql-server -y [root@bfd8749c5c32 /]# service mysqld start [root@bfd8749c5c32 /]# mysqladmin -uroot password "123456" [root@bfd8749c5c32 /]# ps -ef|grep mysqld_safe root 184 1 0 11:49 pts/0 00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=http://www.likecs.com/var/lib/mysql --socket=http://www.likecs.com/var/lib/mysql/mysql.sock --pid-file=http://www.likecs.com/var/run/mysqld/mysqld.pid --basedir=http://www.likecs.com/usr --user=mysql [root@bfd8749c5c32 /]# vi init.sh /bin/sh /usr/bin/mysqld_safe --datadir=http://www.likecs.com/var/lib/mysql --socket=http://www.likecs.com/var/lib/mysql/mysql.sock --pid-file=http://www.likecs.com/var/run/mysqld/mysqld.pid --basedir=http://www.likecs.com/usr --user=mysql # 根据手动构建写dockerfile [root@docker01 /opt/dockerfile/mysql]# vim dockerfile FROM centos:6.9 RUN curl -o /etc/yum.repos.d/epel.repo RUN curl -o /etc/yum.repos.d/CentOS-Base.repo RUN yum install mysql-server -y RUN service mysqld start ADD init.sh /init.sh ENV MYSQL_ROOT=123456 CMD ["/bin/bash","/init.sh"] # 写脚本 [密码设置成传参 不要一直一样 如果不传参默认用dockerfile默认密码] [root@docker01 /opt/dockerfile/mysql]# vim init.sh #!/bin/bash service mysqld start mysqladmin -uroot password "$MYSQL_ROOT" service mysqld stop /bin/sh /usr/bin/mysqld_safe --datadir=http://www.likecs.com/var/lib/mysql --socket=http://www.likecs.com/var/lib/mysql/mysql.sock --pid-file=http://www.likecs.com/var/run/mysqld/mysqld.pid --basedir=http://www.likecs.com/usr --user=mysql # 创建镜像 [root@docker01 /opt/dockerfile/mysql]# docker build -t mysql:v1 . # 启动容器 【没有传参】 [root@docker01 /opt/dockerfile/mysql]# docker run -d mysql:v1 b69f618c245be97ba641e341aed029dd4dfd2c599d523fa6fa44977ca4bc8ea3 # 进入容器 测试 [root@docker01 ~]# docker ps -a -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c1c6a8efc1be mysql:v1 "/bin/bash /init.sh" 12 seconds ago Up 10 seconds upbeat_khorana [root@docker01 ~]# docker exec -it c1c6a8efc1be /bin/bash [root@c1c6a8efc1be /]# mysql ERROR 1045 (28000): Access denied for user \'root\'@\'localhost\' (using password: NO) [root@c1c6a8efc1be /]# mysql -uroot -p123456 mysql> # 创建镜像 [root@docker01 /opt/dockerfile/mysql]# docker build -t mysql:v3 . # 启动容器 【传参】 [root@docker01 /opt/dockerfile/mysql]# docker run -d -e "MYSQL_ROOT=123" mysql:v3 fd1101936fb73fc9c72c8c75f96157c6132266766d15c03a5b4e1c022c9d0238 # 进入容器 测试 [root@docker01 ~]# docker ps -a -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES fd1101936fb7 mysql:v3 "/bin/bash /init.sh" 6 seconds ago Up 5 seconds unruffled_varahamihira [root@docker01 ~]# docker exec -it fd1101936fb7 /bin/bash [root@fd1101936fb7 /]# mysql -uroot -p123456 ERROR 1045 (28000): Access denied for user \'root\'@\'localhost\' (using password: YES) [root@fd1101936fb7 /]# mysql -uroot -p123 mysql> 12.使用dockerfile创建云盘镜像 [root@d0c987bcefa2 /]# cat /etc/nginx/conf.d/cloud.conf server { listen 80; server_name localhost; root /code; index index.php index.html; location ~ \.php$ { root /code; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } # 1.准备需要的文件 [root@docker01 ~/dockerfile/kod]# ls CentOS-Base.repo cloud.conf dockerfile epel.repo init.sh kodexplorer4.40.zip nginx.repo # 2.编写 dockerfile [root@docker01 ~/dockerfile/kod]# cat dockerfile FROM centos:latest ADD CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo ADD epel.repo /etc/yum.repos.d/epel.repo ADD nginx.repo /etc/yum.repos.d/nginx.repo RUN yum install nginx php-fpm unzip php-gd php-mbstring -y RUN rm -rf /etc/nginx/conf.d/default.conf RUN sed -i \'/^user/c user = nginx\' /etc/php-fpm.d/www.conf RUN sed -i \'/^group/c group = nginx\' /etc/php-fpm.d/www.conf RUN sed -i \'/daemonize/s#no#yes#g\' /etc/php-fpm.conf ADD cloud.conf /etc/nginx/conf.d/cloud.conf RUN mkdir /code WORKDIR /code ADD kodexplorer4.40.zip /code/kodexplorer4.40.zip RUN unzip kodexplorer4.40.zip RUN chown -R nginx:nginx . VOLUME /code ADD init.sh /init.sh EXPOSE 80 CMD ["/bin/bash","/init.sh"] # 3.构建新镜像 [root@docker01 ~/dockerfile/kod]# docker build --network=host -t centos_kod:v1 . [root@docker01 ~/dockerfile/kod]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos_kod v1 87071ea7c86b 2 minutes ago 465MB # 4.基于新镜像运行容器 [root@docker01 ~/dockerfile/kod]# docker run -d -p 80:80 centos_kod:v1 4d1007d7194d434248762e5aca76070206643f0f0f533f2112d0359b25fe7ba 十、docker镜像的分层(kvm 链接克隆,写时复制的特性)Docker安装及简单操作 (7)
内容版权声明:除非注明,否则皆为本站原创文章。