docker入门笔记

1 docker安装

docker 加速器 安装(ubuntu)

step 1: 安装必要的一些系统工具
sudo apt-get update
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
step 2: 安装GPG证书
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
Step 3: 写入软件源信息
sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
Step 4: 更新并安装 Docker-CE
sudo apt-get -y update
sudo apt-get -y install docker-ce
#自建加速文件
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://j6o4qczl.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

docker 加速器 安装(Centos7)

安装加速器方法:

step 1: 安装必要的一些系统工具
 yum install -y yum-utils device-mapper-persistent-data lvm2
Step 2: 添加软件源信息
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Step 3: 更新并安装 Docker-CE
yum makecache fast
yum -y install docker-ce
Step 4: 开启Docker服务
systemctl restart docker
systemctl enable docker

#自建加速文件

yum install -y yum-utils device-mapper-persistent-data lvm2 && yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo && yum makecache fast && yum -y install docker-ce 
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://j6o4qczl.mirror.aliyuncs.com"]
}
EOF
systemctl daemon-reload
systemctl restart docker

2 docker 基本命令

2.1 #下载一个centos 镜像
docker pull  centos  
2.2 #删除一个centos 镜像
docker rmi  centos  
2.3 #删除一个正在运行的容器
docker rm -f 容器id 
2.4 #查看本地docker镜像列表
docker images 
2.5 #保存网上镜像
docker save centos  > /iso/xxx.tar.gz  
docker save -o centos.tar centos/centsos
2.6 #使用本地保存镜像
docker load  < /iso/xxx.tar.gz  
2.7 启动容器 并执行命令(命令执行成功后背退出)
docker run centos /bin/echo "hehe" 
2.8 查看docker 容器进程

(可查看 状态,退出或者正在运行的进程,不加-a 只显示正在运行的容器)

docker ps -a  
docker run  -t 打开终端 -i 保持终端打开状态 centos 镜像  -name 名字 /bin/bash 执行命令

使用 CTRL+P+Q 退出当前容器内部回话,就会后台运行了。

(启动容器)
docker start 容器id

2.9 进入容器 多终端 操作同步
docker attach 容器id  

容器理念是单进程, 如 执行一个/bin/bash 当退出/bin/bash之后 相当于退出 bash环境 自然就结束掉了。

2.10 可以访问另一个进程的 名称空间 namespace
nsenter   (安装包util-linux) 


docker inspect --format "{{.State.Pid}}" 容器id  # 获取容器pid
nsenter -t 19233 -u -i -p -n  #  同构nsenter --help  查看参数(-u uts 用户空间 -i ipc 硬件通信  -n 网络 -p pid)
2.11 ###容器持久运行
docker exec -it -d 0b8c68efd69容器id /bin/bash    #-d后台
2.12 容器退出继续运行
ctrl +p +q  

或者想尽一切办法 让shell 一直运行 如 tty:true 或 ./xx.sh && tailf /dev/null

2.13 #删除一个 执行完命令的容器。。用于测试
docker run --rm  容器名称  执行的命令  
docker run --rm centos /bin/echo "hehe"

#变量内容是 只列出所有pid kill 删除掉

 docker kill  $(docker ps -a -q)  
2.14 #docker 后台运行容器,并映射一个随机端口

-d 后台运行 -P 映射随机端口 -p 映射指定端口

[root@localhost ~]# docker run -d -P nginx
4aea45e8c64fd36b0c4d094527db8f9db1dcb57e645878b161ada87af00d833a
[root@localhost ~]# docker run -d -p 81:80 nginx
da15c84223d4d483b8ec25e6e1e8bba7cf14bcadfa2f0489699ffb183335856f
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                   NAMES
da15c84223d4        nginx               "nginx -g 'daemon of…"   4 seconds ago       Up 2 seconds        0.0.0.0:81->80/tcp      kind_joliot
4aea45e8c64f        nginx               "nginx -g 'daemon of…"   17 minutes ago      Up 17 minutes       0.0.0.0:32768->80/tcp   gifted_hamilton

3 docker 容器数据管理

3.1 挂载本地磁盘到容器

#启动一个名字为volume-test1的centos容器 并且挂载硬盘到/data中

docker run -it --name volume-test1 -v /data centos

#在/var/lib/docker/volumes/下可以查到到挂载的硬盘

[root@localhost volumes]# pwd
/var/lib/docker/volumes

#通过docker inspect 容器id 可以查看到容器挂载信息 挂载到哪个硬盘上

#docker inspect 3095e85d9856 “Mounts”: [
{
“Type”: “volume”,
“Name”: “52fac9c68987f0af5bff7c9e6f0e46db732c7e0d095ac1cad16a40b22dda8094”,
“Source”: “/var/lib/docker/volumes/52fac9c68987f0af5bff7c9e6f0e46db732c7e0d095ac1cad16a40b22dda8094/_data”,
“Destination”: “/data”,
“Driver”: “local”,
“Mode”: “”,
“RW”: true,
“Propagation”: “”
}

#通过挂载信息,往本地磁盘里写入文件,容器也会同步更新文件

[root@localhost _data]# touch 123
[root@localhost _data]# pwd
/var/lib/docker/volumes/52fac9c68987f0af5bff7c9e6f0e46db732c7e0d095ac1cad16a40b22dda8094/_data
#指定本地路径挂载
[root@localhost _data]#  docker run -it -v /root:/opt centos
[root@fb7d22214ff9 /]# cd /opt 
[root@fb7d22214ff9 opt]# ll
total 8
-rw-------. 1 root root 1379 Mar 20 06:42 anaconda-ks.cfg
-rwxr-xr-x  1 root root   95 Mar 20 08:34 ns.sh
#也可以挂载单个文件或指定权限rw ro 
docker run -it  -v /root/.bash_profile:/root/.bash_profile:ro
3.2 从其他容器挂载磁盘

#先挂载一个本地磁盘到nfs容器中

docker run -d --name nfs -v /data centos

#再打开一个容器挂载nfs的磁盘 也就是/data

docker run -it --name test2 --volumes-from nfs  centos

#查看结果

[root@43fecb105976 /]# cd /data
[root@43fecb105976 data]# ll
total 0
-rw-r--r-- 1 root root 0 Mar 20 11:02 hello

4 手动构建docker 镜像容器

4.1 生成一个centos 容器 并安装nginx
docker run -it --name mynginx centos
yum install -y nginx #需要内部下载epel 源
4.2 上传镜像
 docker commit -m "my nginx"  dd2c7525d97b qinshuo/mynginx:v1

#-m 写备注
备注后面写之前容器的id
#后面是用户名(git)和版本信息
#检查

[root@localhost _data]# docker images |grep mynginx
\qinshuo/mynginx     vi                  97519b087efa        48 seconds ago      385MB
4.3 启动容器并更改配置
docker run -it --name mynginx qinshuo/mynginx:vi

修改nginx 配置文件

vi /etc/nginx/nginx.conf

最前面加入 daemon off; #将nginx 放到前台运行 而非后台 保持住 防止docker 容器停止

4.4 修改后重新建立镜像
docker commit -m "my nginx 1" 6f2b0985c80d qinshuo/mynginx:v2
4.5 然后开启对外nat 端口 并查看是否持久化运行
docker images 
docker run -d -p 82:80 qinshuo/mynginx:v2 nginx 

-d 后台运行 -p 指定端口 最后的nginx 是执行nginx启动命令 由于是yum 安装有环境变量,如非yum安装需要些绝对路径

ss -lntup|column -c1

5 使用dockerfile 构建容器镜像

5.1 如何编写Dockerfile

docker file 分为四个部分
#基础镜像信息
#维护者信息
#镜像操作指令
#容器启动时执行的命令
实例1-1 创建一个dockerfile

[root@localhost ~]# vim Dockfile
#This is dockerfile
#Version 1
#Author: QinShuo
#Base image
FROM centos

#维护者信息
MAINTAINER Qinshuo qinshuo@csii.com.cn

#Commands 命令
RUN rpm -ivh https://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
RUN yum install -y nginx


#添加文件(zip文件会自动解压)
ADD index.html /usr/share/nginx/html/index.html

#将nginx 放到前台运行 而非后台
RUN echo "daemon off;"   >> /etc/nginx/nginx.conf

#增加对外端口
EXPOSE 80
#最后执行什么命令
CMD ["nginx"]
5.2 构建镜像

-t 打标签
最后有个. 是镜像放置位置

docker build -t qinshuo/mynginx:v3 .
5.3 启动镜像容器
[root@localhost ~]# docker run -d -p 83:80 qinshuo/mynginx:v3
0e8e834dde3b108cf0b0b36885785ff39937053b7299bdbbd9b55a3a3dd08b83

6 构建registry 私有仓库

6.1 下载并运行registry
docker pull registry
docker run  -d -p 5000:5000 registry
6.2 测试是否运行
  1. 查看本地端口是否打开
  2. 使用命令
[root@localhost ~]# curl http://10.0.16:5000/v2/_catalog
{"repositories":[]}
6.3 重新给个镜像打标签
docker tag nginx 10.0.16:5000/mynginx
6.4 把新的镜像放入私有仓库

#由于新版本无法直接push 需要改配置文件 因为默认http访问在互联网不安全,所以强制要求https 访问。
#本地安装nginx 模拟https

yum install -y nginx

我的博客即将同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=1y540reukj288

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

范一刀

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值