【Docker】01 Docker安装与配置



一、Docker

Docker,基于容器技术的轻量级虚拟化解决方案。
Docker是容器引擎,把Linux的cgroup、namespace等容器底层技术进行封装抽象,为用户提供了创建和管理容器的便捷界面(包括命令行和API)。

概念:镜像Image,容器Container,仓库Repository:

  • Docker镜像,是一个静态模板,不能直接修改,可以通过封装生成
  • Docker容器,基于Docker镜像运行启动的应用或系统
  • Docker仓库,存放Docker镜像的地方,常见分为公开仓库(public)和私有仓库(private)两种形式

二、离线安装Docker

Docker官网离线安装文档:Install daemon and client binaries on Linux

安装步骤:
1)官网下载Docker安装包:Index of linux/static/stable/x86_64/ 选取相应版本,这边选了docker-20.10.9.tgz
2)解压到/usr/bin/

[root@localhost ~]# tar -xzvf docker-20.10.9.tgz 
docker/
docker/containerd-shim-runc-v2
docker/dockerd
docker/docker-proxy
docker/ctr
docker/docker
docker/runc
docker/containerd-shim
docker/docker-init
docker/containerd
[root@localhost ~]# ls
anaconda-ks.cfg  docker  docker-20.10.9.tgz
[root@localhost ~]# ls docker
containerd       containerd-shim-runc-v2  docker   docker-init   runc
containerd-shim  ctr                      dockerd  docker-proxy
[root@localhost ~]# mv docker/* /usr/bin/

3)启动服务

[root@localhost ~]# dockerd &
[1] 1673
[root@localhost ~]# INFO[2024-01-05T17:11:32.398698200+08:00] Starting up                                  
WARN[2024-01-05T17:11:32.403021500+08:00] could not change group /var/run/docker.sock to docker: group docker not found 
INFO[2024-01-05T17:11:32.406919900+08:00] libcontainerd: started new containerd process  pid=1678
INFO[2024-01-05T17:11:32.406979400+08:00] parsed scheme: "unix"                         module=grpc
INFO[2024-01-05T17:11:32.406991700+08:00] scheme "unix" not registered,fallback to default scheme  module=grpc
...
...

[root@localhost ~]# ps -ef | grep docker | grep -v grep
root      1673  1625  0 17:11 pts/0    00:00:00 dockerd
root      1678  1673  0 17:11 ?        00:00:00 containerd --config /var/run/docker/containerd/containerd.toml --log-level info

4)验证

[root@localhost ~]# docker info
Client:
 Context:    default
 Debug Mode: false

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 0
 Server Version: 20.10.9
 Storage Driver: overlay2
  Backing Filesystem: xfs
  Supports d_type: true
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 5b46e404f6b9f661a205e28d59c982d3634148f8
 runc version: v1.0.2-0-g52b36a2d
 init version: de40ad0
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 3.10.0-862.el7.x86_64
 Operating System: CentOS Linux 7 (Core)
 OSType: linux
 Architecture: x86_64
 CPUs: 1
 Total Memory: 509.6MiB
 Name: localhost.localdomain
 ID: VM36:UOEO:67ST:Y2EU:RVE6:TC37:SZ6W:UNND:PF22:CWT2:FWYG:XZMA
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false
 Product License: Community Engine

5)注册服务
kill掉前面手动启动的docker服务,
/usr/lib/systemd/system/目录下创建docker.service文件并添加内容:

[root@localhost ~]# vi /usr/lib/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
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
TimeoutStartSec=0
Delegate=yes
KillMode=process
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s

[Install]
WantedBy=multi-user.target

重新加载

[root@localhost ~]# systemctl daemon-reload

服务的相关操作:

[root@localhost ~]# systemctl start docker
[root@localhost ~]# systemctl stop docker
[root@localhost ~]# systemctl status docker
[root@localhost ~]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.

此时就可通过systemctl来启停Docker服务了。

三、联网安装Docker

3.1 下载YUM软件库文件

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

3.2 安装epel-release

yum install epel-release -y

3.3 安装yum-utils

在配置国内镜像仓库时,会遇到如下报错,此时就需要安装yum-utils

[root@server ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
-bash: yum-config-manager: command not found
yum install yum-utils -y

3.4 设置镜像仓库

设置镜像仓库,这边使用国内的阿里云镜像仓库:

[root@server ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Loaded plugins: fastestmirror
adding repo from: http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
grabbing file http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo

3.5 查看docker-ce所有版本

[root@server ~]# yum list docker-ce --show-duplicates
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: mirrors.nju.edu.cn
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
docker-ce-stable                                                           | 3.5 kB  00:00:00     
(1/2): docker-ce-stable/7/x86_64/updateinfo                                |   55 B  00:00:00     
(2/2): docker-ce-stable/7/x86_64/primary_db                                |  62 kB  00:00:00     
Available Packages
docker-ce.x86_64                     17.03.0.ce-1.el7.centos                      docker-ce-stable
docker-ce.x86_64                     17.03.1.ce-1.el7.centos                      docker-ce-stable
docker-ce.x86_64                     17.03.2.ce-1.el7.centos                      docker-ce-stable
docker-ce.x86_64                     17.03.3.ce-1.el7                             docker-ce-stable
docker-ce.x86_64                     17.06.0.ce-1.el7.centos                      docker-ce-stable
docker-ce.x86_64                     17.06.1.ce-1.el7.centos                      docker-ce-stable
docker-ce.x86_64                     17.06.2.ce-1.el7.centos                      docker-ce-stable

3.6 安装Docker

[root@server ~]# yum install docker-ce

3.7 启动Docker

[root@server ~]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@server ~]# systemctl start docker
[root@server ~]#

3.8 查看Docker信息

[root@server ~]# docker info
Client:
Context:    default
Debug Mode: false
Plugins:
  app: Docker App (Docker Inc.,v0.9.1-beta3)
  buildx: Build with BuildKit (Docker Inc.,v0.5.1-docker)
  scan: Docker Scan (Docker Inc.,v0.8.0)

Server:
Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
Images: 0
Server Version: 20.10.7
Storage Driver: overlay2
  Backing Filesystem: xfs
  Supports d_type: true
  Native Overlay Diff: true
  userxattr: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
...
# 安装完成后docker自动创建了docker组,为安全起见,将当前登录用户追加入到该组中
[root@server~ ]# id root
uid=0(root) gid=0(root) groups=0(root)
[root@server~ ]# usermod -aG docker $USER
[root@server~ ]# id root
uid=0(root) gid=0(root) groups=0(root),993(docker)

3.9 启动第一个容器

[root@server ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c1ec31eb5944: Pull complete 
Digest: sha256:ac69084025c660510933cca701f615283cdbb3aa0963188770b54c31c8962493
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/

四、一些配置

4.1 登录DockerHub

访问https://hub.docker.com/,先注册个账号,
之后在本地端登录:

[root@server ~]# docker login docker.io
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID,head over to https://hub.docker.com to create one.
Username: asdfv1929
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
[root@server ~]#

登录完成后,此时便可拉取Docker Hub上自己仓库里的镜像。

4.2 镜像加速器

[root@server ~]# vi /etc/docker/daemon.json
{
        "registry-mirrors": [
	        "http://hub-mirror.c.163.com","https://docker.mirrors.ustc.edu.cn"
	    ]
}

[root@server ~]# systemctl daemon-reload
[root@server ~]# systemctl restart docker

ustc的链接已失效


原文地址:https://blog.csdn.net/tianlong1929/article/details/136065390

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


最近一直在开发Apworks框架的案例代码,同时也在一起修复Apworks框架中的Bug和一些设计上的不足。遇到的一个普遍问题是,代码的调试过程需要依赖很多外部系统,比如MongoDB、PostgreSQL、RabbitMQ等。当然可以在本机逐一安装这些服务,然后对服务进行配置,使其满足自己开发调试
最近每天都在空闲时间努力编写Apworks框架的案例代码WeText。在文本发布和处理微服务中,我打算使用微软的SQL Server for Linux来做演示,于是也就在自己的docker-compose中加入了MS SQL Server的服务。其实在Docker中运行SQL Server是非常容
在《Kubernetes中分布式存储Rook-Ceph部署快速演练》文章中,我快速介绍了Kubernetes中分布式存储Rook-Ceph的部署过程,这里介绍如何在部署于Kubernetes的ASP.NET Core MVC的应用程序中使用Rook-Ceph所创建的存储对象。 构建ASP.NET C
最近在项目中有涉及到Kubernetes的分布式存储部分的内容,也抽空多了解了一些。项目主要基于Rook-Ceph运行,考虑到Rook-Ceph部署也不那么简单,官方文档的步骤起点也不算低,因此,在整合官方文档的某些步骤的基础上,写篇文章简单总结一下。 Rook-Ceph是Kubernetes中分布
CentOS下Docker与.netcore(一) 之 安装 CentOS下Docker与.netcore(二) 之 Dockerfile CentOS下Docker与.netcore(三)之 三剑客之一Docker-Compose CentOS下Docker与.netcore(四)之 三剑客之一D
CentOS下Docker与.netcore(一) 之 安装 CentOS下Docker与.netcore(二) 之 Dockerfile CentOS下Docker与.netcore(三)之 三剑客之一Docker-Compose CentOS下Docker与.netcore(四)之 三剑客之一D
构建镜像最具挑战性的一点是使镜像大小尽可能的小。Dockerfile中的每条指令都为图像添加了一个图层,您需要记住在移动到下一层之前清理任何不需要的工件。对于多阶段构建,您可以在Dockerfile中使用多个FROM语句。每个FROM指令可以使用不同的基础,并且每个指令都开始一个新的构建。您可以选择
本文介绍compose配置文件参数的使用,熟练编写compose文件 [root@docker lnmp]# cat lnmp.yaml version: '3' services: nginx: build: /root/docker_demo/nginx/ ports: - &q
环境 docker-machine主机:192.168.1.9 docker主机:192.168.1.10 步骤: 安装docker-machine 创建ssh密钥对,实现两主机无密登录 创建docker主机,命名host1 变更docker环境变量 运行容器查看两端是否同步 镜像容器同步测试成功
CentOS下Docker与.netcore(一) 之 安装 CentOS下Docker与.netcore(二) 之 Dockerfile CentOS下Docker与.netcore(三)之 三剑客之一Docker-Compose CentOS下Docker与.netcore(四)之 三剑客之一D
https://blog.csdn.net/wanglei_storage/article/details/77508620 实践中会发现,生产环境中使用单个 Docker 节点是远远不够的,搭建 Docker 集群势在必行。然而,面对 Kubernetes, Mesos 以及 Swarm 等众多容
1.引言 紧接上篇.NET Core容器化@Docker,这一节我们先来介绍如何使用Nginx来完成.NET Core应用的反向代理,然后再介绍多容器应用的部署问题。 2. Why Need Nginx .NET Core中默认的Web Server为Kestrel。 Kestrel is grea
docker rm `docker ps -a | grep Exited | awk '{print $1}'` 删除异常停止的docker容器 docker rmi -f `docker images | grep '<none>' | awk &#3
什么是Docker Compose 在微服务盛行的今天,我们通常是这么定义Compose的:对容器的统一启动和关闭的编排工具。 但是我以前还是有个疑惑,谁会用Compose在一台服务器上部署多个服务呢?干脆直接用单体服务就行了!直到我遇到了以下的一个需求,让我明白了在一台服务器上不得不用多个服务的时
CentOS下Docker与.netcore(一) 之 安装 CentOS下Docker与.netcore(二) 之 Dockerfile CentOS下Docker与.netcore(三)之 三剑客之一Docker-Compose CentOS下Docker与.netcore(四)之 三剑客之一D
很多时候,我们在本地开发过程中程序运行很正常,但是发布到线上之后由于环境的原因,可能会有一些异常。通常我们会通过日志来分析问题,除了日志还有一种常用的调试手段就是:附加进程。 VS中的附加进程非常强大,目前提供了9种常用的附加方式。 在当前.Net Core支持跨平台的大背景下,其中Linux环境和
https://www.cnblogs.com/bigberg/p/8867326.html 一、简介 Docker有个编排工具docker-compose,可以将组成某个应该的多个docker容器编排在一起,同时管理。同样在Swarm集群中,可以使用docker stack 将一组相关联的服务进行
.Net6中想实现对某个网址截屏,可通过Selenium模拟访问网址并实现截图。 实现 安装Nuget包 <PackageReference Include="Selenium.Chrome.WebDriver" Version="85.0.0" /&g
原文 https://www.cnblogs.com/gispathfinder/p/5871043.html 我们在使用docker run创建Docker容器时,可以用--net选项指定容器的网络模式,Docker有以下4种网络模式: host模式,使用--net=host指定。 co