【Docker】Docker常规软件的安装

news/2024/10/30 23:17:41/

总体步骤

搜索镜像

拉取镜像

查看镜像

启动镜像

停止容器

移除容器

示例(安装mysql)

搜索镜像

docker search mysql
[root@192 ~]# docker search mysql
NAME                            DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                           MySQL is a widely used, open-source relation…   14016     [OK]       
mariadb                         MariaDB Server is a high performing open sou…   5346      [OK]       
percona                         Percona Server is a fork of the MySQL relati…   603       [OK]       
phpmyadmin                      phpMyAdmin - A web interface for MySQL and M…   777       [OK]       
circleci/mysql                  MySQL is a widely used, open-source relation…   29                   
bitnami/mysql                   Bitnami MySQL Docker Image                      81                   [OK]
bitnami/mysqld-exporter                                                         4                    
ubuntu/mysql                    MySQL open source fast, stable, multi-thread…   44                   
cimg/mysql                                                                      0                    
rapidfort/mysql                 RapidFort optimized, hardened image for MySQL   14                   
google/mysql                    MySQL server for Google Compute Engine          23                   [OK]
rapidfort/mysql8-ib             RapidFort optimized, hardened image for MySQ…   0                    
hashicorp/mysql-portworx-demo                                                   0                    
rapidfort/mysql-official        RapidFort optimized, hardened image for MySQ…   0                    
newrelic/mysql-plugin           New Relic Plugin for monitoring MySQL databa…   1                    [OK]
databack/mysql-backup           Back up mysql databases to... anywhere!         82                   
linuxserver/mysql               A Mysql container, brought to you by LinuxSe…   38                   
mirantis/mysql                                                                  0                    
docksal/mysql                   MySQL service images for Docksal - https://d…   0                    
bitnamicharts/mysql                                                             0                    
vitess/mysqlctld                vitess/mysqlctld                                1                    [OK]
linuxserver/mysql-workbench                                                     48                   
eclipse/mysql                   Mysql 5.7, curl, rsync                          0                    [OK]
drud/mysql                                                                      0                    
ilios/mysql                     Mysql configured for running Ilios              1                    [OK]

拉取镜像

docker pull mysql:5.7
[root@192 ~]# docker pull mysql:5.7
5.7: 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 
0ceb82207cd7: Pull complete 
37f2405cae96: Pull complete 
e2482e017e53: Pull complete 
70deed891d42: Pull complete 
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

查看镜像

[root@192 ~]# docker images
REPOSITORY                     TAG       IMAGE ID       CREATED         SIZE
192.168.12.130:5000/kdubuntu   1.2       9582a943a6b6   17 hours ago    116MB
kd/myubuntu                    1.2       9582a943a6b6   17 hours ago    116MB
mysql                          5.7       c20987f18b13   15 months ago   448MB
registry                       latest    b8604a3fe854   17 months ago   26.2MB
ubuntu                         latest    ba6acccedd29   18 months ago   72.8MB
redis                          6.0.8     16ecd2772934   2 years ago     104MB

可以到现在的镜像有mysql5.7了。

启动镜像

到Docker Hub(镜像仓库)官网查找mysql,然后找到how to use this image,如下:

How to use this image

Start a mysql server instance

Starting a MySQL instance is simple:

$ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag

… where some-mysql is the name you want to assign to your container, my-secret-pw is the password to be set for the MySQL root user and tag is the tag specifying the MySQL version you want. See the list above for relevant tags.

[root@192 ~]# docker run -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
0b82f8f6a8360b791e4d7752de0afd700a1f16cd27b840536e6bc3864bc9989c
[root@192 ~]# docker ps
CONTAINER ID   IMAGE       COMMAND                  CREATED         STATUS         PORTS                                                  NAMES
0b82f8f6a836   mysql:5.7   "docker-entrypoint.s…"   4 seconds ago   Up 2 seconds   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   zealous_wiles
#进入容器实例
[root@192 ~]# docker exec -it 0b82f8f6a836 /bin/bash
#进入mysql
root@0b82f8f6a836:/# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.36 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> create database db01;
Query OK, 1 row affected (0.00 sec)mysql> use db01;
Database changedmysql> create table kd(id int,name varchar(20));
Query OK, 0 rows affected (0.04 sec)mysql> insert into kd values(1,'zhangsan');
Query OK, 1 row affected (0.00 sec)mysql> select * from kd;
+------+----------+
| id   | name     |
+------+----------+
|    1 | zhangsan |
+------+----------+
1 row in set (0.00 sec)
#到这里可以看到,mysql正常使用

在主机上使用数据库工具可以正常连接docker中的mysql。

删除容器后,mysql数据怎么办

要想删除容器后,可以继续保存mysql数据,需要使用数据卷,命令如下:

docker run -d -p 3306:3306 --privileged=true -v /tmp/mysql/log:/var/log/mysql -v /tmp/mysql/data:/var/lib/mysql -v /tmp/mysql/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 --name mysql mysql:5.7

1、删除当前容器

[root@192 ~]# docker ps
CONTAINER ID   IMAGE       COMMAND                  CREATED          STATUS         PORTS                                                  NAMES
b74f0b9cfe34   mysql:5.7   "docker-entrypoint.s…"   11 minutes ago   Up 6 minutes   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   mysql
[root@192 ~]# docker rm -f b74f0b9cfe34
b74f0b9cfe34
[root@192 ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

2、重新启动容器

[root@192 ~]# docker run -d -p 3306:3306 --privileged=true -v /tmp/mysql/log:/var/log/mysql  -v /tmp/mysql/data:/var/lib/mysql  -v /tmp/mysql/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 --name mysql mysql:5.7
2be1c7d94c4606ea5e4be6cfa5df35aed653cd9cf9d6b9365ffc4c1ee9251bd6
[root@192 ~]# docker ps
CONTAINER ID   IMAGE       COMMAND                  CREATED         STATUS         PORTS                                                  NAMES
2be1c7d94c46   mysql:5.7   "docker-entrypoint.s…"   4 seconds ago   Up 3 seconds   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   mysql
[root@192 ~]# docker exec -it mysql /bin/bash
root@2be1c7d94c46:/# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.36 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> use db01;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changed
mysql> select * from kd;
+------+--------+
| id   | name   |
+------+--------+
|    1 | dddd   |
|    2 | lisi   |
|    3 | 王五   |
+------+--------+
3 rows in set (0.01 sec)

可以看到重新启动后,之前的数据还在。

解决mysql乱码

1、在宿主机数据卷 /tmp/mysql/conf创建my.cnf文件,并添加如下内容

[root@192 conf]# cat my.cnf 
[client]
default_character_set=utf8
[mysqld]
collation_server = utf8_general_ci
character_set_server = utf8

2、重启容器并重新进入查看

[root@192 conf]# docker restart mysql
mysql
#进入
[root@192 conf]# docker exec -it b74f0b9cfe34 /bin/bash
#启动mysql
root@b74f0b9cfe34:/# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.36 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> SHOW VARIABLES LIKE 'character%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.02 sec)mysql> create database db01;
Query OK, 1 row affected (0.02 sec)mysql> use db01;
Database changed
mysql> create table kd(id int,name varchar(20));
Query OK, 0 rows affected (0.01 sec)mysql> insert into kd values(1,'dddd');
Query OK, 1 row affected (0.01 sec)mysql> select * from kd;
+------+--------+
| id   | name   |
+------+--------+
|    1 | dddd   |
|    2 | lisi   |
|    3 | 王五   |
+------+--------+
3 rows in set (0.00 sec)

可以看到,乱码问题已经解决。


http://www.ppmy.cn/news/42092.html

相关文章

基于改进多目标灰狼优化算法的考虑V2G技术的风、光、荷、储微网多目标日前优化调度研究(Matlab代码实现)

💥💥💞💞欢迎来到本博客❤️❤️💥💥 🏆博主优势:🌞🌞🌞博客内容尽量做到思维缜密,逻辑清晰,为了方便读者。 ⛳️座右铭&a…

基于深度学习的花卉识别

1、数据集 春天来了,我在公园的小道漫步,看着公园遍野的花朵,看起来真让人心旷神怡,一周工作带来的疲惫感顿时一扫而光。难得一个糙汉子有闲情逸致俯身欣赏这些花朵儿,然而令人尴尬的是,我一朵都也不认识。…

用ChatGPT怎么赚钱?普通人用这5个方法也能赚到生活费

ChatGPT在互联网火得一塌糊涂,因为它可以帮很多人解决问题。比如:帮编辑人员写文章,还可以替代程序员写代码,帮策划人员写文案策划等等。ChatGPT这么厉害,能否用它来赚钱呢?今天和大家分享用ChatGPT赚钱的5…

银行数字化转型导师坚鹏:ChatGPT解密与银行应用案例

ChatGPT解密与银行应用案例 ——开启人类AI新纪元 打造数字化转型新利器 课程背景: 很多企业和员工存在以下问题:不清楚ChatGPT对我们有什么影响?不知道ChatGPT的发展现状及作用?不知道ChatGPT的银行业应用案例?…

Matlab基础

Matlab基础目录Matlab变量特殊常量变量的命名规则变量定义与赋值变量的显示变量的存取变量的清楚变量的检查数组和矩阵一维数组的创建和元素提取一维数组的创建一维数组的提取二维数组的创建与元素提取二维数组的创建二维矩阵元素提取字符数组和空数组矩阵的基本算术运算数据可…

应用程序接口(API)安全的入门指南

本文简单回顾了 API 的发展历史,其基本概念、功能、相关协议、以及使用场景,重点讨论了与之相关的不同安全要素、威胁、认证方法、以及十二项优秀实践。 根据有记录的历史,随着 Salesforce 的销售自动化解决方案的推出,首个 We…

Linux下异步socket客户端

文章目录socket 客户端1. 创建socketsocket()函数返回值2. 设置socket的属性connect函数sockaddr_in结构体inet_pton函数3. fcntl设置非阻塞4. recv函数socket 客户端 1. 创建socket socket()函数 #include <sys/socket.h> int socket(int domain, int type, int proto…

Go 语言切片是如何扩容的?

原文链接&#xff1a; Go 语言切片是如何扩容的&#xff1f; 在 Go 语言中&#xff0c;有一个很常用的数据结构&#xff0c;那就是切片&#xff08;Slice&#xff09;。 切片是一个拥有相同类型元素的可变长度的序列&#xff0c;它是基于数组类型做的一层封装。它非常灵活&am…