云原生应用2:docker在线/离线安装与基础用法

本篇博客能让你了解到docker的安装与基础语法

1.docker安装与基础用法

依赖环境:Linux kernel 3.10+
下面使用 Centos7.6演示:查看内核与系统版本:uname -a ,cat /etc/centos-release
在这里插入图片描述

2.安装docker
  • 1.在阿里云开源镜像仓库找到docker-ce库,https://mirrors.aliyun.com/docker-ce/linux/centos/ ,找到 docker-ce.repo ,并右键复制链接地址,在centos上 cd /etc/yum.repos.d/ , wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
  • 2.执行yum repolist,再安装docker-ce,yum install docker-ce,中间步骤输入 y 同意
    如果出现 container-selinux >= 2.9问题无法安装,请先下载 container-selinux
    下载:wget http://mirror.centos.org/centos/7/extras/x86_64/Packages/container-selinux-2.107-3.el7.noarch.rpm
    安装:rpm -ivh container-selinux-2.107-3.el7.noarch.rpm
  • 3.设置docker镜像加速器,dockerhub在国外,因此docker镜像的下载很慢,需要设置加速地址,有很多第三方镜像加速地址,如:阿里云镜像加速器、163、docker-cn等,设置加速器:1.mkdir -p /etc/docker , 2.vi /etc/docker/daemon.json , 3.输入 {“registry-mirrors” : [“https://xxx.mirror.aliyuncs.com”,“https://registry.docker-cn.com”]} 里面是数组,可以输入多个
  • 4.启动docker,systemctl start docker.service ,开机启动,systemctl enable docker.service
  • 5.查看docker版本,docker version , docker info,可看到运行中的容器,docker 版本,以及刚刚设置的镜像加速器地址,可看到机器上有一个已停止的容器
    在这里插入图片描述
3.docker基础用法

帮助文档可让你快速了解它,输入docker,或 docker image --help 查看帮助文档
安装一个nginx来快速了解docker的使用,以 nginx 为例进行说明:

  • 1.查询镜像,有2种方式,一是使用 docker search nginx 命令查询,二是在https://hub.docker.com/ 页面上输入nginx查询
  • 2.下载镜像:docker pull nginx:latest,或指定标签版本 tag
  • 3.查看镜像列表:docker image ls
  • 4.启动容器:docker run -it -d --name nginx1 -p 8080:80 nginx:latest,运行一个有交互窗口的容器,名称叫nginx1,且容器的80端口映射宿主机的8080端口,启动后可通过 http://宿主机IP:8080 访问nginx
  • 5.查看容器:docker ps ,docker ps -a(可查看已停止的容器)
  • 6.停止容器:docker stop nginx1
  • 7.删除容器:docker rm nginx1,再通过docker ps -a 就看不到停止的容器了
  • 8.查看容器详细信息:docker inspect nginx1
  • 9.查看容器中日志信息:docker container logs nginx1

上面命令截图
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
访问刚刚运行的 nginx 容器
在这里插入图片描述
docker运行状态图:
在这里插入图片描述

阿里云镜像加速器,需要先注册,然后在 容器镜像服务–镜像加速器中查看,如下图:
在这里插入图片描述

下一篇:云原生应用3:docker镜像管理

配置一个 jenkins 容器:vi start_jenkins.sh,内容如下:

docker run -d --name jenkins -u root -it --restart=always
-p 7080:8080 -p 50000:50000
-p 8190:8190
-p 8191:8191
–privileged=true
-v /var/jenkins_home:/var/jenkins_home
-v /home:/home
-v /usr/bin/docker:/usr/bin/docker
-v /var/run/docker.sock:/var/run/docker.sock
-v /usr/lib64/libltdl.so.7:/usr/lib/x86_64-linux-gnu/libltdl.so.7
-v /etc/localtime:/etc/localtime -v /etc/timezone:/etc/timezone
jenkinsci/blueocean

-v /etc/localtime:/etc/localtime -v /etc/timezone:/etc/timezone 这行特别重要,表示容器的时间使用宿主机的时间,否则你应用内容的时间会少8个小时

docker也可以离线安装
离线包下载地址tg:https://download.docker.com/linux/static/stable/x86_64/
离线包下载地址rpm:https://download.docker.com/linux/centos/7/x86_64/stable/Packages/

上传文件,解压

[root@dev1 opt]# tar -xvf docker-18.06.1-ce.tgz
docker/
docker/docker-containerd
docker/docker-proxy
docker/docker
docker/docker-runc
docker/dockerd
docker/docker-containerd-ctr
docker/docker-containerd-shim
docker/docker-init

复制 [root@dev1 opt] cp docker/* /usr/bin/
设置开机自启动,vim /etc/systemd/system/docker.service ,内容如下:

[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target

[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
#TasksMax=infinity
TimeoutStartSec=0
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
# restart the docker process if it exits prematurely
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s

[Install]
WantedBy=multi-user.target

添加文件权限并启动docker:chmod +x /etc/systemd/system/docker.service
重载unit配置文件:systemctl daemon-reload
启动Docker(2种方式): systemctl start docker / dockerd &
设置开机自启:systemctl enable docker.service

测试
[root@centos75-1 ~]# docker -v

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章