Centos7 Docker安装

在Centos7安装 Docker

1.手动安装
卸载旧版本

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

2.安装Docker Engine-Community
使用 Docker 仓库进行安装
在新主机上首次安装 Docker Engine-Community 之前,需要设置 Docker仓库。此后可以从仓库安装和更新 Docker
设置仓库
安装所需要的软件包。yum-utils 提供了 yum-config-manager,并且 device mapper 存储启动程序所需要 device-mapper-persistent-data 和 1vm2.

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

使用以下命令来设置稳定仓库

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

安装 Docker Engine-Community

  • 列出并且排序存储库中可用的版本
[root@localhost ~]# yum list docker-ce --showduplicates | sort -r
已加载插件:fastestmirror
已安装的软件包
可安装的软件包
 * updates: mirrors.aliyun.com
Loading mirror speeds from cached hostfile
 * extras: mirrors.aliyun.com
docker-ce.x86_64            3:20.10.9-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:20.10.9-3.el7                    @docker-ce-stable
docker-ce.x86_64            3:20.10.8-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:20.10.7-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:20.10.6-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:20.10.5-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:20.10.4-3.el7                    docker-ce-stable 
  • 通过其完整的软件包名称安装特定版本:
sudo yum install docker-ce-(version) docker-ce-cli-(version) containerd.io
#例如 docker-ce-20.10.9-3.el7
sudo yum install docker-ce-20.10.9-3.el7 docker-ce-cli-20.10.9-3.el7 containerd.io

启动 Docker

sudo systemctl start docker

通过运行hello-world 镜像来验证是否正确安装了 Docker Engine-Community。

sudo docker run hello-world
[root@localhost ~]# sudo docker run hello-world

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
删除安装包

yum remove docker-ce

删除镜像、容器、配置文件等内容

rm -rf /var/lib/docker

Docker 安装Nginx
·使用 docker search nginx 来查看可用版本
·使用 docker pull nginx:latest 拉取最新版 nginx 镜像
·使用 docker images 查看本地镜像

[root@localhost ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
nginx         latest    f2f70adc5d89   19 hours ago   142MB
hello-world   latest    feb5d9fea6a5   5 months ago   13.3kB
[root@localhost ~]# 

运行容器

  • docker run --name nginx-2022 -p 40:80 -d nginx
    参数说明:

  • –name nginx-2022: 指定容器名(自定义)

  • -p 40:80 : 端口进行映射,将本地的40 端口映射到 容器内部的80端口

  • -d nginx: 设置容器一直在后台运行

Docker 输出指定内容

[root@localhost ~]# docker run nginx /bin/echo "Hello Nginx"
Hello Nginx
  • docker : Docker 的二进制执行文件
  • run:与docker组合前俩运行一个容器
  • nginx 指定要运行的镜像,Docker 首先从本地主机上查找镜像是否存在,如果不存在,Docker 就会从镜像仓库下载公共镜像
  • /bin/echo “Hello Nginx”: 在启动的容器里执行的命令
  • 解释: Docker 以 nginx 镜像创建一个新的容器,然后在容器里面执行 /bin/echo “Hello Nginx”,然后输出结果

运行交互式的容器

docker run -i -t nginx /bin/bash
# -i 运行对容器内的标准输入进行交互
# -t 在新的容器内指定一个伪终端或终端(相当于一台虚拟机--可以执行命令)
# 例如: ls
ls -- 查看列表文件

启动容器(后台模式)
1.

# 用以下命令创建一个以进程方式运行的容器
docker run -d nginx /bin/bash -c "while true;do echo Hello Nginx;sleep 1;done"
# 输出结果:
bf9d90a14d0519ca23e5db8cdba671e5ba26f3dc7f60dec412ba3bffbf1907cc
这是容器ID,唯一

查看 docker 内的进程

docker ps
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES
bf9d90a14d05   nginx     "/docker-entrypoint.…"   5 seconds ago   Up 4 seconds   80/tcp    distracted_montalcini
#
#
CONTAINER ID: 容器 ID。

IMAGE: 使用的镜像。

COMMAND: 启动容器时运行的命令。

CREATED: 容器的创建时间。

STATUS: 容器状态。
状态有7种:
created(已创建)
restarting(重启中)
running 或 Up(运行中)
removing(迁移中)
paused(暂停)
exited(停止)
dead(死亡)
 
PORTS (TCP/UDP)
NAMES 自动分配的容器名称

在宿主机里面使用 docker logs (容器名--nginx,或者 容器ID--e6f995b0f47b )

[root@localhost ~]# docker logs e6f995b0f47b
\Hello Nginx
Hello Nginx
Hello Nginx
Hello Nginx
Hello Nginx
Hello Nginx

停止容器

docker stop (nginx/ID)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值