docker学习

一.docker命令

1.Docker 启动与停止容器

启动已运行过的容器

docker start 容器名称|容器id
  如: docker start mycentos

  

启动所有运行过的容器(注意:反单引号` `), docker ps -a -q 是查询所有运行过的容器ID

docker start `docker ps -a -q`
  

停止正在运行的容器(正常停止)

docker stop 容器名称|容器id
  如: docker stop mycentos

  

强制停止正在运行的容器(一般不用此,除非卡了)

docker kill 容器名称|容器id
  如: docker kill mycentos1

  

停止所有在运行的容器

docker stop `docker ps -a -q`

2.Docker操作命令——查看、停止、删除容器

列出所有容器 ID
1
docker ps -aq
停止所有容器
1
docker stop $(docker ps -aq)
停止单个容器
1
docker stop 要停止的容器名
删除所有容器
1
docker rm $(docker ps -aq)
删除单个容器
1
docker rm 要删除的容器名
删除所有的镜像
1
docker rmi $(docker images -q)

3.用docker创建MySQL容器并设置使用navicat连接

docker pull mysql 			//直接下载最新版本MySQL的image
docker pull mysql:5.7		//直接下载MySQL5.7的image
$docker images              //查看images
$ docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=数据库密码 --name 容器名 mysql   //后台启动mysql容器
--简单注释:
	-d 后台运行
	-p 端口映射,把容器中的3306端口映射到宿主机的3307端口
	-e MySQL参数,设置root密码为123456
	-name 给容器命名为my_mysql

进入MySQL容器内做配置:
$ docker exec -it my_mysql 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.13 MySQL Community Server - GPL
Copyright (c) 2000, 2018, 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>
设置允许远程连接,刷新权限
mysql> grant all privileges on *.* to 'root'@'%' identified by '数据库密码';
Query OK, 0 rows affected (0.35 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

4.docker管理

1. 创建 Apache 容器
[root@centos7 ~]# docker run -d -p 80:80 httpd

// -d: 放入后台运行
// -p: 指定端口映射关系(第一个为本地端口、第二个为容器端口)


2. 创建 Nginx 容器
[root@centos7 ~]# docker run -d -p 8080:80 nginx

// 将本地的 8080 端口映射到容器的 80 端口;
 
3. 查看容器
[root@centos7 ~]# docker ps -a
// docker ps 命令时查看运行中的容器
// -a 选项是查看所以容器(不论容器属于什么状态)

4. 查看 docker 标准输出
[root@centos7 ~]# docker ps -a
[root@centos7 ~]# docker logs “容器ID”


5.. 停止和启动容器
# 查看容器ID:
[root@centos7 ~]# docker ps -a

# 停止容器,使用容器ID来指定:
[root@centos7 ~]# docker stop “容器ID”

# 再次查看容器运行状态:
[root@centos7 ~]# docker ps -a

# 启动已停止的容器,ID来指定:
[root@centos7 ~]# docker start “容器ID”

# 再次查看容器运行状态:
[root@centos7 ~]# docker ps -a


6.. 删除容器
# 查看容器:
[root@centos7 ~]# docker ps -a

# 强制删除容器(谨慎使用该命令)
[root@centos7 ~]# docker rm -f “容器ID”

7. 查询端口映射
[root@centos7 ~]# docker port “容器ID”
80/tcp -> 0.0.0.0:80
8. 容器为任务而生
[root@centos7 ~]# docker run httpd echo "www"
www
[root@centos7 ~]# docker ps -a


9.进入容器修改内容
[root@centos7 ~]# docker exec -it “容器ID” /bin/bash
root@f1507b05b31c:/usr/local/apache2#        # 已经入容器

root@f1507b05b31c:/usr/local/apache2/htdocs# pwd
/usr/local/apache2/htdocs
root@f1507b05b31c:/usr/local/apache2/htdocs# echo Hello > index.html


注:容器本身是个精简化的东西,未自带vi和vim命令,在未安装之前我们简单通过重定向进入测试。



10. 查询容器详细信息
[root@centos7 ~]# docker inspect “容器ID”


11. 复制本地文件到容器中
[root@centos7 ~]# docker cp /etc/profile 容器ID:/root
[root@centos7 ~]# docker exec -it f15 /bin/bash
// “f15” 是容器的省略ID


12. 本地目录跟容器目录挂载
创建本地挂载目录:

[root@centos7 ~]# mkdir /root/httpd_data
创建容器:

[root@centos7 ~]# docker run -d -p 80:80 -v /root/httpd_data:/usr/local/apache2/htdocs  --privileged=true  httpd
// -v 本地目录/root/httpd_data 容器目录/usr/local/apache2/htdocs
// --privileged=true 关闭安全权限,否则你容器操作文件夹没有权限


测试目录是否挂载成功:

# 在本地挂载的目录下创建一个文件,我这里创建的是HTML文件
[root@centos7 ~]# cd httpd_data/
[root@centos7 httpd_data]# cat test.html
Test
进入容器目录查看是否已创建:


# 进入容器
[root@centos7 httpd_data]# docker exec -it 24 /bin/bash

# 进入挂载目录
root@246f1a196a9b:/usr/local/apache2# ls
bin  build  cgi-bin  conf  error  htdocs  icons  include  logs    modules
root@246f1a196a9b:/usr/local/apache2# cd htdocs/

# 查看文件内容
root@246f1a196a9b:/usr/local/apache2/htdocs# ls
test.html
root@246f1a196a9b:/usr/local/apache2/htdocs# cat test.html 
Test

# 完成容器目录跟本地目录的挂载!

二.docker遇到的问题

1.docker启动mysql 连不上数据库 (2003:Can’t connect to MySQL server (10060))

# docker进入mysql容器

docker exec -it mysql容器名或者容器id /bin/bash

# 进入mysql

mysql

# 执行命令

grant all privileges on *.* to 'root'@'%' identified by '数据库密码';
flush privileges;

# 重启mysql容器

docker restart mysql容器名或者容器id

2.配置mysql允许远程连接的方法

默认情况下,mysql只允许本地登录,如果要开启远程连接,则需要修改/etc/mysql/my.conf文件。

一、修改/etc/mysql/my.conf
找到bind-address = 127.0.0.1这一行
改为bind-address = 0.0.0.0即可

二、为需要远程登录的用户赋予权限
1、新建用户远程连接mysql数据库
grant all on *.* to admin@'%' identified by '123456' with grant option; 
flush privileges;
允许任何ip地址(%表示允许任何ip地址)的电脑用admin帐户和密码(123456)来访问这个mysql server。
注意admin账户不一定要存在。

2、支持root用户允许远程连接mysql数据库
grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
flush privileges;

3.docker _port is already allocated错误

ps -aux | grep -v grep | grep docker-proxy 第二列为进程号

停止 doker 进程,删除所有容器,然后删除 local-kv.db 这个文件,再启动 docker 就可以了。

sudo service docker stop
docker rm $(docker ps -aq)
sudo rm /var/lib/docker/network/files/local-kv.db
sudo service docker start

参考资料:https://blog.csdn.net/u013939884/article/details/103558570
https://www.cnblogs.com/linjiqin/p/5270938.html
https://blog.csdn.net/weixin_45087693/article/details/112849808
https://blog.csdn.net/KgdYsg/article/details/101785398
https://blog.csdn.net/qq_21856521/article/details/86715016
创建关于docker的mysql容器有挂载
https://www.cnblogs.com/liconglong/p/11685316.html
https://www.cnblogs.com/l-hh/p/10772942.html
关注博主:
https://blog.csdn.net/u013939884?t=1
https://blog.csdn.net/weixin_45087693?t=1
https://blog.csdn.net/qq_21856521?t=1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Summer524!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值