Centos7 docker安装实例

简介: 安装docker,常用命令

安装docker

yum install docker
  • 1
  • 1

配置国内镜像源(提速)

国内加速源:https://www.daocloud.io/mirror.html#accelerator-doc

curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://0fc8f023.m.daocloud.io
  • 1
  • 1

下载docker镜像

  • 搜索镜像
docker search centos
  • 1
  • 1
[root@cloud CentOS-YUM]# docker search centos
INDEX       NAME                                             DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/centos                                 The official build of CentOS.                   2987      [OK]       
docker.io   docker.io/jdeathe/centos-ssh                     CentOS-6 6.8 x86_64 / CentOS-7 7.3.1611 x8...   55                   [OK]
docker.io   docker.io/nimmis/java-centos                     This is docker images of CentOS 7 with dif...   20                   [OK]
docker.io   docker.io/consol/centos-xfce-vnc                 Centos container with "headless" VNC sessi...   18                   [OK]
docker.io   docker.io/million12/centos-supervisor            Base CentOS-7 with supervisord launcher, h...   12                   [OK]
docker.io   docker.io/torusware/speedus-centos               Always updated official CentOS docker imag...   8           
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 下载镜像
docker pull centos:6 #下载centos6
  • 1
  • 1
  • 查看镜像
docker images  #查看镜像
  • 1
  • 1

启动容器

  • 创建容器实例
[root@cloud ~]# docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
ssh-centos6                   latest              670e6db182f5        5 days ago          497.1 MB
docker.io/centos              latest              67591570dd29        3 weeks ago         191.8 MB
docker.io/centos              6                   8315978ceaaa        9 weeks ago         194.6 MB
[root@cloud ~]# docker run -i -t --name=centos10  ssh-centos6  /bin/bash     #用镜像ssh-centos6创建容器,设置名称为centos10
[root@e308c0493046 /]# 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

docker命令

# 查看运行中的容器
docker ps 

# 查看所有容器
docker ps -a

# 退出容器
按Ctrl+D 即可退出当前容器【但退出后会停止容器】

# 退出不停止容器:
组合键:Ctrl+P+Q

# 启动容器
docker start 容器名或ID

# 进入容器
docker attach 容器名或ID

# 停止容器
docker start 容器名或ID

# 删除全部容器--慎用
docker stop $(docker ps -q) & docker rm $(docker ps -aq)

#保存容器,生成镜像
docker commit 容器ID 镜像名称

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

docker配置

限制容器资源

# CPU使用0-3总共4个核心,内存1G ,--net=none不配置网络(桥接模式是pipework设置网络)
docker run -itd --net=none --name=centos07 --cpuset-cpus="0-3"  -m 1024M --memory-reservation 1024M centos6-132.97.8.7-170106_2 /bin/bash
  • 1
  • 2
  • 1
  • 2

更改存储目录

#复制docker存储目录
rsync -aXS /var/lib/docker/. /home/docker

#更改 docker 存储文件目录
ln -s  /home/docker  /var/lib/docker
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

设置存储大小

# ubuntu在/etc/default/docker
# centos在/etc/sysconfig/docker-storage
cat /etc/sysconfig/docker-storage

DOCKER_STORAGE_OPTIONS="--storage-opt dm.loopdatasize=2000G --storage-opt dm.loopmetadatasize=10G --storage-opt dm.fs=ext4 --storage-opt dm.basesize=20G"

#dm.loopdatasize=2000G是指存放数据的数据库空间为2t,默认是100g
#dm.loopmetadatasize=10G是存放Metadata数据空间为10g,默认是2g
#dm.fs=ext4是指容器磁盘分区为ext4
#dm.basesize=20G是指容器根分区默认为20g,默认是10g
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

docker桥接模式

linux桥接网络配置

[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-eth0 ifcfg-br0
[root@localhost network-scripts]# vi ifcfg-eth0

//改成这样
DEVICE=eth0
HWADDR=00:0C:29:06:A2:35
TYPE=Ethernet
UUID=34b706cc-aa46-4be3-91fc-d1f48c301f23
ONBOOT=yes
BRIDGE=br0
NM_CONTROLLED=yes
BOOTPROTO=static

[root@localhost network-scripts]# vi ifcfg-br0

//改成这样
DEVICE=br0
TYPE=Bridge
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.216.131
NETMASK=255.255.255.0
GATEWAY=192.168.216.2
DNS=8.8.8.8

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 重启网卡
service network restart
  • 1
  • 1

设置启动参数

# centos 在/etc/sysconfig/docker
# 其他操作系统请前往下面的网址
# https://docs.docker.com/installation/#installation
vi /etc/sysconfig/docker #在OPTIONS='--selinux-enabled'这行中修改为OPTIONS='--selinux-enabled -b=br0'即让docker服务启动时使用br0网卡进行桥接
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

安装pipework

git clone https://github.com/jpetazzo/pipework
cp ~/pipework/pipework /usr/local/bin/
  • 1
  • 2
  • 1
  • 2

启动手动设置网络的容器

docker run -itd --net=none --name=centos06 --cpuset-cpus="0-3"  -m 1024M --memory-reservation 1024M ssh-centos6 /bin/bash
  • 1
  • 1

使用pipework设置IP

pipework br0 centos06 132.97.8.6/24@132.97.8.1
  • 1
  • 1

进去docker查看IP

docker attach centos06
ifconfig
service sshd restart #重启ssh
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

docker开机启动脚本

vi docker_start.sh
  • 1
  • 1
#! /bin/bash
# chkconfig: 2345 10 90
# description: docker 

service docker start
docker start centos06
docker start centos07
pipework br0 centos06 132.97.8.6/24@132.97.8.1
pipework br0 centos07 132.97.8.7/24@132.97.8.1
docker exec -i  centos06 service sshd restart
docker exec -i  centos07 service sshd restart
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
1、将脚本移动到/etc/rc.d/init.d目录下
mv  /home/xy/docker_start.sh /etc/rc.d/init.d

2、增加脚本的可执行权限
chmod +x  /etc/rc.d/init.d/docker_start.sh

3、添加脚本到开机自动启动项目中
cd /etc/rc.d/init.d
chkconfig --add docker_start.sh
chkconfig docker_start.sh on
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

其他资料

# 5分钟弄懂Docker!
http://www.csdn.net/article/2014-07-02/2820497-what%27s-docker

# docker操作实例,图文教程
http://blog.csdn.net/wuzhilon88/article/details/41621285/
目录
相关文章
|
1月前
|
Ubuntu 虚拟化 Docker
Docker的安装
Docker的安装
50 0
|
1月前
|
Oracle 关系型数据库 数据库
如何利用 Docker 安装 Oracle 数据库
【2月更文挑战第14天】
89 0
|
6天前
|
时序数据库 Docker 容器
Docker安装InfluxDB
Docker安装InfluxDB
10 0
|
8天前
|
关系型数据库 MySQL 数据库
docker自定义安装mysql 5.7
docker自定义安装mysql 5.7
19 0
|
19天前
|
Linux Shell 开发工具
CentOS8中Docker安装及部署
CentOS8中Docker安装及部署
67 0
|
21天前
|
弹性计算 Serverless 数据库
ECS安装问题之docker安装如何解决
ECS(Elastic Compute Service,弹性计算服务)是云计算服务提供商提供的一种基础云服务,允许用户在云端获取和配置虚拟服务器。以下是ECS服务使用中的一些常见问题及其解答的合集:
|
27天前
|
关系型数据库 分布式数据库 PolarDB
PolarDB常见问题之安装gms时出现docker如何解决
PolarDB是阿里云推出的下一代关系型数据库,具有高性能、高可用性和弹性伸缩能力,适用于大规模数据处理场景。本汇总囊括了PolarDB使用中用户可能遭遇的一系列常见问题及解答,旨在为数据库管理员和开发者提供全面的问题指导,确保数据库平稳运行和优化使用体验。
|
29天前
|
JavaScript Java jenkins
如何利用CentOS7+docker+jenkins+gitee部署springboot+vue前后端项目(保姆教程)
如何利用CentOS7+docker+jenkins+gitee部署springboot+vue前后端项目(保姆教程)
76 0
|
29天前
|
Devops 开发工具 数据安全/隐私保护
Docker Swarm总结+CI/CD Devops、gitlab、sonarqube以及harbor的安装集成配置(3/5)
Docker Swarm总结+CI/CD Devops、gitlab、sonarqube以及harbor的安装集成配置(3/5)
43 0
|
29天前
|
NoSQL 关系型数据库 MySQL
Docker安装详细步骤及相关环境安装配置(mysql、jdk、redis、自己的私有仓库Gitlab 、C和C++环境以及Nginx服务代理)
Docker安装详细步骤及相关环境安装配置(mysql、jdk、redis、自己的私有仓库Gitlab 、C和C++环境以及Nginx服务代理)
183 0