技术交流QQ群:①185473046   ②190706903   ③203744115   网站地图
登录

下次自动登录
现在的位置: 首页Docker>正文
Docker下使用Dockerfile基于centos基础镜像构建mysql-8.0容器镜像
2021年09月07日 Docker 暂无评论 ⁄ 被围观 6,944次+

一、CentOS 7.x 安装Docker-ce社区版本

https://www.osyunwei.com/archives/11592.html

二、准备安装软件包

cd /usr/local/src #建议先把需要安装的软件包下载到本地目录

下载mysql二进制安装包

wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz

三、构建mysql容器镜像

1、从docker hub拉取官方基础镜像

#我们用centos系统进行构建

https://hub.docker.com/_/centos?tab=tags&page=1&ordering=last_updated

选择centos:7.9.2009版本

docker pull centos:7.9.2009 #拉取系统镜像

docker image ls #查看docker镜像

docker run -itd --name centos centos:7.9.2009 #运行容器

docker ps #查看容器

[root@master01 src]# docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

3c79f14bf82f centos:7.9.2009 "/bin/bash" 8 seconds ago Up 7 seconds centos

docker exec -it 3c79f14bf82f /bin/bash #进入容器

exit #退出容器

2、编写Dockerfile构建文件

cd /usr/local/src #进入目录

vi /usr/local/src/dockerfile #dockerfile文件必须和上一步下载的软件包在同一目录

#基于这个镜像进行操作

FROM centos:7.9.2009

#作者和邮箱

MAINTAINER osyunwei osyunwei@osyunwei.com

#设置时区

RUN \cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone && echo 'Zone=Asia/Shanghai' >/etc/sysconfig/clock

#安装依赖包

#gcc \

#gcc-c++ \

#make \

#bison \

#ncurses \

#ncurses-devel \

#openssl \

#openssl-devel \

#perl-Data-Dumper \

#perl \

#perl-devel \

#libaio \

#net-tools \

#libtirpc-devel \

#diffutils \

#numactl \

RUN set -xe \

&& buildDeps=" \

libaio-devel \

numactl-devel \

" \

&& yum install -y ${buildDeps} \

&& yum clean all

#设置变量

ENV mysql=mysql-8.0.26-linux-glibc2.12-x86_64

#二进制安装mysql

#创建mysql运行用户和组

RUN groupadd mysql && useradd -g mysql mysql -s /bin/false \

#创建mysql安装目录

&& mkdir -p /usr/local/mysql \

#设置mysql数据库存放目录及权限

&& mkdir -p /data/mysql \

&& chown -R mysql:mysql /data/mysql

#安装mysql

#指定容器里面的路径,为后面的RUN、CMD或者ENTERPOINT操作指定目录

WORKDIR /usr/local/src

COPY $mysql.tar.xz ./

RUN tar -xvf $mysql.tar.xz -C /usr/local/mysql --strip-components 1 \

&& rm -rf /usr/local/src/$mysql \

&& rm -rf /usr/local/src/$mysql.tar.xz

#配置mysql

RUN rm -rf /etc/my.cnf \

&& touch /usr/local/mysql/my.cnf \

&& echo '[client]' >> /usr/local/mysql/my.cnf \

&& echo 'port=3306' >> /usr/local/mysql/my.cnf \

&& echo 'socket=/tmp/mysql.sock' >> /usr/local/mysql/my.cnf \

&& echo '[mysqld]' >> /usr/local/mysql/my.cnf \

&& echo 'datadir=/data/mysql' >> /usr/local/mysql/my.cnf \

&& echo 'port=3306' >> /usr/local/mysql/my.cnf \

&& echo 'user = mysql' >> /usr/local/mysql/my.cnf \

&& echo 'socket=/tmp/mysql.sock' >> /usr/local/mysql/my.cnf \

&& echo 'tmpdir = /tmp' >> /usr/local/mysql/my.cnf \

&& echo 'key_buffer_size=16M' >> /usr/local/mysql/my.cnf \

&& echo 'max_allowed_packet=128M' >> /usr/local/mysql/my.cnf \

&& echo 'default_authentication_plugin=mysql_native_password' >> /usr/local/mysql/my.cnf \

&& echo 'open_files_limit = 60000' >> /usr/local/mysql/my.cnf \

&& echo 'explicit_defaults_for_timestamp' >> /usr/local/mysql/my.cnf \

&& echo 'server-id = 1' >> /usr/local/mysql/my.cnf \

&& echo 'character-set-server = utf8' >> /usr/local/mysql/my.cnf \

&& echo 'federated' >> /usr/local/mysql/my.cnf \

&& echo 'max_connections = 1000' >> /usr/local/mysql/my.cnf \

&& echo 'max_connect_errors = 100000' >> /usr/local/mysql/my.cnf \

&& echo 'interactive_timeout = 86400' >> /usr/local/mysql/my.cnf \

&& echo 'wait_timeout = 86400' >> /usr/local/mysql/my.cnf \

&& echo 'sync_binlog=0' >> /usr/local/mysql/my.cnf \

&& echo 'back_log=100' >> /usr/local/mysql/my.cnf \

&& echo 'default-storage-engine = InnoDB' >> /usr/local/mysql/my.cnf \

&& echo 'log_slave_updates = 1' >> /usr/local/mysql/my.cnf \

&& echo 'key_buffer_size=16M' >> /usr/local/mysql/my.cnf \

&& echo '[mysqldump]' >> /usr/local/mysql/my.cnf \

&& echo 'quick' >> /usr/local/mysql/my.cnf \

&& echo '[client]' >> /usr/local/mysql/my.cnf \

&& echo '# The following password will be sent to all standard MySQL clients' >> /usr/local/mysql/my.cnf \

&& echo 'password="my password"' >> /usr/local/mysql/my.cnf \

&& echo '[mysqld-8.0]' >> /usr/local/mysql/my.cnf \

&& echo 'sql_mode=TRADITIONAL' >> /usr/local/mysql/my.cnf \

&& echo '[mysqladmin]' >> /usr/local/mysql/my.cnf \

&& echo 'force' >> /usr/local/mysql/my.cnf \

&& ln -s /usr/local/mysql/my.cnf /etc/my.cnf \

#初始化数据库

&& /usr/local/mysql/bin/mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql --initialize-insecure \

&& ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql \

&& ln -s /usr/local/mysql/include/mysql /usr/include/mysql \

&& mkdir /var/lib/mysql \

&& ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock \

&& echo 'export PATH=$PATH:/usr/local/mysql/bin' >> /etc/profile \

&& source /etc/profile \

&& echo 'source /etc/profile'>> /root/.bashrc

#切换到根目录

WORKDIR /root

#设置mysql运行端口

EXPOSE 3306

#设置mysql前台方式运行

CMD ["/usr/local/mysql/bin/mysqld_safe"]

:wq! #保存退出

3、构建容器

docker build --no-cache -t osyunwei/mysql:8.0.26 /usr/local/src

或者

docker build -t osyunwei/mysql:8.0.26 .

4、运行容器

docker image ls #查看docker镜像

docker run -itd --name mysql osyunwei/mysql:8.0.26 #运行容器

docker ps #查看容器

[root@master01 src]# docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

16a32fac0b6f osyunwei/mysql:8.0.26 "/usr/local/mysql/bi…" 7 seconds ago Up 6 seconds 3306/tcp mysql

docker exec -it 16a32fac0b6f /bin/bash #进入容器

#测试mysql容器

#mysql免密登录

/usr/local/mysql/bin/mysql -p -S /tmp/mysql.sock

#设置root登录

create user root@'%' identified by '';

grant all privileges on *.* to root@'%' with grant option;

flush privileges;

exit #退出mysql

exit #退出容器

5、上传mysql容器镜像到阿里云容器仓库

docker image ls #查看镜像id

[root@master01 src]# docker image ls

REPOSITORY TAG IMAGE ID CREATED SIZE

osyunwei/mysql 8.0.26 63cbda15df45 5 minutes ago 2.87GB

5.1重命名镜像标签

docker tag 63cbda15df45 registry.cn-hangzhou.aliyuncs.com/osyunwei/mysql:8.0.26

#registry.cn-hangzhou.aliyuncs.com是阿里云容器仓库地址

#osyunwei是自己创建的命名空间

#mysql是自己创建的仓库名称

#:8.0.26是自定义php-fpm镜像的版本号

5.2登录阿里云仓库

docker login --username=你的阿里云登录账号 registry.cn-hangzhou.aliyuncs.com

[root@master01 src]# docker login --username=你的阿里云登录账号 registry.cn-hangzhou.aliyuncs.com

Password:刚才设置的固定密码

WARNING! Your password will be stored unencrypted in /root/.docker/config.json.

Configure a credential helper to remove this warning. See

https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

[root@master01 src]#

5.3上传镜像到阿里云仓库

docker push registry.cn-hangzhou.aliyuncs.com/osyunwei/mysql:8.0.26

5.4拉取阿里云容器镜像

docker pull registry.cn-hangzhou.aliyuncs.com/osyunwei/mysql:8.0.26

至此,Docker下使用Dockerfile基于centos基础镜像构建mysql-8.0容器镜像教程完成。

     

  系统运维技术交流QQ群:①185473046 系统运维技术交流□Ⅰ ②190706903 系统运维技术交流™Ⅱ ③203744115 系统运维技术交流™Ⅲ

给我留言

您必须 [ 登录 ] 才能发表留言!



Copyright© 2011-2024 系统运维 All rights reserved
版权声明:本站所有文章均为作者原创内容,如需转载,请注明出处及原文链接
陕ICP备11001040号-3