一,docker镜像制作并上传到Harbor私服镜像库

前言

本次咱们以制作一个mysql5.7版本的数据库镜像为例。对docker相关知识需要有一定的掌握,docker学习的相关资料。

链接:https://pan.baidu.com/s/1WOzrTECTnbhjUxdXXBJ4hg 
提取码:5rvq

下面我们进入正题。

Step 1:前置条件

  • 已安装docker环境
  • 已安装Harbor镜像服务器

Step 2:拉取mysql官方镜像库

[root@harbor ~]# docker search mysql
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   9463                [OK]                
mariadb                           MariaDB is a community-developed fork of MyS…   3421                [OK]                
[root@harbor ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
54fec2fa59d0: Pull complete 
bcc6c6145912: Pull complete 
951c3d959c9d: Pull complete 
05de4d0e206e: Pull complete 
319f0394ef42: Pull complete 
d9185034607b: Pull complete 
013a9c64dadc: Pull complete 
e745b3361626: Pull complete 
03145d87b451: Pull complete 
3991a6b182ee: Pull complete 
62335de06f7d: Pull complete 
Digest: sha256:e821ca8cc7a44d354486f30c6a193ec6b70a4eed8c8362aeede4e9b8d74b8ebb
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7  
[root@harbor ~]# docker images
REPOSITORY                      TAG                              IMAGE ID            CREATED             SIZE
mysql                           5.7                              f965319e89de        9 days ago          448MB                                       

Step 3:制造自定义镜像

一,准备4个文件:

my.cnf   mysql数据库配置文件

# Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

[mysqld]
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
lower_case_table_names=1
character-set-server=utf8

Dockerfile  docker镜像构建文件

FROM mysql:5.7

COPY ./my.cnf /etc/mysql/
COPY ./*.sql /mysql/
COPY setup.sh /opt/setup.sh

CMD ["sh", "/opt/setup.sh"]
EXPOSE 3306

privileges.sql mysql远程登陆及密码配置文件

use mysql;
grant all privileges on *.* to root@"%" identified by "root" with grant option;
flush privileges;

setup.sh  docker镜像启动的第一个执行脚本

#!/bin/bash
set -e

echo '1.启动mysql....'
service mysql start
sleep 3
echo `service mysql status`
echo '1.启动mysql完毕....'

echo '4.开始修改密码....'
mysql < /mysql/privileges.sql
echo '5.修改密码完毕....'
tail -f /dev/null

二,构建镜像:

准备的文件及路径

[root@harbor mysql]# pwd
/root/docker-images/mysql
[root@harbor mysql]# ll
total 16
-rw-r--r-- 1 root root 134 May  6 19:45 Dockerfile
-rw-r--r-- 1 root root 856 May  6 19:41 my.cnf
-rw-r--r-- 1 root root 109 May  6 19:46 privileges.sql
-rw-r--r-- 1 root root 245 May  6 19:49 setup.sh

开始构建

[root@harbor mysql]# docker build -t iam-mysql:5.7 -f Dockerfile .             
Sending build context to Docker daemon  5.632kB
Step 1/6 : FROM mysql:5.7
 ---> f965319e89de
Step 2/6 : COPY ./my.cnf /etc/mysql/
 ---> 55edb36d3589
Step 3/6 : COPY ./*.sql /mysql/
 ---> 0e498b628345
Step 4/6 : COPY setup.sh /opt/setup.sh
 ---> fea73b379e6b
Step 5/6 : CMD ["sh", "/opt/setup.sh"]
 ---> Running in 974b8876c6f9
Removing intermediate container 974b8876c6f9
 ---> fed841d04900
Step 6/6 : EXPOSE 3306
 ---> Running in 63054882e278
Removing intermediate container 63054882e278
 ---> cbb1a3944622
Successfully built cbb1a3944622
Successfully tagged iam-mysql:5.7
[root@harbor mysql]# docker images
REPOSITORY                      TAG                              IMAGE ID            CREATED             SIZE
iam-mysql                       5.7                              cbb1a3944622        13 seconds ago      448MB
mysql                           5.7                              f965319e89de        9 days ago          448MB

测试运行

[root@harbor mysql]# docker run -d -p 3306:3306 iam-mysql:5.7 
fc5d96b8e39e04a65b757b89299b172c09b2b849d8b40d00ff60207bb20204e9
[root@harbor mysql]# docker ps -a
CONTAINER ID        IMAGE                                                     COMMAND                  CREATED             STATUS                  PORTS                               NAMES
fc5d96b8e39e        iam-mysql:5.7                                             "docker-entrypoint.s…"   7 seconds ago       Up 6 second

远程连接

一,docker镜像制作并上传到Harbor私服镜像库_第1张图片

Step 4:上传到镜像服务器

镜像源配置信息,登录私服

[root@harbor mysql]# cat /etc/docker/daemon.json 
{
  "registry-mirrors": ["https://9exvf3nd.mirror.aliyuncs.com"],
  "insecure-registries": ["192.168.0.240:80"]
}
[root@harbor mysql]# docker login 192.168.0.240:80

打tag,并上传

[root@harbor mysql]# docker tag iam-mysql:5.7  192.168.0.240:80/iam/mysql:5.7 
[root@harbor mysql]# docker images
REPOSITORY                      TAG                              IMAGE ID            CREATED             SIZE
iam-mysql                       5.7                              cbb1a3944622        10 minutes ago      448MB
192.168.0.240:80/iam/mysql      5.7                              cbb1a3944622        10 minutes ago      448MB
mysql                           5.7                              f965319e89de        9 days ago          448MB

[root@harbor mysql]# docker push  192.168.0.240:80/iam/mysql:5.7
The push refers to repository [192.168.0.240:80/iam/mysql]
667cb2219d4b: Pushed 
80c38fddcfdc: Pushed 
7645d42a3023: Pushed 
c818eedb4b85: Layer already exists 
446140d87d8a: Layer already exists 
c85bca00fd2a: Layer already exists 
9574dcd2d660: Layer already exists 
ace74cb61ec0: Layer already exists 
d84f8cf1dc23: Layer already exists 
24bd91e7be37: Layer already exists 
49baacc63c3b: Layer already exists 
8d3b3830445d: Layer already exists 
49003fe88142: Layer already exists 
c2adabaecedb: Layer already exists 
5.7: digest: sha256:2b03f9dd4e3c7cbdf5ab37894d6a635cc4417cd1f6b6b81898fa526442d228ce size: 3242

验证:

一,docker镜像制作并上传到Harbor私服镜像库_第2张图片

你可能感兴趣的:(一,docker镜像制作并上传到Harbor私服镜像库)