docker学习笔记(一)

286 阅读2分钟

一、docker在线和离线部署

1.docker在线部署

1.查看系统配置是否支持docker

uname -r
#CentOS>3.10

2.更新yum版本,检查所需依赖,设置yum源

yum update
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

3.选择安装指定版本docker

yum list docker-ce --showduplicates | sort -r
yum install docker-ce-xx.xx.x.ce
systemctl start docker
systemctl enable docker
docker version

4.安装docker-compose

curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

2.离线部署docker

1.下载docker及docker-compose

#docker
https://download.docker.com/linux/static/stable/x86_64/
https://download.docker.com/linux/static/stable/x86_64/docker-18.06.3-ce.tgz
#docker-compose
https://github.com/docker/compose/releases
https://github.com/docker/compose/releases/download/1.29.1/docker-compose-Linux-x86_64

2.安装

#docker
tar -zxvf docker-xx.xx.x-ce.tgz
cp xxx /usr/bin/
cd /etc/systemd/system/
touch docker.service
vim docker.sevive
--------------------------------
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target
 
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd --selinux-enabled=false --insecure-registry=####YOURserviceIP
ExecReload=/bin/kill -s HUP $MAINPID
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
#TasksMax=infinity
TimeoutStartSec=0
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
# restart the docker process if it exits prematurely
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
 
[Install]
WantedBy=multi-user.target
 ------------------------------------------
 chmod 777 /etc/systemd/system/docker.service
 systemctl daemon-reload
 systemctl start docker
 systemctl enable docker.service
 systemctl status docker
 
#docker-compose
cd  /usr/local/bin
mv  docker-compose-Linux-x86_64  docker-compose
chmod +x docker-compose
docker-compose --version

3.docker配置仓库源

1.配置国内镜像源

vi /etc/docker/daemon.json
--------------------------
{"registry-mirrors": ["http://hub-mirror.c.163.com"]}
--------------------------
service docker restart

2.搭建本地仓库

#使用命令创建仓库dockr
docker run -d -p 8081:8080 atcol/docker-registry-ui
##8081: 指定端口。
##运行成功后使用 宿主机ip+端口 访问 url :
##https://github.com/atcol/docker-registry-ui

二、docker基础指令

1.运行docker相关

#从docker仓库获取镜像
docker pull {tag}
#使用镜像启动容器
docker run -itd --name {容器名称} {镜像IDor镜像名称} /bin/bash/
#查看运行中的容器
docker ps
#进入容器
docker exec -it {容器名称or容器ID} /bin/bash/
#启动已经停止的容器
docker start {容器ID}
#停止容器
docker stop {容器ID}

2.制作docker镜像

  • **REPOSITORY:**表示镜像的仓库源
  • **TAG:**镜像的标签
  • **IMAGE ID:**镜像ID
  • **CREATED:**镜像创建时间
  • **SIZE:**镜像大小
#将容器导出成镜像tar包
docker export {容器ID} > {tar包名}.tar
#将容器tar包导入成镜像
cat {tar包名}.tar | docker import - {repository}:{TAG}
#删除容器
docker rm -f {容器ID}

3.docker其他命令

# 
docker load < {}.tar
#