Docker基础案例1


案例1 Docker安装MySQL

MySQL 是开源的关系数据库实现。

该仓库位于 https://hub.docker.com/_/mysql/ ,提供了 MySQL 5.5 ~ 8.x 各个版本的镜像

1、docker search mysql 命令来查看可用版本:

[root@Base ~]# docker search mysql
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   11876     [OK]       
mariadb                           MariaDB Server is a high performing open sou…   4540      [OK]       
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   888                  [OK]
percona                           Percona Server is a fork of the MySQL relati…   566       [OK]       
phpmyadmin                        phpMyAdmin - A web interface for MySQL and M…   407       [OK]       
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   92                   
mysql/mysql-cluster               Experimental MySQL Cluster Docker images. Cr…   90                   
centurylink/mysql                 Image containing mysql. Optimized to be link…   59                   [OK]
databack/mysql-backup             Back up mysql databases to... anywhere!         54                   
prom/mysqld-exporter                                                              46                   [OK]
deitch/mysql-backup               REPLACED! Please use http://hub.docker.com/r…   41                   [OK]
tutum/mysql                       Base docker image to run a MySQL database se…   35                   
linuxserver/mysql                 A Mysql container, brought to you by LinuxSe…   34                   
schickling/mysql-backup-s3        Backup MySQL to S3 (supports periodic backup…   31                   [OK]
mysql/mysql-router                MySQL Router provides transparent routing be…   23                   
centos/mysql-56-centos7           MySQL 5.6 SQL database server                   21                   
arey/mysql-client                 Run a MySQL client from a docker container      20                   [OK]
fradelg/mysql-cron-backup         MySQL/MariaDB database backup using cron tas…   18                   [OK]
openshift/mysql-55-centos7        DEPRECATED: A Centos7 based MySQL v5.5 image…   6                    
ansibleplaybookbundle/mysql-apb   An APB which deploys RHSCL MySQL                3                    [OK]
devilbox/mysql                    Retagged MySQL, MariaDB and PerconaDB offici…   3                    
idoall/mysql                      MySQL is a widely used, open-source relation…   3                    [OK]
centos/mysql-80-centos7           MySQL 8.0 SQL database server                   2                    
jelastic/mysql                    An image of the MySQL database server mainta…   2                    
widdpim/mysql-client              Dockerized MySQL Client (5.7) including Curl…   1                    [OK]

2、拉取 MySQL 镜像

这里我们拉取官方的最新版本的镜像:

[root@Base ~]# docker pull mysql:latest
latest: Pulling from library/mysql
72a69066d2fe: Pull complete 
93619dbc5b36: Pull complete 
99da31dd6142: Pull complete 
626033c43d70: Pull complete 
37d5d7efb64e: Pull complete 
ac563158d721: Pull complete 
d2ba16033dad: Pull complete 
688ba7d5c01a: Pull complete 
00e060b6d11d: Pull complete 
1c04857f594f: Pull complete 
4d7cfa90e6ea: Pull complete 
e0431212d27d: Pull complete 
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest

3、查看本地镜像

使用以下命令来查看是否已安装了 mysql:

[root@Base ~]#  docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
mysql         latest    3218b38490ce   6 days ago     516MB
ubuntu        latest    ba6acccedd29   2 months ago   72.8MB
hello-world   latest    feb5d9fea6a5   3 months ago   13.3kB

4、运行容器

安装完成后,我们可以使用以下命令来运行 mysql 容器:

mkdir -p /data/mysql/data
docker run -itd --name mysql-test \
           -p 3306:3306 \
           -e MYSQL_ROOT_PASSWORD=123456 \
           -v /data/mysql/data:/var/lib/mysql \
           --restart=always \
           mysql

参数说明:

  • -p 3306:3306 :映射容器服务的 3306 端口到宿主机的 3306 端口,外部主机可以直接通过 宿主机ip:3306 访问到 MySQL 的服务。
  • -e MYSQL_ROOT_PASSWORD=123456:设置 MySQL 服务 root 用户的密码。
  • -v /data/mysql/data:/var/lib/mysql:设置本地数据目录和容器中的数据目录绑定

5、安装成功

通过 docker ps 命令查看是否安装成功:

[root@Base ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                                                  NAMES
ebff285ff733   mysql     "docker-entrypoint.s…"   4 minutes ago   Up 4 minutes   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   mysql-test

通过exec使用MySQL容器:

[root@Base ~]# docker exec -it  mysql-test bash
root@5fe9d68ae468:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.27 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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> \s
--------------
mysql  Ver 8.0.27 for Linux on x86_64 (MySQL Community Server - GPL)

Connection id:          8
Current database:
Current user:           root@localhost
SSL:                    Not in use
Current pager:          stdout
Using outfile:          ''
Using delimiter:        ;
Server version:         8.0.27 MySQL Community Server - GPL
Protocol version:       10
Connection:             Localhost via UNIX socket
Server characterset:    utf8mb4
Db     characterset:    utf8mb4
Client characterset:    latin1
Conn.  characterset:    latin1
UNIX socket:            /var/run/mysqld/mysqld.sock
Binary data as:         Hexadecimal
Uptime:                 34 sec

Threads: 2  Questions: 5  Slow queries: 0  Opens: 117  Flush tables: 3  Open tables: 36  Queries per second avg: 0.147
--------------

案例2 Docker安装WordPress

mysql 中创建wp1库

mysql> create database wp1 charset utf8mb4;
docker run -itd --name wodpress1 \
           -e WORDPRESS_DB_HOST=192.168.8.101:3306 \
           -e WORDPRESS_DB_USER=root \
           -e WORDPRESS_DB_PASSWORD=123456 \
           -e WORDPRESS_DB_NAME=wp1 \
           -p 8086:80 \
           --restart=always \
           wordpress

案例三 Docker 安装nginx

# 创建本地目录用于映射nginx容器
mkdir -p /data/nginx/conf
cat > /data/nginx/conf/default.conf << EOF
server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;
    location / {
        proxy_pass http://192.168.8.101:8086;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}
EOF

# 搜索镜像
docker search nginx
# 下载镜像
docker pull nginx
# 查看镜像
docker images
# 启动镜像
docker run -itd --name nginx01 \
           -p 80:80 \
           -v /data/nginx/conf:/etc/nginx/conf.d \
           --restart=always \
           nginx
# 查看容器
docker ps
# 本机自测
curl localhost

浏览器访问http://192.168.8.101