Docker总结

Docker总结,第1张

Docker总结

目录
  • 背景
  • 一、添加下载源
  • 二、docker常用命令
    • 对容器
    • 对镜像
  • 三、镜像拉取与使用
      • ubuntu镜像的相关
      • Nginx镜像相关
      • Python镜像相关
  • 四、 镜像制作
  • 五、 数据卷挂载
  • 六、Dockerfile文件制作与使用
    • 实例
  • 参考文档

背景

Windows10下的Docker学习总结。

一、添加下载源

设置中的Docker Engine的json中添加

"registry-mirrors": [
   "https://registry.docker-cn.com",
   "http://hub-mirror.c.163.com",
   "https://3laho3y3.mirror.aliyuncs.com",
   "https://mirror.ccs.tencentyun.com",
   "http://f1361db2.m.daocloud.io"
 ]
二、docker常用命令 对容器
  1. 查看所有容器:docker ps -a
  2. 查看在运行的容器:docker ps
  3. 启动容器:docker start container_ID
  4. 进入容器: docker exec -it container_ID /bin/bash

    docker attach container_ID

  5. 停止容器:docker stop container_ID
对镜像
  1. 查看镜像:docker images

Docker 容器使用:https://www.runoob.com/docker/docker-container-usage.html
删除容器和镜像相关命令:https://www.cnblogs.com/111testing/p/9715887.html

三、镜像拉取与使用 ubuntu镜像的相关
  1. 拉取
    docker pull ubuntu:16.04
  2. 创建交互式的容器
    docker run -i -t ubuntu:15.10 /bin/bash
  3. ubuntu开发环境配置
    更新apt:apt-get update
    安装sudo:apt-get install sudo
    安装:sudo add-apt-repository ppa:jonathonf/python-3.6
    安装python3:sudo apt-get install python3.6
    安装pip3:sudo apt-get install python3-pip
    安装依赖库:sudo apt-get install build-essential python3-dev libssl-dev libffi-dev libxml2 libxml2-dev libxslt1-dev zlib1g-dev
    参考
    Python笔记——Ubuntu下安装pip3和Python的第三方库
    Ubuntu:彻底卸载 Python
  4. 进入container:
    docker start 容器名#启动容器
    docker exec -it 容器ID bash#进入容器
Nginx镜像相关
  1. 拉取
    docker pull nginx
  2. 指定端口启动Nginx
    docker run -d --name nginx01 -p 3344:80 nginx

    -d 后台运行
    -p 宿主机端口:容器内部端口,将容器80映射到主机3344端口,即主机通过3344端口即可访问容器内80端口
    –name 容器命名为nginx01

  3. 测试: curl localhost:3344
  4. 进入nginx:
    docker exec -it nginx01 /bin/bash
Python镜像相关
  1. 拉取
    docker pull python:3.6
  2. 创建交互式的容器
    docker run -i -t python:3.6 /bin/bash

    猜测:进入控制台,自动在linux环境下,即python镜像拉取时,也会拉取linux环境,因此镜像较大。

  3. 进入Python容器(方法同上)
四、 镜像制作
  1. 说明
    当对一个Docker容器配置完成后,通过docker commit即可将其打包为镜像文件,方便上传与使用。
  2. 代码语句
    docker commit -a “提交的镜像作者” -m “一些关于image的介绍” containerID new_imageName:Tag

    例子:
    docker commit -p -a “zhoulikun” -m “this is a test” ubuntu01 ubuntu_mine:16.04
    即,将ubuntu01容器打包为ubuntu_mine镜像,
    说明:本人的ubuntu01在基础ubuntu镜像(空白系统,只含基础命令)添加了sudo、python3.5、pip3、django

五、 数据卷挂载
	宿主机文件上传到Docker容器
  1. Docker cp命令方法
    docker cp 本地路径 容器长ID:容器路径

    参考:https://www.cnblogs.com/guohu/p/13125606.html

  2. 数据卷挂载法
    docker run -it -v 主机目录:容器目录

    例子:
    docker run --name ubuntu01 -it -v /C/Users/Administrator/Desktop/docker/shoot:/home ubuntu:16.04 /bin/bash
    新创建一个容器,并将容器home目录和主机shoot目录连接

    docker inspect container_ID

    查看container信息
    关于挂载的相关信息:

    挂载完成

六、Dockerfile文件制作与使用
  1. 创建Dockerfile文件(该文件无后缀)

    参考写入Dockerfile文件:
    FROM nginx
    RUN echo '这是一个本地构建的nginx镜像' > /usr/share/nginx/html/index.html

  2. Dockerfile运行
    控制台cd到Dockerfile文件下,输入docker build -t nginx:v3 .
    语法:docker build -t 新镜像的名字:TAG dockerFIle文件路径
    参考
实例
创建ubuntu镜像,要求配置python3.5,pip,Django.
  1. Dockerfile内容:

    #基础镜像
    FROM ubuntu:16.04
    #维护者信息
    MAINTAINER zhou 1597027672@email.com
    #镜像的 *** 作指令
    RUN apt-get -y update
    RUN apt-get install sudo && sudo apt-get -y install python3
    RUN sudo apt-get -y install python3-pip && pip3 install django==2.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
    #CMD [,]

    各关键字说明:参考

  2. 启动Dockerfile:
    cd到Dockerfile对应文件夹下;
    输入:docker build -t my_image_dockerfile:v1 .

  3. 导出镜像
    docker save -o my_image_dockerfile.tar my_image_dockerfile

参考文档

docker入门

未完成,持续更新

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5689339.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-17
下一篇2022-12-17

发表评论

登录后才能评论

评论列表(0条)

    保存