码迷,mamicode.com
首页 > 其他好文 > 详细

docker

时间:2018-02-21 22:21:02      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:listen   exit   sea   ror   dex   日志   tar   stc   内核   


docker 容器
基于LXC技术,容器引擎,内核虚拟化技术(linux)
iptables的坑:执行任何iptables命令都会启动iptables,灰度发布,平滑升级


yum install docker-io 安装docker
service docker start 启动docker

docker search centos 在官方镜像中搜索centos镜像
docker pull centos 下载docker官方centos镜像到本地

docker save centos >/opt/centos.tar.gz 将当前centos镜像打包到压缩包,方便无联网时复制到其它机器
docker load </opt/centos.tar.gz 从压缩包中加载centos镜像

docker images 查看当前已经下载的镜像
[root@iZ250o6h8zmZ nginx]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
oldboyedu/mynginx v3 fa66ca428374 13 minutes ago 401.3 MB
oldboyedu/mynginx v2 da6d0c97579b 14 hours ago 382 MB
oldboyedu/mynginx v1 c1f07d48c3ac 15 hours ago 382 MB
nginx latest 71326c378a50 6 days ago 107.5 MB
centos latest 72a210db1424 3 weeks ago 192.5 MB
docker rmi <IMAGE ID号> 删除已下载的镜像
docker ps -a 查看所有容器
docker run --name centos_v0.0.1 -t -i centos /bin/bash 运行一个名为centos_v0.0.1的容器,-t 伪终端,-i 标准输入保持打开状态,从centos镜像启用容器,在容器中运行一个bash进程

docker start <CONTAINER ID号> 启动指定容器
docker attach <CONTAINER ID号> 进入一个正在运行的容器

nsenter -t <容器PID号> -u -i -n -p -m 从NameSpace进程号进入容器
[root@iZ250o6h8zmZ nginx]# nsenter -h
Usage:
nsenter [options] <program> [args...]
Options:
-t, --target <pid> target process to get namespaces from
-m, --mount [=<file>] enter mount namespace
-u, --uts [=<file>] enter UTS namespace (hostname etc)
-i, --ipc [=<file>] enter System V IPC namespace
-n, --net [=<file>] enter network namespace
-p, --pid [=<file>] enter pid namespace
-r, --root [=<dir>] set the root directory
-w, --wd [=<dir>] set the working directory
-F, --no-fork do not fork before exec‘ing <program>
-h, --help display this help and exit
-V, --version output version information and exit

nsenter -t $(docker inspect -f "{{.State.Pid}}" <容器ID号>) -m -i -n -u -p

如何获取容器PID号:
docker inspect --format "{{.State.Pid}}" <容器ID号>
例如:
[root@iZ250o6h8zmZ nginx]# docker inspect -f "{{.State.Pid}}" 41c1bf5229d5
3589

docker run 进入容器,exit默认会停止容器,可以使用NameSpace方式进入容器,exit退出后容器仍然正常运行

#!/bin/bash
#ScriptName=ns.sh
PID=$(docker inspect --format "{{.State.Pid}}" $1)
nsenter -t $PID -m -u -i -n -p

运行脚本后跟上容器ID号:
./ns.sh 41c1bf5229d5

docker rm <容器ID号> 删除一个容器
docker rm -f <容器ID号> 删除一个正在运行的容器

docker run --rm -it centos /bin/echo "hehe" 运行完命令后自动删除容器

docker search nginx? 在官方镜像中搜索nginx镜像
docker pull nginx? 下载nginx镜像到本地


docker run -d -P nginx (从nginx镜像允许一个容器,名字随机,-d 放在后台运行,-P 随机端口映射) 32768-->80(tcp)
docker ps
docker ps -a
docker ps -a -l


docker logs <nginx容器ID号> 显示指定容器的日志信息

docker run -d -p 3391:80 nginx??(-p 指定端口映射)

docker inspect <容器ID号> 查看容器详细信息
docker inspect <容器ID号> |grep -i mounts

docker inspect -f "{{.Volumes}}" <容器ID号> 查看指定容器的数据卷位置,宿主机_data目录映射到容器的/data目录
map[/data:/var/lib/docker/volumes/32ab9ce6afdd7a2e7f4b8928dfd3383fffd586cdf4246abce84b4eeecfa0dd7c/_data]

数据卷如何映射(两种格式):
docker run -it --name volume_test1 -v /data centos
docker run -it --name volume_test2 -v /nginx:/nginx centos
docker run -it --name volume_test3 -v /nginx:/nginx:rw centos

逻辑卷容器如何映射:
docker run -d --name nfs -it -v /data centos 单独运行一个容器来当nfs:(逻辑卷容器)
docker run -d --name nfs_test1 -it --columes-from nfs centos 从nfs容器挂载逻辑卷

手动打包镜像:(已部好的容器-->镜像)

从镜像运行一个容器:
v0:
docker run --name mynginx -it centos
rpm -ivh https://mirrors.aliyun.com/epel/epel-release-latest-6.noarch.rpm
yum install nginx

将上一步中的容器克隆为本地镜像:
v1:
docker commit -m "my nginx" <容器ID号> oldboyedu/mynginx:v1
docker run -it --name nginxv1 oldboyedu/mynginx:v1
vim /etc/nginx/nginx.conf
4: daemon off;

再将v1版的容器克隆为v2版镜像:
v2:
docker commit -m "my nginx" <容器ID号> oldboyedu/mynginx:v2
docker images
docker run -d -p 3392:80 oldboyedu/mynginx:v2 nginx 后台运行容器,并执行启动nginx命令
docker ps -a 检查是否后台允许,3392:80(tcp)映射是否正常
netstat -tnlp
tcp 0 0 :::3393 :::* LISTEN 3524/docker-proxy

Dockerfile的编写:
FROM centos 基础镜像
MAINTAINER ihoney root@ihoney.net.cn 作者是谁
RUN rpm -ivh https://mirrors.aliyun.com/epel/epel-release-latest-6.noarch.rpm 安装epel源
RUN yum -y install nginx 安装nginx
ADD index.html /usr/share/nginx/html/index.html 添加文件
RUN sed -i ‘4i\daemon off;‘ /etc/nginx/nginx.conf 修改配置
EXPOSE 80 绑定80端口
CMD ["nginx"] 启动nginx

echo "docker file test" >> /opt/dockerfile/nginx/index.html

从Dockerfile建立一个新的镜像v3:(指定目录,index.html和Dockerfile放在同一个目录)
docker build -t oldboyedu/mynginx:v3 /opt/dockerfile/nginx/ 注意观察过程
docker run -d -p 3393:80 oldboyedu/mynginx:v3 映射3393端口到容器80端口


本地打开http://xx.xx.xx.xx:3393,显示是index.html内容就正常

 

docker cp 拷贝文件到容器

[root@iZ250o6h8zmZ ~]# docker cp /root/dig.sh f12d4f077804:/etc/nginx/

[root@iZ250o6h8zmZ ~]# docker exec -it f12d4f077804 "bin/bash"
root@f12d4f077804:/# cd /etc/nginx/
root@f12d4f077804:/etc/nginx# ls
conf.d dig.sh fastcgi_params koi-utf koi-win mime.types modules nginx.conf scgi_params uwsgi_params win-utf
root@f12d4f077804:/etc/nginx#

docker

标签:listen   exit   sea   ror   dex   日志   tar   stc   内核   

原文地址:https://www.cnblogs.com/i-honey/p/8457535.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!