docker常用命令详解

docker常用命令详解

  • 一、docker概念复习
  • 二、docker中常用命令
    • 1、 docker相关命令
      • 查看docker的版本
      • 查看docker系统的相关信息
      • 重启 Docker 服务的命令
    • 2、容器相关命令
      • 查看容器启动的进程
      • 启动容器
      • 停止(关闭)容器
      • 删除容器---未启动
      • 进入容器内部修改信息
      • 查看容器内部的进程
      • 查看容器的网络类型
      • 查看容器资源的消耗
      • 查看容器日志
    • 3、镜像相关命令
      • 创建镜像
      • 删除镜像
      • 导出镜像
      • 导入镜像到另外一台机器
  • 三、docker镜像与容器的区别
  • 四、dockerfile
    • 1、下载镜像
    • 2、启动MySQL容器的实例
    • 3、启动失败的排错过程
    • 4、SQLyog连接mysql容器原理图

一、docker概念复习

1、docker属于c/s架构的:client-server
2、docker里面存放的是软件、镜像(相当于将软件放入容器中运行、跑)
3、镜像:来源问题,默认从docker官方提供的网站去下载。镜像相当于一个安装包(自我理解)
images镜像:是一个包含了程序代码、基础操作系统,以及程序启动所依赖软件和库,在容器运行的整体单元,镜像本质上就是一个文件。
docker hub is the world’s

docker hub:存放镜像的
docker git:存放代码的
仓库:存放docker镜像的

凡是你想要得到的一个软件,都有一个镜像,我们直接启动镜像就可以了,获得整个软件。
4、docker容器启动软件,颠覆了我们传统的软件安装的方式。
容器在linux里就对应一个进程。
5、docker和虚拟机的区别:docker耗费的系统资源占内存少;
docker优势:起一个容器,消耗的资源和cpu也比较少。
docker缺点:有隔离。

二、docker中常用命令

docker的三个概念:
容器 镜像 仓库
docker pull flask

1、 docker相关命令

查看docker的版本

docker version

查看docker系统的相关信息

docker info:查看docker的版本、镜像和容器数量、存储驱动
docker container inspect 【容器名】:查看容器的详细信息

重启 Docker 服务的命令

service docker restart 会导致所有正在运行的容器暂停。

[root@cali yum.repos.d]# service docker restart   会导致所有的正在运行的容器暂停
Redirecting to /bin/systemctl restart docker.service
[root@cali yum.repos.d]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

2、容器相关命令

查看容器启动的进程

docker ps:查看容器启动的进程
docker ps -a:查看启动哪些容器,包括没有启动的

启动容器

① 启动容器的命令=创建一个新的容器
docker run = docker pull + docker create + docker start ② 启动一个已经存在的容器,启动已经存在的
docker start 容器名: 启动容器

停止(关闭)容器

docker stop 【容器名】: 关闭/停止容器
docker stop sc-nginx :停止一个nginx这个容器

删除容器—未启动

docker rm 【sc-mysql-2】:删除没有启动的容器

进入容器内部修改信息

进入sc-nginx-1 容器里面

docker exec -it sc-nginx-1 /bin/bash

查看容器内部的进程

docker top 【容器名】:查看相应容器的更详细信息;查看容器里运行了哪些程序(哪些进程)

查看容器的网络类型

== docker network ls==: 查看容器的网络类型

查看容器资源的消耗

docker stats 【容器名】:查看资源的消耗,查看容器使用的cpu和内存、网络等信息.
docker stats :Docker 提供的一个用于实时显示一个或多个容器的资源使用情况的工具。

查看容器日志

docker logs 容器名: 查看容器启动的日志
docker logs 【选项】 : 查看docker的错误日志。

3、镜像相关命令

创建镜像

docker create创建一个新的镜像

删除镜像

==docker rmi 【镜像名】==删除镜像

[root@cali yum.repos.d]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS                     PORTS                                   NAMES
a5b752cc4485   mysql:5.7.39   "docker-entrypoint.s…"   6 minutes ago   Exited (1) 6 minutes ago                                           sc-mysql-1
bda1e5c73838   nginx          "/docker-entrypoint.…"   3 hours ago     Up 3 hours                 0.0.0.0:8090->80/tcp, :::8090->80/tcp   sc-nginx
[root@cali yum.repos.d]# docker rm sc-mysql-1  删除启动失败的容器,正在运行的容器不能直接删除
sc-mysql-1

导出镜像

docker save -o 保存的文件名.tar 镜像名称:标签
docker save -o nginx.tar nginx 默认导出到当前目录下
就是将nginx镜像保存为nginx.tar文件

[root@sc-master ~]# docker save -o nginx.tar nginx
[root@sc-master ~]# ls
a                            grafana-enterprise-8.5.3-1.x86_64.rpm  nginx-1.21.6.tar.gz
?[A                          hello.c                                nginx.tar

docker save -o nginx_latest.tar nginx:latest 保存名为 nginx 且标签为 latest 的镜像,会把 nginx:latest 镜像保存为 nginx_latest.tar 文件。

导入镜像到另外一台机器

镜像的导入和导出:docker load [OPTIONS]
常见的 OPTIONS 选项:

-i, --input:指定要加载的镜像归档文件的路径,此选项是最常用的,它能明确加载操作的数据源。
-q, --quiet:精简输出信息,在加载过程中只显示关键结果,避免过多日志信息的干扰。

docker load -i nginx_image.tar Docker 会解析 nginx_image.tar 文件,将其中的镜像数据加载到本地,并且会保留镜像原有的标签信息。

三、docker镜像与容器的区别

在企业都是自己去制作镜像。
容器:运行镜像的地方
docker镜像image:是一堆只读文件,容器(container)的定义和镜像(images)几乎一模一样,也是一堆层的统一视角,唯一的区别在于容器的最上面那一层是可读可写的。
容器=镜像+读写层
docker常用命令详解_第1张图片

四、dockerfile

是制作docker镜像的文件,理解成一个配方文件。

https://hub.docker.com/_/mysql

使用容器安装mysql 5.7.39

1、下载镜像

[root@sc-master ~]# docker pull mysql:5.7.39
5.7.39: Pulling from library/mysql
9815334b7810: Pull complete 
f85cb6fccbfd: Pull complete 
b63612353671: Pull complete 
[root@sc-master ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
mysql         5.7.39    daff57b7d2d1   8 hours ago     430MB
nginx         latest    2b7d6430f78d   2 days ago      142MB
hello-world   latest    feb5d9fea6a5   11 months ago   13.3kB

2、启动MySQL容器的实例

1、相当于:授权给用户所有的权限

grant all on * * to ‘root’@‘%’ identified by ‘sc123456’,

2、启动MySQL容器

docker run -d --name sc-mysql-1 -p 33060:3306 -e MYSQL_ROOT_PASSWORD='sc123456' mysql:5.7.39
[root@sc-master ~]# docker run -d --name sc-mysql-1 -p 33060:3306  -e MYSQL_ROOT_PASSWORD='sc123456'  mysql:5.7.39
708c717886494f6335787cbc5c8ea72c55658cd4e67c2ccdae5a204734d86fab

3、如果启动时候输入的指令输入错误,可以通过查看错误日志。
docker logs [OPTIONS] CONTAINER 查看docker的日志,查看容器失败的日志。
docker logs +名字/container/containid 重新启动容器的时候启动不了,日志上面说明需要删除容器。
docker rmi nginx 删除镜像(docker image:查看docker里面的镜像)
docker rm sc-nginx 删除启动失败的容器(docker ps -a:查看容器的状态),正在运行的容器不能删除,需要先停止在删除。

进入MySQL容器里面:
docker exec -it sc-mysql-1
bash

[root@sc-master ~]# docker exec -it sc-mysql-1 bash
bash-4.2# 
bash-4.2# 
bash-4.2# cat /ect/redhat-release
cat: /ect/redhat-release: No such file or directory

进入mysql容器里
docker exec 进入容器内部,执行命令 execute

-it 开启一个终端,交互式登录进入
sc-mysql-1 容器的名字
bash 进入容器里运行的程序

在容器内部登录进入容器里的mysql

bash-4.2# mysql -uroot -p'sc123456'  在容器内部登陆进入容器里的mysql
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.39 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, 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 databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)
mysql> create  database sanchuang;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sanchuang          |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

然后在容器中再创建一个mysql2的容器,用来测试,然后再MySQL1中创建的数据库,在mysql2中不能看到。

[root@sc-master ~]#  docker run -d --name sc-mysql-2 -p 33061:3306  -e MYSQL_ROOT_PASSWORD='.39
c5b3bf0ae287598fd60e42a1514abef169626995edb3acff36e26d73c29e0395
[root@sc-master ~]# docker exec -it sc-mysql-2 bash
bash-4.2# mysql -uroot -p'sc123456' 
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.39 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, 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 databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

3、启动失败的排错过程

[root@cali yum.repos.d]# docker logs a5b752cc4485 查看容器启动失败的日志
2022-08-25 07:05:50+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.39-1.el7 started.
2022-08-25 07:05:50+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2022-08-25 07:05:50+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.39-1.el7 started.
2022-08-25 07:05:50+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified
    You need to specify one of the following:
    - MYSQL_ROOT_PASSWORD
    - MYSQL_ALLOW_EMPTY_PASSWORD
    - MYSQL_RANDOM_ROOT_PASSWORD
[root@cali yum.repos.d]# 
[root@cali yum.repos.d]# docker logs sc-mysql-1
2022-08-25 07:05:50+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.39-1.el7 started.
2022-08-25 07:05:50+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2022-08-25 07:05:50+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.39-1.el7 started.
2022-08-25 07:05:50+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified
    You need to specify one of the following:
    - MYSQL_ROOT_PASSWORD
    - MYSQL_ALLOW_EMPTY_PASSWORD
    - MYSQL_RANDOM_ROOT_PASSWORD

使用SQLyog去连接mysql。
账号:root 密码:sc123456 端口:3306

4、SQLyog连接mysql容器原理图

docker常用命令详解_第2张图片

你可能感兴趣的:(云原生,docker,eureka,容器,运维,云原生,云服务,linux)