CentOS下原生docker的安装与基本使用

系统版本:CentOS7

网络配置为桥接

虚拟化软件VMware

Docker-ce安装

关闭防火墙

[root@localhost ~]# systemctl stop firewalld

[root@localhost ~]# systemctl disable firewalld

Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.

Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

修改主机名

[root@localhost ~]# hostnamectl set-hostname docker01

[root@localhost ~]# bash

配置yum源

添加软件源信息(这里使用阿里云的源)

[root@docker01 ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

[root@docker01 ~]# cd /etc/yum.repos.d/

[root@docker01 yum.repos.d]# ls

CentOS-Base.repo  CentOS-Debuginfo.repo  CentOS-Media.repo    CentOS-Vault.repo

CentOS-CR.repo    CentOS-fasttrack.repo  CentOS-Sources.repo  docker-ce.repo

生成缓存(可以不做)

[root@docker01 yum.repos.d]# yum makecache fast

下载docker-ce

[root@docker01 yum.repos.d]# yum -y install docker-ce

启动docker,并设置为开机自动启动

[root@docker01 yum.repos.d]# systemctl start docker

[root@docker01 yum.repos.d]# systemctl enable docker

Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.

由于docker镜像仓库在国外,下载速度会很慢,所以替换为国内的

进入阿里云,搜索容器镜像服务,点击镜像工具,镜像加速器,会给你生成一个唯一的加速器地址,

[root@docker01 yum.repos.d]# mkdir -p /etc/docker

[root@docker01 yum.repos.d]# tee /etc/docker/daemon.json <<-'EOF'

> {

>   "registry-mirrors": ["https://zo590uqq.mirror.aliyuncs.com"]

> }

> EOF

{

  "registry-mirrors": ["https://zo590uqq.mirror.aliyuncs.com"]

}

[root@docker01 yum.repos.d]# systemctl daemon-reload

[root@docker01 yum.repos.d]# systemctl restart docker

运行第一个容器

拉取镜像(没有指定版本默认为latest

[root@docker01 yum.repos.d]# docker pull nginx

查看镜像

[root@docker01 yum.repos.d]# docker images

REPOSITORY   TAG       IMAGE ID       CREATED       SIZE

nginx        latest    605c77e624dd   3 weeks ago   141MB

创建一个容器

[root@docker01 yum.repos.d]# docker create --name web1 nginx

7dc823897732b0489235ae5ddd0da89ac687dd30f6c71b056de535010cd75b1a

查看容器

[root@docker01 yum.repos.d]# docker ps -a

运行容器

[root@docker01 yum.repos.d]# docker start id
删除容器

[root@docker01 yum.repos.d]# docker rm -f id

以上创建运行容器的步骤可以使用一条命令

[root@docker01 system]# docker run -d -p 8088:80 --name web03 httpd

-d    将容器放在后台运行

-p   指定端口映射

--name  指定容器名字

进入容器内部

 [root@docker01 system]# docker exec -i -t id /bin/bash

 

 容器的内核和cpu资源都是使用宿主机,但是它没有操作系统,所以它比虚拟机更轻量。一般会将配置好的文件拷贝到容器中进行运行,但是也可以进入容器进行操作

将主机的文件拷贝到容器的根目录

[root@docker01 ~]# docker cp /etc/passwd   id:/

进入到一个nginx的容器中,进行一个简单的网站编辑操作

root@a72082edb06a:/etc# cat /etc/issue
Debian GNU/Linux 11 \n \l
root@a72082edb06a:/etc# apt-get upgate
root@a72082edb06a:/etc# apt-get install vim
root@a72082edb06a:/etc# cd /usr/share/nginx/html/
root@a72082edb06a:/usr/share/nginx/html# ls
50x.html  index.html
root@a72082edb06a:/usr/share/nginx/html# echo hellow word > index.html 
root@a72082edb06a:/usr/share/nginx/html# 
在本地主机上访问nginx服务

 可以看到服务正常访问。

猜你喜欢

转载自blog.csdn.net/qq_53086187/article/details/122590513
今日推荐