Docker应用

安装docker:
1.安装docker依赖
yum install -y yum-utils device-mapper-persistent-data lvm2

2.设置yum源
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

3.查看docker版本
yum list docker-ce --showduplicates | sort -r

4.安装docker
yum install docker-ce-17.12.1.ce

5.启动docker,加入开机启动项
systemctl start docker
systemctl enable docker

6.查看docker版本
docker version

7.docker国内镜像加速,创建或修改/etc/docker/daemon.json 文件,修改为如下形式
{
"registry-mirrors": [
"https://registry.docker-cn.com",
"http://hub-mirror.c.163.com",
"https://docker.mirrors.ustc.edu.cn"
]
}
然后重新启动docker
sudo systemctl daemon-reload
sudo systemctl restart docker

 

安装docker-compose

8.通过pip安装docker-compose

安装python-pip,然后
yum -y install epel-release
yum -y install python-pip
pip install --upgrade pip
pip install docker-compose


9.如果pip安装docker-compose失败,则使用如下方法安装:
curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.5/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

 

 

docker常用命令


查看docker镜像
docker image

docker创建自定义网络,并指定网段:
docker network create --driver=bridge --subnet=172.18.0.0/24 bridge_internet

查看docker网络:
docker network ls/rm
docker network inspect bridge_internet

查看宿主机网络:ip a

查看正在运行的容器,不截断
docker ps -a --no-trunc

查看容器详细信息
docker inspect container_id

查看容器端口:
docker port container_id
宿主机查看端口及防火墙策略:
netstat -antlp |grep 6379
iptables -t nat -nL |grep 6379

进入容器
docker exec -it container_id /bin/bash

容器内部安装命令:
apt-get update
apt-get install -y telnet
apt-get install -y net-tools
apt-get install -y iputils-ping
apt-get install -y curl/vim

 

docker-compose.yml文件编写:

version: '3'
services:
  wad_redis:
    image: redis:latest
    ports:            ###将端口映射到宿主
      - "6379:6379"
    restart: always
    networks:      ####使用已创建的网络bridge_internet,并指定IP
      bridge_internet:
        ipv4_address: 172.18.0.11

networks:         ###此处要先声明已有的网络bridge_internet
 bridge_internet:
    external: true

 

上一篇:实验:记录一则删除GI的过程


下一篇:Linux-net-bridge网桥-br.c