docker学习

7 篇文章 0 订阅
3 篇文章 0 订阅

使用docker

Docker 运行在CentOS 7 上,要求系统为64位、系统内核版本为 3.10 以上。

我们采用 yum来安装(基于centOS7)

安装

  • 查看 linux内核版本是否符合要求

运行命令 :uname -r

[root@a ~]# uname -r
3.10.0-1062.1.2.el7.x86_64

满足

  • 更新yum

运行命令:yum update

yum update
# 更新过程中要求输入 y 确认
# 等待完成
  • 执行docker安装脚本

运行命令:curl -sSL https://get.docker.com/ | sh

[root@a ~]# curl -sSL https://get.docker.com/ | sh
# Executing docker install script, commit: f45d7c11389849ff46a6b4d94e0dd1ffebca32c1
+ sh -c 'yum install -y -q yum-utils'
+ sh -c 'yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo'
Loaded plugins: fastestmirror
adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
+ '[' stable '!=' stable ']'
+ sh -c 'yum makecache'
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
base                                                                              | 3.6 kB  00:00:00     
docker-ce-stable                                                                  | 3.5 kB  00:00:00     
epel                                                                              | 5.4 kB  00:00:00     
extras                                                                            | 2.9 kB  00:00:00     
mysql-connectors-community                                                        | 2.5 kB  00:00:00     
mysql-tools-community                                                             | 2.5 kB  00:00:00     
mysql56-community                                                                 | 2.5 kB  00:00:00     
updates                                                                           | 2.9 kB  00:00:00     
(1/14): docker-ce-stable/x86_64/updateinfo                                        |   55 B  00:00:00     
(2/14): docker-ce-stable/x86_64/primary_db                                        |  37 kB  00:00:00     
(3/14): docker-ce-stable/x86_64/filelists_db                                      |  18 kB  00:00:00     
(4/14): epel/x86_64/prestodelta                                                   | 4.0 kB  00:00:00     
(5/14): epel/x86_64/other_db                                                      | 3.3 MB  00:00:00     
(6/14): docker-ce-stable/x86_64/other_db                                          | 111 kB  00:00:00     
(7/14): epel/x86_64/updateinfo_zck                                                | 1.5 MB  00:00:00     
(8/14): epel/x86_64/filelists_db                                                  |  12 MB  00:00:00     
(9/14): mysql-tools-community/x86_64/filelists_db                                 | 192 kB  00:00:01     
(10/14): mysql56-community/x86_64/other_db                                        |  63 kB  00:00:00     
(11/14): mysql-connectors-community/x86_64/other_db                               |  13 kB  00:00:01     
(12/14): mysql56-community/x86_64/filelists_db                                    | 783 kB  00:00:01     
(13/14): mysql-tools-community/x86_64/other_db                                    |  15 kB  00:00:02     
(14/14): mysql-connectors-community/x86_64/filelists_db                           |  66 kB  00:00:21     
Metadata Cache Created
+ '[' -n '' ']'
+ sh -c 'yum install -y -q docker-ce'
warning: /var/cache/yum/x86_64/7/docker-ce-stable/packages/containerd.io-1.2.10-3.2.el7.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 621e9f35: NOKEY
Public key for containerd.io-1.2.10-3.2.el7.x86_64.rpm is not installed
Importing GPG key 0x621E9F35:
 Userid     : "Docker Release (CE rpm) <docker@docker.com>"
 Fingerprint: 060a 61c5 1b55 8a7f 742b 77aa c52f eb6b 621e 9f35
 From       : https://download.docker.com/linux/centos/gpg
setsebool:  SELinux is disabled.
If you would like to use Docker as a non-root user, you should now consider
adding your user to the "docker" group with something like:

  sudo usermod -aG docker your-user

Remember that you will have to log out and back in for this to take effect!

WARNING: Adding a user to the "docker" group will grant the ability to run
         containers which can be used to obtain root privileges on the
         docker host.
         Refer to https://docs.docker.com/engine/security/security/#docker-daemon-attack-surface
         for more information.

  • 验证是否安装好

运行命令:docker --version

[root@a ~]# docker --version
Docker version 19.03.4, build 9013bf583a

  • 测试是否可用

先启动docker

systemctl start docker

运行下一Helloworld

[root@a ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete 
Digest: sha256:c3b4ada4687bbaa170745b3e4dd8ac3f194ca95b2d0518b417fb47e5879d9b5f
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

常用操作

我这里是 centOS7,且用的root账号操作

  • 启动

运行命令:systemctl start docker

systemctl start docker

root     26938     1  1 10:08 ?        00:00:00 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
root     27075 26895  0 10:08 pts/0    00:00:00 grep --color=auto docker
# 看到docker启动了
  • 停止

运行命令:systemctl docker stop

systemctl docker stop

[root@zhangsan ~]# ps -ef|grep docker
root     26930 26895  0 10:07 pts/0    00:00:00 grep --color=auto docker
# 看到没有正在运行的 docker
  • 查看版本

运行命令:docker --version

[root@zhangsan ~]# docker --version
Docker version 19.03.4, build 9013bf583a
  • 查看本地已经有的镜像

运行命令:docker images

[root@zhangsan ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              fce289e99eb9        10 months ago       1.84kB

# hello-world 是测试时下载的镜像
  • 获取镜像

运行命令:docker pull <镜像>

[root@zhangsan ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
8d691f585fa8: Pull complete 
5b07f4e08ad0: Pull complete 
abc291867bca: Pull complete 
Digest: sha256:922c815aa4df050d4df476e92daed4231f466acc8ee90e0e774951b0fd7195a4
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest

# 这种方式省略了很多参数
# 默认获取的是最新的版本

想要获取具体版本docker pull nginx:xxx

部署一个web项目

  • 安装tomcat镜像包

命令:docker pull hub.c.163.com/library/tomcat:latest

  • 将自己打包好的war包上传到服务器

  • 创建Dockerfile文件

内容如下:

from hub.c.163.com/library/tomcat

MAINTAINER xxx xxx@126.com

copy mind_sub.war /usr/share/tomcat/webapps

MAINTAINER - 维护者的信息,名字 和邮箱

  • 根据docker file 构建镜像
docker build -t lottery:latest .

构建一个镜像 基于当前目录下的

使用 docker images,查看

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gqwc9EFa-1574074710861)(D:\typoraImages\1573800104006.png)]

我们刚刚生成了一个 叫做 lottery 的镜像

  • 运行自己的镜像

执行以下命令启动自己的镜像,并指定映射端口为:容器内部8080-》本地80端口

命令:docker run -d -p 80:8080 lottery

-d 后台运行

-p 指定端口号

lottery 是我们刚刚生成的镜像名字

  • 访问

访问地址 主机:80/项目名

80端口是htttp默认的,可以省略

其他命令

docker ps -a 列出所有容器, 不加 -a 仅列出正在运行的,像退出了的或者仅仅只是创建了的就不列出来
docker run -d -p 8800:80 --name nginx_xiao  nginx  #运行指定的镜像 
docker run -d --privileged=true -p 83:80 --name nginx83 nginx   #提升权限
#宿主主机端口:容器内部端口 
  -d  后台运行   
  -p 8800:80 是指定对外暴露的端口  容器内部用80 对应外部宿主主机的的8800  代理一样
  --name指定容器的名字  最后的nginx 代码要运行的镜像名字  有tag的加上tag 如 nginx:xxx  默认为latest
  • 修改镜像tag

运行命令:docker tag <source> <target>

docker tag <原来的 imgId或者REPOSITORY:tag> <新的 REPOSITORY:tag>

# 原来的镜像
[root@zhangsan ~]# docker images
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
zhangsan                       new                 d9f47ee54340        About an hour ago   304MB

# 修改名字
[root@zhangsan ~]# docker tag zhangsan:new zhangsan2:new2

# 更新后的
[root@zhangsan ~]# docker images
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
zhangsan2                      new2                d9f47ee54340        About an hour ago   304MB
zhangsan                       new                 d9f47ee54340        About an hour ago   304MB


  • 在运行的容器中运行命令

docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

# 在运行的容器中运行命令
[root@zhangsan ~]# docker exec -it 36f8eb73af76 /bin/bash
root@36f8eb73af76:/usr/local/tomcat# 
  • 删除一个镜像

运行命令:docker rmi <imgId或者REPOSITORY:tag>

[root@iZ2zeioqjurm6yjogwdwwaZ ~]# docker rmi zhangsan2:new2
Untagged: zhangsan2:new2

安装报错解决

curl -sSL https://get.docker.com/ | sh

我们是通过官方提供的一键脚本安装的

卡在了一个地方

warning: /var/cache/yum/x86_64/7/docker-ce-stable/packages/docker-ce-19.03.5-3.el7.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 621e9f35: NOKEY
Public key for docker-ce-19.03.5-3.el7.x86_64.rpm is not installed


GPG key retrieval failed: [Errno 12] Timeout on https://download.docker.com/linux/centos/gpg: (28, 'Operation timed out after 30000 milliseconds with 0 out of 0 bytes received')

# 超时

看前面的执行情况,官方脚本里 核心就是 下载 yum的docker源,利用yum来安装

查看 /etc/yum.repos.d下,发现了 docker-ce.repo

[root@LAPTOP yum.repos.d]# ls
CentOS-Base.repo      CentOS-Debuginfo.repo  CentOS-Sources.repo
CentOs-Base.repo.bak  CentOS-fasttrack.repo  CentOS-Vault.repo
CentOS-CR.repo        CentOS-Media.repo      docker-ce.repo
[root@LAPTOP-3C99HJ28 yum.repos.d]# yum install docker-ce.repo

我们接着执行命令,手动安装docker

 yum install docker-ce
 
 # 这一次就可以安装成功了

参考链接

https://www.jianshu.com/p/dcf61276efa9

https://www.jianshu.com/p/e6b946c79542

https://www.cnblogs.com/lsgxeva/p/8746644.html

https://www.runoob.com/docker/docker-image-usage.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值