阿里云服务器(Ubuntu 16.04 64位)安装Docker(MySql、Redis)

Docker 安装步骤

# step 1: 安装必要的一些系统工具
sudo apt-get update
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
# step 2: 安装GPG证书
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
# Step 3: 写入软件源信息
sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
# Step 4: 更新并安装 Docker-CE
sudo apt-get -y update
sudo apt-get -y install docker-ce
 
# 安装指定版本的Docker-CE:
# Step 1: 查找Docker-CE的版本:
# apt-cache madison docker-ce
#   docker-ce | 18.06.2~ce~3-0~ubuntu | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
 	docker-ce | 18.06.1~ce~3-0~ubuntu | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
 	docker-ce | 18.06.0~ce~3-0~ubuntu | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
 	docker-ce | 18.03.1~ce-0~ubuntu | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
 	docker-ce | 18.03.0~ce-0~ubuntu | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
 	docker-ce | 17.12.1~ce-0~ubuntu | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
 	docker-ce | 17.12.0~ce-0~ubuntu | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
 	docker-ce | 17.09.1~ce-0~ubuntu | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
 	docker-ce | 17.09.0~ce-0~ubuntu | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
 	docker-ce | 17.06.2~ce-0~ubuntu | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
 	docker-ce | 17.06.1~ce-0~ubuntu | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
	docker-ce | 17.06.0~ce-0~ubuntu | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
 	docker-ce | 17.03.3~ce-0~ubuntu-xenial | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
 	docker-ce | 17.03.2~ce-0~ubuntu-xenial | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
 	docker-ce | 17.03.1~ce-0~ubuntu-xenial | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
 	docker-ce | 17.03.0~ce-0~ubuntu-xenial | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages

# Step 2: 安装指定版本的Docker-CE: (VERSION 例如上面的 18.06.2~ce~3-0~ubuntu)
# sudo apt-get -y install docker-ce=[VERSION]

Docker安装完成校验

root@iZwz9c5s8jxhick0fpiz4nZ:~# sudo docker version
Client:
 Version:	18.03.0-ce
 API version:	1.37
 Go version:	go1.9.4
 Git commit:	0520e24
 Built:	Wed Mar 21 23:10:01 2018
 OS/Arch:	linux/amd64
 Experimental:	false
 Orchestrator:	swarm

Server:
 Engine:
  Version:	18.03.0-ce
  API version:	1.37 (minimum version 1.12)
  Go version:	go1.9.4
  Git commit:	0520e24
  Built:	Wed Mar 21 23:08:31 2018
  OS/Arch:	linux/amd64
  Experimental:	false

Docker安装Mysql

1、查看可用的 MySQL 版本
# 用 docker search mysql 命令来查看可用版本
root@iZwz9c5s8jxhick0fpiz4nZ:~# docker search mysql
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   9345                [OK]                
mariadb                           MariaDB is a community-developed fork of MyS…   3352                [OK]                
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   684                                     [OK]
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   74                                      
mysql/mysql-cluster               Experimental MySQL Cluster Docker images. Cr…   66                                      
centurylink/mysql                 Image containing mysql. Optimized to be link…   61                                      [OK]
deitch/mysql-backup               REPLACED! Please use http://hub.docker.com/r…   41                                      [OK]
bitnami/mysql                     Bitnami MySQL Docker Image                      37                                      [OK]
tutum/mysql                       Base docker image to run a MySQL database se…   34                                      
schickling/mysql-backup-s3        Backup MySQL to S3 (supports periodic backup…   29                                      [OK]
prom/mysqld-exporter                                                              27                                      [OK]
linuxserver/mysql                 A Mysql container, brought to you by LinuxSe…   25                                      
centos/mysql-56-centos7           MySQL 5.6 SQL database server                   19                                      
circleci/mysql                    MySQL is a widely used, open-source relation…   19                                      
mysql/mysql-router                MySQL Router provides transparent routing be…   15                                      
databack/mysql-backup             Back up mysql databases to... anywhere!         14                                      
arey/mysql-client                 Run a MySQL client from a docker container      13                                      [OK]
openshift/mysql-55-centos7        DEPRECATED: A Centos7 based MySQL v5.5 image…   6                                       
genschsa/mysql-employees          MySQL Employee Sample Database                  5                                       [OK]
fradelg/mysql-cron-backup         MySQL/MariaDB database backup using cron tas…   5                                       [OK]
devilbox/mysql                    Retagged MySQL, MariaDB and PerconaDB offici…   2                                       
ansibleplaybookbundle/mysql-apb   An APB which deploys RHSCL MySQL                2                                       [OK]
jelastic/mysql                    An image of the MySQL database server mainta…   1                                       
widdpim/mysql-client              Dockerized MySQL Client (5.7) including Curl…   0                                       [OK]
monasca/mysql-init                A minimal decoupled init container for mysql    0        

2、拉取 MySQL 镜像
# 拉取官方的最新版本的镜像:
root@iZwz9c5s8jxhick0fpiz4nZ:~# docker pull mysql:latest
latest: Pulling from library/mysql
c499e6d256d6: Pull complete 
22c4cdf4ea75: Pull complete 
6ff5091a5a30: Pull complete 
2fd3d1af9403: Pull complete 
0d9d26127d1d: Pull complete 
54a67d4e7579: Pull complete 
fe989230d866: Pull complete 
3a808704d40c: Pull complete 
826517d07519: Pull complete 
69cd125db928: Pull complete 
b5c43b8c2879: Pull complete 
1811572b5ea5: Pull complete 
Digest: sha256:b69d0b62d02ee1eba8c7aeb32eba1bb678b6cfa4ccfb211a5d7931c7755dc4a8
Status: Downloaded newer image for mysql:latest

3、查看本地镜像
# 查看是否已安装了 mysql:
root@iZwz9c5s8jxhick0fpiz4nZ:~# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               latest              9228ee8bac7a        6 days ago          547MB
root@iZwz9c5s8jxhick0fpiz4nZ:~# docker run -itd --name mysql-test -p 3306:3306 -e MYSQL_ROOT_PASSWORD=1234 mysql
76cc7931b10e7fc0d5f49da963ecb4781d2eace68172d6109689b78b2c606292

4、运行容器
# 安装完成后,我们可以使用以下命令来运行 mysql 容器:
# eg:
#    -p 3306:3306 :映射容器服务的 3306 端口到宿主机的 3306 端口,外部主机可以直接通过 宿主机ip:3306 访问到 MySQL 的服务。
#    MYSQL_ROOT_PASSWORD=123456:设置 MySQL 服务 root 用户的密码。
root@iZwz9c5s8jxhick0fpiz4nZ:~# docker run -itd --name mysql-test -p 3306:3306 -e MYSQL_ROOT_PASSWORD=1234 mysql
76cc7931b10e7fc0d5f49da963ecb4781d2eace68172d6109689b78b2c606292

5、安装成功
# 查看是否安装成功:
root@iZwz9c5s8jxhick0fpiz4nZ:~# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
76cc7931b10e        mysql               "docker-entrypoint.s…"   26 minutes ago      Up 26 minutes       0.0.0.0:3306->3306/tcp, 33060/tcp   mysql-test

6、安装mysql-client
# 查看客户端是否安装
root@iZwz9c5s8jxhick0fpiz4nZ:~# mysql
The program 'mysql' can be found in the following packages:
 * mysql-client-core-5.7
 * mariadb-client-core-10.0
Try: apt install <selected package>
# 安装客户端
root@iZwz9c5s8jxhick0fpiz4nZ:~# apt install mysql-client-core-5.7
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
  mysql-client-core-5.7
0 upgraded, 1 newly installed, 0 to remove and 18 not upgraded.
Need to get 6,074 kB of archives.
After this operation, 30.4 MB of additional disk space will be used.
Get:1 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-updates/main amd64 mysql-client-core-5.7 amd64 5.7.29-0ubuntu0.16.04.1 [6,074 kB]
Fetched 6,074 kB in 0s (42.2 MB/s)              
Selecting previously unselected package mysql-client-core-5.7.
(Reading database ... 102079 files and directories currently installed.)
Preparing to unpack .../mysql-client-core-5.7_5.7.29-0ubuntu0.16.04.1_amd64.deb ...
Unpacking mysql-client-core-5.7 (5.7.29-0ubuntu0.16.04.1) ...
Processing triggers for man-db (2.7.5-1) ...
Setting up mysql-client-core-5.7 (5.7.29-0ubuntu0.16.04.1) ...

7、访问mysql服务
# 通过 root 和密码 123456 访问 MySQL 服务
root@iZwz9c5s8jxhick0fpiz4nZ:~# mysql -h 127.0.0.1 -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.19 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

Docker 安装 Redis

1、查看可用的 Redis 版本
root@iZwz9c5s8jxhick0fpiz4nZ:~# docker search  redis
NAME                             DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
redis                            Redis is an open source key-value store that…   8000                [OK]                
bitnami/redis                    Bitnami Redis Docker Image                      140                                     [OK]
sameersbn/redis                                                                  79                                      [OK]
grokzen/redis-cluster            Redis cluster 3.0, 3.2, 4.0 & 5.0               65                                      
rediscommander/redis-commander   Alpine image for redis-commander - Redis man…   36                                      [OK]
kubeguide/redis-master           redis-master with "Hello World!"                31                                      
redislabs/redis                  Clustered in-memory database engine compatib…   24                                      
arm32v7/redis                    Redis is an open source key-value store that…   21                                      
redislabs/redisearch             Redis With the RedisSearch module pre-loaded…   20                                      
oliver006/redis_exporter          Prometheus Exporter for Redis Metrics. Supp…   20                                      
bitnami/redis-sentinel           Bitnami Docker Image for Redis Sentinel         12                                      [OK]
webhippie/redis                  Docker images for Redis                         11                                      [OK]
redislabs/redisgraph             A graph database module for Redis               10                                      [OK]
s7anley/redis-sentinel-docker    Redis Sentinel                                  9                                       [OK]
insready/redis-stat              Docker image for the real-time Redis monitor…   9                                       [OK]
arm64v8/redis                    Redis is an open source key-value store that…   8                                       
redislabs/redismod               An automated build of redismod - latest Redi…   7                                       [OK]
centos/redis-32-centos7          Redis in-memory data structure store, used a…   5                                       
redislabs/redisinsight           RedisInsight - The GUI for Redis                4                                       
circleci/redis                   CircleCI images for Redis                       4                                       [OK]
clearlinux/redis                 Redis key-value data structure server with t…   2                                       
tiredofit/redis                  Redis Server w/ Zabbix monitoring and S6 Ove…   1                                       [OK]
runnable/redis-stunnel           stunnel to redis provided by linking contain…   1                                       [OK]
wodby/redis                      Redis container image with orchestration        1                                       [OK]
xetamus/redis-resource           forked redis-resource                           0                                       [OK]

2、取最新版的 Redis 镜像
root@iZwz9c5s8jxhick0fpiz4nZ:~# docker pull redis:latest
latest: Pulling from library/redis
c499e6d256d6: Already exists 
bf1bc8a5a7e4: Pull complete 
7564fb795604: Pull complete 
ec6e86f783e4: Pull complete 
1371d6223f46: Pull complete 
021fd554320f: Pull complete 
Digest: sha256:a732b1359e338a539c25346a50bf0a501120c41dc248d868e546b33e32bf4fe4
Status: Downloaded newer image for redis:latest

3、查看本地镜像
root@iZwz9c5s8jxhick0fpiz4nZ:~# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
redis               latest              4cdbec704e47        6 days ago          98.2MB
mysql               latest              9228ee8bac7a        6 days ago          547MB

4、运行容器
# eg:
#    -p 6379:6379:映射容器服务的 6379 端口到宿主机的 6379 端口。外部可以直接通过宿主机ip:6379 访问到 Redis 的服务。
root@iZwz9c5s8jxhick0fpiz4nZ:~# docker run -itd --name redis-test -p 6379:6379 redis
ec7a5392852e5c616e5b7fa437d05bd84aed8e6497ee1a429f28585ca5ea144e

5、安装成功
# 命令查看容器的运行信息:
root@iZwz9c5s8jxhick0fpiz4nZ:~# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                               NAMES
ec7a5392852e        redis               "docker-entrypoint.s…"   About a minute ago   Up About a minute   0.0.0.0:6379->6379/tcp              redis-test
76cc7931b10e        mysql               "docker-entrypoint.s…"   About an hour ago    Up About an hour    0.0.0.0:3306->3306/tcp, 33060/tcp   mysql-test

# 通过 redis-cli 连接测试使用 redis 服务:
root@iZwz9c5s8jxhick0fpiz4nZ:~# docker exec -it redis-test /bin/bash
root@ec7a5392852e:/data# redis-cli
127.0.0.1:6379> 

你可能感兴趣的:(Docker)