1. docker基本命令
1.1 查看已拉取镜像
[root@hadoop66 ~]# docker images
[root@hadoop66 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/mysql 5.7 d05c76dbbfcf 37 hours ago 448 MB
xcli0621/neo4j 3.4.5 9c3d920129dd 2 weeks ago 309 MB
xcli0621/postgres 9.5.5 44e76538ce21 2 weeks ago 197 MB
docker.io/xcli0621/docker-bench-security 1.0 b949a7d13ecd 7 weeks ago 82.9 MB
k8s.gcr.io/kube-proxy v1.17.4 6dec7cfde1e5 4 months ago 116 MB
registry.cn-hangzhou.aliyuncs.com/openthings/k8s-gcr-io-kube-proxy v1.17.4 6dec7cfde1e5 4 months ago 116 MB
k8s.gcr.io/kube-apiserver v1.17.4 2e1ba57fe95a 4 months ago 171 MB
registry.cn-hangzhou.aliyuncs.com/openthings/k8s-gcr-io-kube-apiserver v1.17.4 2e1ba57fe95a 4 months ago 171 MB
k8s.gcr.io/kube-controller-manager v1.17.4 7f997fcf3e94 4 months ago 161 MB
registry.cn-hangzhou.aliyuncs.com/openthings/k8s-gcr-io-kube-controller-manager v1.17.4 7f997fcf3e94 4 months ago 161 MB
k8s.gcr.io/kube-scheduler v1.17.4 5db16c1c7aff 4 months ago 94.4 MB
registry.cn-hangzhou.aliyuncs.com/openthings/k8s-gcr-io-kube-scheduler v1.17.4 5db16c1c7aff 4 months ago 94.4 MB
k8s.gcr.io/coredns 1.6.5 70f311871ae1 8 months ago 41.6 MB
registry.cn-hangzhou.aliyuncs.com/openthings/k8s-gcr-io-coredns 1.6.5 70f311871ae1 8 months ago 41.6 MB
k8s.gcr.io/etcd 3.4.3-0 303ce5db0e90 8 months ago 288 MB
registry.cn-hangzhou.aliyuncs.com/openthings/k8s-gcr-io-etcd 3.4.3-0 303ce5db0e90 8 months ago 288 MB
k8s.gcr.io/pause 3.1 da86e6ba6ca1 2 years ago 742 kB
registry.cn-hangzhou.aliyuncs.com/openthings/k8s-gcr-io-pause 3.1 da86e6ba6ca1 2 years ago 742 kB
1.2 拉取镜像
[root@hadoop66 ~]# docker pull 镜像名称和版本
[root@hadoop66 ~]# docker pull mysql:5.7
Trying to pull repository docker.io/library/mysql ...
5.7: Pulling from docker.io/library/mysql
Digest: sha256:ea560da3b6f2f3ad79fd76652cb9031407c5112246a6fb5724ea895e95d74032
Status: Image is up to date for docker.io/mysql:5.7
1.3 显示已有容器,显示当前运行容器
[root@hadoop66 ~]# docker ps
[root@hadoop66 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
432e960f8b11 mysql:5.7 "docker-entrypoint..." 23 minutes ago Up 10 minutes mysql_server
e1ed0367e6c4 xcli0621/neo4j:3.4.5 "/sbin/tini -g -- ..." 5 days ago Up 14 hours (healthy) 0.0.0.0:7474->7474/tcp, 7473/tcp, 0.0.0.0:7687->7687/tcp neo4j
2e2ce63df755 xcli0621/postgres:9.5.5 "docker-entrypoint..." 5 days ago Up 27 hours (healthy) 0.0.0.0:5432->5432/tcp postgres
1.4 创建容器
[root@hadoop66 ~]# docker run -d --name 自定义容器名称 镜像ID或镜像名称
-d 后台运行,--name 设置名称,容器名称,-p 端口映射,第一个3306是当前主机的端口,第二个3306指容器中的端口,-e 设置root用户密码,mysql:5.7为镜像名称
[root@hadoop66 ~]# docker run --name mysql_server -p 3306:3306 -e MYSQL_ROOT_PASSWORD=自定密码 -d mysql:5.7
432e960f8b11ec158f7c776e1a94b6428904d53a2039e17a880c7a383841343d
1.5 运行容器
docker start 容器名称或者容器ID
1.6 停止容器
docker stop 容器名称或者容器ID
1.7 进入容器内部
docker exec -it 容器名称或者容器ID
2. docker安装mysql 步骤
2.1 拉取mysql 5.7 镜像
[root@hadoop66 ~]# docker pull mysql:5.7
Trying to pull repository docker.io/library/mysql ...
5.7: Pulling from docker.io/library/mysql
8559a31e96f4: Pull complete
d51ce1c2e575: Pull complete
c2344adc4858: Pull complete
fcf3ceff18fc: Pull complete
16da0c38dc5b: Pull complete
b905d1797e97: Pull complete
4b50d1c6b05c: Pull complete
0a52a5c57cd9: Pull complete
3b816a39d367: Downloading [===> ] 2.62 MB/42.96 MB
13ee22d6b3bb: Download complete
e517c3d2ba35: Download complete
2.2 创建docker容器 mysql_server
[root@iZwz9ib7bcz07jq4653ommZ ~]# docker run --name mysql_server -p 3306:3306 -e MYSQL_ROOT_PASSWORD=test1234 -d mysql:5.7
432e960f8b11ec158f7c776e1a94b6428904d53a2039e17a880c7a383841343d
2.2 查看mysql_server运行的容器实例
[root@hadoop66 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
432e960f8b11 mysql:5.7 "docker-entrypoint..." 23 minutes ago Up 10 minutes mysql_server
e1ed0367e6c4 xcli0621/neo4j:3.4.5 "/sbin/tini -g -- ..." 5 days ago Up 14 hours (healthy) 0.0.0.0:7474->7474/tcp, 7473/tcp, 0.0.0.0:7687->7687/tcp neo4j
2e2ce63df755 xcli0621/postgres:9.5.5 "docker-entrypoint..." 5 days ago Up 27 hours (healthy) 0.0.0.0:5432->5432/tcp postgres
2.3 进入docker容器mysql_server命令行
[root@hadoop66 ~]# docker exec -it mysql_server bash
root@432e960f8b11:/# 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.31 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>
2.4 mysql 5.7版本授权
此处为mysql5.7的授权,非8.0版本,mysql8.0版本方法改变了,我其他文章有写。
示例中test1234为数据库密码
[root@hadoop66 ~]# docker exec -it mysql_server bash
root@432e960f8b11:/# 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.31 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.
";
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql>
mysql> GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "test1234";
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> exit
Bye
root@432e960f8b11:/# exit
exit
[root@hadoop66 ~]#