docker发布镜像到阿里云

史上最通俗易懂的docker 发布知识介绍,轻轻松松从入门到精通

Posted by yishuifengxiao on 2019-03-04

dockr 常用镜像的使用与发布镜像到阿里云的全过程解析


docker 镜像使用步骤

  1. 搜索镜像
  2. 拉取镜像
  3. 查看镜像
  4. 启动镜像
  5. 停止容器
  6. 移除容器

一 docker 安装 mysql

1.1 查找镜像

  1. docker hub上查找 mysql 镜像

docker search mysql

yishui@yishui:~/桌面/docker_images$  docker search mysql
NAME                                                   DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                                                  MySQL is a widely used, open-source relation…   7878                [OK]                
mariadb                                                MariaDB is a community-developed fork of MyS…   2613                [OK]                
mysql/mysql-server                                     Optimized MySQL Server Docker images. Create…   592                                     [OK]
percona                                                Percona Server is a fork of the MySQL relati…   418                 [OK]                
zabbix/zabbix-server-mysql                             Zabbix Server with MySQL database support       169                                     [OK]
hypriot/rpi-mysql                                      RPi-compatible Docker Image with Mysql          110                                     
zabbix/zabbix-web-nginx-mysql                          Zabbix frontend based on Nginx web-server wi…   91                                      [OK]
centurylink/mysql                                      Image containing mysql. Optimized to be link…   60                                      [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5   ubuntu-16-nginx-php-phpmyadmin-mysql-5          49                                      [OK]
centos/mysql-57-centos7                                MySQL 5.7 SQL database server                   47                                      
mysql/mysql-cluster                                    Experimental MySQL Cluster Docker images. Cr…   41                                      
tutum/mysql                                            Base docker image to run a MySQL database se…   31                                      
bitnami/mysql                                          Bitnami MySQL Docker Image                      25                                      [OK]
schickling/mysql-backup-s3                             Backup MySQL to S3 (supports periodic backup…   25                                      [OK]
linuxserver/mysql                                      A Mysql container, brought to you by LinuxSe…   20                                      
zabbix/zabbix-proxy-mysql                              Zabbix proxy with MySQL database support        20                                      [OK]
centos/mysql-56-centos7                                MySQL 5.6 SQL database server                   13                                      
circleci/mysql                                         MySQL is a widely used, open-source relation…   10                                      
mysql/mysql-router                                     MySQL Router provides transparent routing be…   8                                       
openshift/mysql-55-centos7                             DEPRECATED: A Centos7 based MySQL v5.5 image…   6                                       
dsteinkopf/backup-all-mysql                            backup all DBs in a mysql server                6                                       [OK]
jelastic/mysql                                         An image of the MySQL database server mainta…   1                                       
cloudposse/mysql                                       Improved `mysql` service with support for `m…   0                                       [OK]
widdpim/mysql-client                                   Dockerized MySQL Client (5.7) including Curl…   0                                       [OK]
ansibleplaybookbundle/mysql-apb                        An APB which deploys RHSCL MySQL                0                                       [OK]
yishui@yishui:~/桌面/docker_images$ 

1.2 拉取镜像到本地

  1. doker hub上 上拉取镜像到本地(这里指定版本为 5.6)

docker pull mysql:5.6

yishui@yishui:~/桌面/docker_images$ docker pull mysql:5.6
5.6: Pulling from library/mysql
6ae821421a7d: Pull complete 
a9e976e3aa6d: Pull complete 
e3735e44a020: Pull complete 
bfd564e9483f: Pull complete 
df705f26e488: Pull complete 
25097d3ea7b8: Pull complete 
606c1b74fda2: Pull complete 
eda5767f8d3f: Pull complete 
5b57c9a598ba: Pull complete 
57f28f294e2f: Pull complete 
94b29ab4a8ab: Pull complete 
Digest: sha256:3ded8f4be8a6581c02abc3d0fa29e9806716acb2fd4190f65336e9d6e61f5b9c
Status: Downloaded newer image for mysql:5.6

1.3 查看是够有拉取到镜像

docker images

yishui@yishui:~/桌面/docker_images$ docker images mysql
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               5.6                 96e41ac53eac        3 weeks ago         256MB

1.4 运行镜像

docker run -p 12345:3306 --name mysql -v /demo/mysql/conf:/etc/mysql/conf.d -v /demo/mysql/logs:/logs -v /demo/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.6

命令说明:

-p 12345:3306:将主机的12345端口映射到docker容器的3306端口。

--name mysql:运行服务名字

-v /demo/mysql/conf:/etc/mysql/conf.d :将主机/demo/mysql录下的conf/my.cnf 挂载到容器的 /etc/mysql/conf.d

-v /demo/mysql/logs:/logs:将主机/demo/mysql目录下的 logs 目录挂载到容器的 /logs。

-v /demo/mysql/data:/var/lib/mysql :将主机/demo/mysql目录下的data目录挂载到容器的 /var/lib/mysql

-e MYSQL_ROOT_PASSWORD=123456:初始化 root 用户的密码。

-d mysql:5.6 : 后台程序运行mysql5.6

二 docker 安装 redis

2.1 拉取 redis 的 docker 镜像

yishui@yishui:~$ docker pull redis:3.2
3.2: Pulling from library/redis
f17d81b4b692: Pull complete 
b32474098757: Pull complete 
8980cabe8bc2: Pull complete 
58af19693e78: Pull complete 
a977782cf22d: Pull complete 
9c1e268980b7: Pull complete 
Digest: sha256:7b0a40301bc1567205e6461c5bf94c38e1e1ad0169709e49132cafc47f6b51f3
Status: Downloaded newer image for redis:3.2
yishui@yishui:~$ docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
yishui_tomcat        latest              b359862a31e7        22 hours ago        726MB
yishui/tomcat-demo   1.0                 1323b27a2652        2 days ago          463MB
<none>               <none>              7cfe70fc34b0        2 days ago          463MB
tomcat               latest              168588387c68        3 weeks ago         463MB
mysql                5.6                 96e41ac53eac        3 weeks ago         256MB
centos               latest              1e1148e4cc2c        2 months ago        202MB
redis                3.2                 87856cc39862        4 months ago        76MB

2.2 使用镜像

docker run -p 6379:6379 -v /demo/data:/data -v /demo/conf/redis.conf:/usr/local/etc/redis/redis.conf  -d redis:3.2 redis-server /usr/local/etc/redis/redis.conf --appendonly yes

2.3 在/demo/conf/redis.conf 目录下新建 redis.conf 文件

2.4 测试连接 redis

docker exec -it 运行着Rediis服务的容器ID redis-cli

三 docker 发布镜像到阿里云

2.1 生成自定义镜像

2.2 将自定义镜像生成的正在运行的容器推送到默认仓库(可选)

docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

OPTIONS 说明:

-a :提交的镜像作者;

-c :使用 Dockerfile 指令来创建镜像;

-m :提交时的说明文字;

-p :在 commit 时,将容器暂停。

2.3 登录[阿里云镜像容器服务]

  1. 登录阿里云镜像容器服务

2.4 创建命名空间

2.5 创建仓库空间

2.6 将镜像推送到阿里云

$ sudo docker login --username=阿里云账号全名registry.cn-hangzhou.aliyuncs.com
$ sudo docker tag [ImageId] registry.cn-hangzhou.aliyuncs.com/yishuifengxiao/yishuifengxiao:[镜像版本号]
$ sudo docker push registry.cn-hangzhou.aliyuncs.com/yishuifengxiao/yishuifengxiao:[镜像版本号]

请根据实际镜像信息替换示例中的[ImageId]和[镜像版本号]参数。

2.7 从 Registry 中拉取镜像

$ sudo docker pull registry.cn-hangzhou.aliyuncs.com/yishuifengxiao/yishuifengxiao:[镜像版本号]