欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【docker安装以及基本命令】

程序员文章站 2024-02-02 10:35:16
...

一、docker:基于容器的虚拟化(将应用托管到应用容器上),实现了一种应用程序级别的资源隔离及配额。

什么是容器?
容器:轻量级的虚拟化(共享同一系统内核),容器是由容器镜像来运行
优点:容器密度高,启动快,没有太多额外的开销;缺点:只能在linux操作系统
虚拟化和容器的区别?
不同点:两者都讲究的是隔离,虚拟化是物理机上安装虚拟机然后安装多种操作系统(常用的vmwore、openstack、kvm都是使用的虚拟化技术);容器是直接将容器层安装在linux操作系统之上实现各种板块隔离

1.docker安装

方法一:使用官方安装脚本自动安装
实际上就是下载一个安装脚本,再执行安装(不推荐,因为不能选择版本安装)
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
方法二:使用yum进行安装,不推荐
yum install docker
这样直接从光盘或yum仓库里安装,版本较低
方法三:使用yum添加源进行安装指定版本,推荐
(1)添加docker-ce 源信息

yum-config-manager --add-repo=https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

(2)更新yum源:是把软件包信息提前把本地缓存一份,为了提高安装搜索软件的速度

yum makecache fast

(3)安装docker-ce

yum -y install docker-ce  #默认下载的是最新版本

① 查看都有哪些docker版本
yum list docker-ce.x86_64 --showduplicates | sort -r
② 下载指定版本,这里下载的稳定版本
yum -y install docker-ce-17.03.2.ce

测试:docker version / docker -v

[aaa@qq.com yum.repos.d]# docker version
Client: Docker Engine - Community
 Version:           19.03.12
 API version:       1.40
 Go version:        go1.13.10
 Git commit:        48a66213fe
 Built:             Mon Jun 22 15:46:54 2020
 OS/Arch:           linux/amd64
 Experimental:      false
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running? 
 #说明docker服务没有开启
 [aaa@qq.com yum.repos.d]# docker -v
Docker version 1.13.1, build 64e9980/1.13.1s

2.开启服务:systemctl enable --now docker

注1:安装过程中可能出现的问题:
原因是可能之前已经安装过旧版本的docker,在安装的时候报错如下
Transaction check error: file /usr/bin/docker from install of docker-ce-cli-1:19.03.12-3.el7.x86_64 conflicts with file from package docker-common-2:1.13.1-161.git64e9980.el7_8.x86_64 file /usr/bin/dockerd from install of docker-ce-3:19.03.12-3.el7.x86_64 conflicts with file from package docker-common-2:1.13.1-161.git64e9980.el7_8.x86_64
解决:卸载旧版本的包
yum remove docker-common-2:1.13.1-161.git64e9980.el7_8.x86_64
再次安装docker就会成功安装!

注2::[aaa@qq.com ~]# docker --help 帮助命令

二、docker三大核心:镜像、容器、仓库

镜像:类似虚拟机镜像
容器:类似linux系统环境,运行和隔离应用。容器从镜像启动的时候,docker会在镜像的最上一层创建一个可写层,镜像本身是只读的,保持不变
仓库:每个仓库存放某一类镜像。分为两类:docker hub共有仓库、docker-registry私有仓库。(图形化构建docker仓库)

【docker安装以及基本命令】

三、docker操作

1.仓库相关命令

docker search 镜像名 从仓库搜索镜像
docker pull 镜像名 从仓库拉出镜像,默认拉取的最新版
docker push 镜像名 向仓库推进镜像 【hub.docker.com必须在网页上注册一个账号(docker hub注册账号),才能把镜像推到仓库;然后在命令行使用docker login登录】

eg1.从仓库搜索镜像centos

[aaa@qq.com ~]# docker search centos  
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
centos                             The official build of CentOS.                   6061                [OK]                
ansible/centos7-ansible            Ansible on Centos7                              130                                     [OK]
consol/centos-xfce-vnc             Centos container with "headless" VNC session…   116                                     [OK]
jdeathe/centos-ssh                 OpenSSH / Supervisor / EPEL/IUS/SCL Repos -114 
...................

eg2: 从仓库拉取镜像busybox

[aaa@qq.com ~]# docker pull busybox 
Using default tag: latest
latest: Pulling from library/busybox
76df9210b28c: Pull complete 
Digest: sha256:95cf004f559831017cdf4628aaf1bb30133677be8702a8c5f2994629f637a209
Status: Downloaded newer image for busybox:latest
docker.io/library/busybox:latest

eg3:向仓库推进镜像
hub.docker.com必须在网页上注册一个账号(docker hub注册账号),才能把镜像推到仓库

docker push 镜像名
docker login

2.镜像相关命令

docker search 镜像名 搜索镜像
docker pull 镜像名 拉出镜像,默认拉取的最新版
docker image save 镜像名 > name.tar.gz 导出镜像
docker image rm 镜像名 删除镜像(若要删除正在运行的镜像加-f)
docker image load -i 文件名 导入镜像
docker image ls 查看当前镜像列表
docker image inspect 镜像名 查看镜像详细信息
docker image tag 镜像名:latest 镜像名:tag名 给镜像打标记,一般用在把镜像push仓库

eg1:查看当前镜像列表,查看镜像详细信息,查看busybox镜像历史记录

[aaa@qq.com ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
busybox             latest              1c35c4412082        3 weeks ago         1.22MB
[aaa@qq.com ~]# docker image inspect busybox 
[aaa@qq.com ~]# docker image history busybox
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
1c35c4412082        3 weeks ago         /bin/sh -c #(nop)  CMD ["sh"]                   0B                  
<missing>           3 weeks ago         /bin/sh -c #(nop) ADD file:a84c53d2fe5207d17…   1.22MB 

eg2:导出镜像:
方法1:docker image save 镜像名 > 路径位置信息(name.tar.gz)

[aaa@qq.com ~]# docker image save busybox > busybox1.tar.gz

方法2:docker image save -o 路径位置信息(name.tar.gz) 镜像名:latest

[aaa@qq.com ~]# docker image save -o busybox2.tar.gz busybox:latest

eg3:删除镜像:docker image rm 镜像名

[aaa@qq.com ~]# docker image rm busybox
Untagged: busybox:latest
Untagged: aaa@qq.com:95cf004f559831017cdf4628aaf1bb30133677be8702a8c5f2994629f637a209
Deleted: sha256:1c35c441208254cb7c3844ba95a96485388cef9ccc0646d562c7fc026e04c807
Deleted: sha256:1be74353c3d0fd55fb5638a52953e6f1bc441e5b1710921db9ec2aa202725569
[aaa@qq.com ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              7                   b5b4d78bc90c        7 weeks ago         203MB

eg4:导入镜像:docker image load -i 路径位置信息

[aaa@qq.com ~]# docker image load -i busybox1.tar.gz
1be74353c3d0: Loading layer  1.437MB/1.437MB
Loaded image: busybox:latest
[aaa@qq.com ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
busybox             latest              1c35c4412082        3 weeks ago         1.22MB
centos              7                   b5b4d78bc90c        7 weeks ago         203MB

或者

[aaa@qq.com ~]# docker image import busybox1.tar.gz busybox:v1
sha256:70fe82f93ddc2f501380d3749e743be3e9afd3d9dfc106ae2e22c92d830f91df
[aaa@qq.com ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
busybox             v1                  70fe82f93ddc        6 seconds ago       1.44MB
centos              7                   b5b4d78bc90c        7 weeks ago         203MB

eg5:给镜像打标记:tag 一般用在把镜像push进仓库
docker image tag 镜像名:latest 镜像名:tag名

[aaa@qq.com ~]# docker image tag busybox:latest busybox:v1.1
[aaa@qq.com ~]# docker image tag busybox:latest busybox:v1.2
[aaa@qq.com ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
busybox             latest              1c35c4412082        3 weeks ago         1.22MB
busybox             v1.1                1c35c4412082        3 weeks ago         1.22MB
busybox             v1.2                1c35c4412082        3 weeks ago         1.22MB
centos              7                   b5b4d78bc90c        7 weeks ago         203MB
[aaa@qq.com ~]# docker image rm busybox:v1.1 busybox:v1.2
Untagged: busybox:v1.1
Untagged: busybox:v1.2
[aaa@qq.com ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
busybox             latest              1c35c4412082        3 weeks ago         1.22MB
centos              7                   b5b4d78bc90c        7 weeks ago         203MB

docker镜像帮助命令

[aaa@qq.com ~]# docker image --help

Usage:	docker image COMMAND

Manage images

Commands:
  build       Build an image from a Dockerfile
  history     Show the history of an image
  import      Import the contents from a tarball to create a filesystem image
  inspect     Display detailed information on one or more images
  load        Load an image from a tar archive or STDIN
  ls          List images
  prune       Remove unused images
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rm          Remove one or more images
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

Run 'docker image COMMAND --help' for more information on a command.

3.容器相关命令:

docker run 镜像名 [-i/-d/-t/–name] 启动容器
docker stop 容器名字/容器ID 停止容器
docker kill 容器名字/容器ID 杀死运行中的容器
docker rm 容器名/容器ID 删除容器(加-f 表示删除运行中的)
docker start 容器名字/容器ID **关闭中的容器
docker ps 查看正在运行的容器的信息(状态)
docker ps -a 查看所有容器的信息(状态)
docker ps -q 查看正在运行的容器的ID号
docker ps -aq 查看所有容器的ID号
docker inspect 容器名字/容器ID 查看某个容器的详细信息
docker exec -it 容器名 指定的shell 对运行的容器执行指定命令exec

eg1:启动容器
方法1(不推荐):
先创建一个容器:docker create 镜像名
再启动容器:docker start 容器名

[aaa@qq.com ~]# docker create busybox:latest 
47f2de8f995999651498e6f0b58764214817e182ad8eff32b2b0b231f5fd3628
[aaa@qq.com ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
47f2de8f9959        busybox:latest      "sh"                16 seconds ago      Created                                 recursing_galileo
[aaa@qq.com ~]# docker start recursing_galileo
recursing_galileo
[aaa@qq.com ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[aaa@qq.com ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
47f2de8f9959        busybox:latest      "sh"                58 seconds ago      Exited (0) 9 seconds ago                       recursing_galileo

方法2:docker run 镜像名 [-d/-i/-t/–name],这种启动的话进去又退出的话状态就变成Exited状态

[aaa@qq.com ~]# docker run -it busybox:latest 
/ # ls
bin   dev   etc   home  proc  root  sys   tmp   usr   var
/ # exit
[aaa@qq.com ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[aaa@qq.com ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
1fcc320d6f11        busybox:latest      "sh"                23 seconds ago      Exited (0) 10 seconds ago                       gracious_hopper
47f2de8f9959        busybox:latest      "sh"                3 minutes ago       Exited (0) 2 minutes ago                        recursing_galileo

而docker run 镜像名 [-d/-i/-t/–name],进去按CTRL+p ,CTRL+Q的话,状态是UP

[aaa@qq.com ~]# docker run -it busybox:latest 
/ # [aaa@qq.com ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
328a8dbff50f        busybox:latest      "sh"                9 seconds ago       Up 8 seconds                            vigorous_easley

或者用后台(-d)执行的方式,使状态成为UP【推荐】

[aaa@qq.com ~]# docker run -idt --name c1 busybox:latest 
baafbd0b1a3dfe19692929cac68e29096652c277140ccdc48f2ec8bd331631ac
[aaa@qq.com ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
baafbd0b1a3d        busybox:latest      "sh"                10 seconds ago      Up 9 seconds                            c1

eg2:停止运行的容器:

[aaa@qq.com ~]# docker stop vigorous_easley
vigorous_easley
[aaa@qq.com ~]# docker stop baafbd0b1a3d
baafbd0b1a3d
[aaa@qq.com ~]# docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

eg3: 杀死运行的容器

[aaa@qq.com ~]# docker kill competent_robinson
competent_robinson

eg4:**关闭的容器:

[aaa@qq.com ~]# docker start competent_robinson
competent_robinson

eg5:查看容器的详细信息:

[aaa@qq.com ~]# docker inspect 9f91c7213a7d
[
    {
        "Id": "9f91c7213a7dda367f8fe152403eb2b6612c2849bb9a06299e360378db3c9f8e",
        "Created": "2020-06-25T09:34:09.421984223Z",
        "Path": "/bin/sh",
        "Args": [],
        "State": {
            "Status": "exited",
            "Running": false,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 0,
            "ExitCode": 0,

查看正在运行的容器的ID号

[aaa@qq.com ~]# docker ps -q 
f658dc1f9c9f

查看所有的容器的ID号

[aaa@qq.com ~]# docker ps -aq 
f658dc1f9c9f
328a8dbff50f
baafbd0b1a3d
1fcc320d6f11
47f2de8f9959

eg6:删除容器:docker rm 容器名或容器ID
删除所有的容器:查询所有容器 IDdocker ps -aq,加-f表示删除运行中的

[aaa@qq.com ~]# docker rm -f `docker ps -aq`
f658dc1f9c9f
328a8dbff50f
baafbd0b1a3d
1fcc320d6f11
47f2de8f9959
[aaa@qq.com ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

eg7:对运行的容器执行指定命令exec:

[aaa@qq.com ~]# docker exec -it centos7 /bin/bash
[aaa@qq.com73e57bae16bf /]# cat /etc/redhat-release 
CentOS Linux release 7.8.2003 (Core)
[aaa@qq.com73e57bae16bf /]# exit

查询容器内部日志:
docker logs

四、外部访问docker容器:docker run -p(-p表示端口映射);也就是将应用托管到应用容器上

1.从仓库拉出Mysql镜像

[aaa@qq.com ~]# docker pull mysql:5.6
5.6: Pulling from library/mysql
7d2977b12acb: Pull complete 
5fb8400e7f07: Pull complete 
234877fbb165: Pull complete 
6fe1021f12f3: Pull complete 
7e36fe6b53f0: Pull complete 
996ec709c11b: Pull complete 
5198b7523387: Pull complete 
cc9bdad4dcc0: Pull complete 
380cd37ad979: Downloading  6.446MB/64.2MB
d64465acf034: Download complete 
d4ee6606b3ab: Download complete 
380cd37ad979: Downloading  18.26MB/64.2MB
380cd37ad979: Downloading  29.01MB/64.2MB
380cd37ad979: Pull complete 
d64465acf034: Pull complete 
d4ee6606b3ab: Pull complete 
Digest: sha256:2bf1a0a05a6ad437dcac6689e48a9c33774ac92c6213fce2c4196343210592f3
Status: Downloaded newer image for mysql:5.6
docker.io/library/mysql:5.6

2.启动容器mysql(并设置root密码),并且设置外部端口映射

[aaa@qq.com ~]# docker run --name mysql -itd -p 3305:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.6 
97be89fd63737771d36882d512f86cf484c5518de9a56848d68ca8ed31edc42c

3.外部安装mysql5.6,并且查看端口3305是否监听
yum whatprovides mysql
yum install mariadb -y

[aaa@qq.com ~]# netstat -ltunp | grep 3305
tcp6       0      0 :::3305                 :::*                    LISTEN      6379/docker-proxy  

4.从外部访问mysql容器
方法一:docker exec -it mysql mysql -uroot -p123456

[aaa@qq.com ~]# docker exec -it mysql mysql -uroot -p123456
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 1
Server version: 5.6.48 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> select version();
+-----------+
| version() |
+-----------+
| 5.6.48    |
+-----------+
1 row in set (0.00 sec)

mysql> exit
Bye

方法2:mysql -uroot -p -h172.17.0.2

[aaa@qq.com ~]# mysql -uroot -p -h172.17.0.2
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.48 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> exit
Bye

注:外部是直接不能进入mysql的,因为没有权限

相关标签: docker