Docker(2):安装和基本操作

本文详细介绍了在CentOS 7.4环境下安装Docker的过程,包括检查内核版本、删除旧版本、添加依赖及仓库、安装Docker并进行基本配置等步骤。同时,提供了常用的Docker操作命令,如查看、拉取、运行镜像等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、docker 安装

1.系统环境CentOS7.4

2.安装步骤

(1)查看内核是否支持docker

Docker 要求 CentOS 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的CentOS 版本是否支持 Docker 。

uname -r

3.10.0-693.11.1.el7.x86_64

(2)删除老版本

sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine

(3)添加相应依赖

sudo yum install -y yum-utils \
device-mapper-persistent-data \
lvm2

(4)添加稳定仓库

鉴于国内网络问题,强烈建议使用国内源,官方源请在注释中查看。
执行下面的命令添加 yum 软件源:

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

sudo sed -i 's/download.docker.com/mirrors.aliyun.com\/docker-ce/g' /etc/yum.repos.d/docker-ce.repo

# 官方源
# $ sudo yum-config-manager \
#     --add-repo \
#     https://download.docker.com/linux/centos/docker-ce.repo

上面最近安装不行,可以使用阿里的

使用官方源地址(比较慢)
$ sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo

可以选择国内的一些源地址:

阿里云
$ sudo yum-config-manager \
    --add-repo \
    http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

清华大学源
$ sudo yum-config-manager \
    --add-repo \
    https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo

(5)安装 Docker

-》安装docker ce【与下面安装命令,选择一个就可以了!】

sudo yum install docker-ce docker-ce-cli containerd.io

-》安装docker旧版本

yum -y install docker

(6)启动 Docker 后台服务

systemctl start docker

(7)查看docker版本(安装ce)

[root@localhost ~]# docker version
Client:
 Version:           18.09.6
 API version:       1.39
 Go version:        go1.10.8
 Git commit:        481bc77156
 Built:             Sat May  4 02:34:58 2019
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          18.09.6
  API version:      1.39 (minimum version 1.12)
  Go version:       go1.10.8
  Git commit:       481bc77
  Built:            Sat May  4 02:02:43 2019
  OS/Arch:          linux/amd64
  Experimental:     false

(8)开机启动

systemctl enable docker

(9)测试运行 hello-world,由于本地没有hello-world这个镜像,所以会下载一个hello-world的镜像,并在容器内运行。

docker run hello-world

运行结果

[root@localhost ~]# sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete 
Digest: sha256:0e11c388b664df8a27a901dce21eb89f11d8292f7fca1b3e3c4321bf7897bffe
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/

二、docker常用命令

1.查看本地镜像

docker images

2.拉取镜像

docker pull 镜像名称

3.查看运行镜像

docker ps

4.运行镜像

docker run [OPTIONS] IMAGE[:TAG][COMMAND][ARG…]
例如:

docker run hello-world

5.停止镜像

docker stop 镜像名称

6.删除本地镜像

(1)删除镜像

docker  rmi <image id>

(2)删除容器

docker rm <image id>

7.保存镜像
(1)查看要要保存的镜像的ID

docker images

(2)保存镜像

docker save imagename  -o  /opt/docker/hive.tar

8.加载镜像

指定保存镜像位置,加载保存镜像

docker load -i /opt/docker/hive.tar  


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值