docker部署MySQL、Redis和Nginx

一、MySQL的部署

  1. 搜索MySQL的镜像

    root@localhost ~]# docker search mysql
    INDEX       NAME                                        DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
    docker.io   docker.io/mysql                             MySQL is a widely used, open-source relati...   9802      [OK]       
    docker.io   docker.io/mariadb                           MariaDB is a community-developed fork of M...   3576      [OK]       
    docker.io   docker.io/mysql/mysql-server                Optimized MySQL Server Docker images. Crea...   718                  [OK]
    docker.io   docker.io/percona                           Percona Server is a fork of the MySQL rela...   499       [OK]       
    docker.io   docker.io/centos/mysql-57-centos7           MySQL 5.7 SQL database server                   79                   
    

    第一个mysql是MySQL的最新版本,当前的MySQL的最新版本是8.0.21。如果你需要创建MySQL的5.7版本的话,请使用centos/mysql-57-centos7

  2. 创建容器

    docker run -di --name=mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root mysql
    

    -p 代表端口映射,格式:[宿主机端口]:[容器端口]。把宿主机的端口映射到容器的端口上。
    -e 代表添加环境变量MYSQL_ROOT_PASSWORD是root用户的登陆密码。
    注:5.7版本中,如果是在容器中使用root登录的话,那么其密码为空。远程登录的密码即为刚刚设置的密码。

    实例如下:

    [root@localhost ~]# docker run -di --name=mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root mysql
    Unable to find image 'mysql:latest' locally
    Trying to pull repository docker.io/library/mysql ... 
    latest: Pulling from docker.io/library/mysql
    6ec8c9369e08: Pull complete 
    177e5de89054: Pull complete 
    ab6ccb86eb40: Pull complete 
    e1ee78841235: Pull complete 
    09cd86ccee56: Pull complete 
    78bea0594a44: Pull complete 
    caf5f529ae89: Pull complete 
    cf0fc09f046d: Pull complete 
    4ccd5b05a8f6: Pull complete 
    76d29d8de5d4: Pull complete 
    8077a91f5d16: Pull complete 
    922753e827ec: Pull complete 
    Digest: sha256:fb6a6a26111ba75f9e8487db639bc5721d4431beba4cd668a4e922b8f8b14acc
    Status: Downloaded newer image for docker.io/mysql:latest
    a3bd85b47386ab160ba20e738b42304d1f8d384f6cb473232dcdf26e69f92a74
    
  3. 操作MySQL容器
    进入容器

    docker exec -it mysql bash
    

    登录MySQL

    mysql -uroot -p
    

    实例

    [root@localhost ~]# docker exec -it mysql bash
    root@a3bd85b47386:/# mysql -uroot -p
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or g.
    Your MySQL connection id is 10
    Server version: 8.0.21 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> 
    

二、Redis部署

  1. 拉取镜像
    docker pull redis
    
    实例:
    [root@localhost ~]# docker pull redis
    Using default tag: latest
    Trying to pull repository docker.io/library/redis ... 
    latest: Pulling from docker.io/library/redis
    6ec8c9369e08: Already exists 
    efe6cceb88f8: Pull complete 
    cdb6bd1ce7c5: Pull complete 
    9d80498f79fe: Pull complete 
    b7cd40c9247b: Pull complete 
    96403647fb55: Pull complete 
    Digest: sha256:d86d6739fab2eaf590cfa51eccf1e9779677bd2502894579bcf3f80cb37b18d4
    Status: Downloaded newer image for docker.io/redis:latest
    
  2. 创建容器
    docker run -di --name=myredis -p 6379:6379 redis
    
    实例:
    [root@localhost ~]# docker run -di --name=myredis -p 6379:6379 redis
    5a8b5c13284bdc17a6ef9ccd40e53040657cc3deaa498a1419a114177869f65f
    
  3. 操作Redis容器
    # 1.进入容器
    docker exec -it myredis /bin/bash
    # 2.进入redis的安装目录
    cd /usr/local/bin
    # 3.连接redis
    ./redis-cli
    
    实例:
    [root@localhost ~]# docker run -di --name=myredis -p 6379:6379 redis
    5a8b5c13284bdc17a6ef9ccd40e53040657cc3deaa498a1419a114177869f65f
    [root@localhost ~]# docker exec -it myredis bash
    root@5a8b5c13284b:/data# cd /usr/local/bin
    root@5a8b5c13284b:/usr/local/bin# ./redis-cli 
    127.0.0.1:6379> ping
    PONG
    
    这里能正常ping通,也可以使用远程的redis连接方式,连接端口是6379

三、Nginx的部署

  1. 拉取镜像
    docker pull nginx
    
  2. 创建容器
    docker run -di --name=mynginx -p 80:80 nginx
    
原文地址:https://www.cnblogs.com/hxsen/p/13456659.html