当前位置: 首页 > news >正文

ubuntu20从docker安装到制作自己的镜像使用记录

ubuntu20从docker安装到制作自己的镜像使用记录

第一章:配置环境

1.ubuntu20
2.docker镜像18.04
3.参考:https://www.runoob.com/docker/docker-tutorial.html

第二章:安装docker

一、安装docker

参考1:Ubuntu安装docker并运行测试【docker默认最新,虚拟机版本22.04】
目前docker的几个大的国内镜像站都嘎了,需要配置镜像文件。镜像网址参考:docker镜像加速源配置,目前可用镜像源列举

1.安装教程

Step1:更新系统软件包
sudo apt updateStep2:安装依赖包【用于通过HTTPS来获取仓库】
sudo apt install apt-transport-https ca-certificates curl software-properties-commonStep3:添加Docker官方GPG密钥
sudo -i
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/trusted.gpg.d/docker-ce.gpgStep4:验证
sudo apt-key fingerprint 0EBFCD88
0EBFCD88 是公钥的指纹。执行这个命令后,系统会显示与该指纹相关的公钥信息。Step4:添加Docker阿里稳定版软件源
sudo add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"Step5:再次更新软件包
sudo apt updateStep6:安装默认最新版
sudo apt install docker-ce docker-ce-cli containerd.ioStep7:测试,安装好后默认启动
sudo docker run hello-world
如果输出“Hello from Docker!”则表示Docker已经成功安装。Step8:查看有哪些镜像
sudo docker imagesStep9:配置用户组
sudo usermod -aG docker galaxfy
su - galaxfy  # 刷新shell状态
docker images # 验证Step10:其他docker运行命令
查看状态:sudo systemctl status docker
启动:sudo systemctl start docker
开机自启:sudo systemctl enable docker
停止:sudo systemctl stop docker其他:安装特定版docker
sudo apt-cache madison docker-ce  # 显示可用版本
sudo apt-get install docker-ce=<VERSION_STRING> docker-ce-cli=<VERSION_STRING> containerd.io # 将需要的版本替换VERSION_STRING进行安装,例如 5:20.10.17~3-0~ubuntu-focal

2.镜像网址配置
在pull拉取镜像时,如果拉不到文件,可以尝试修改daemon.json文件
依次输入如下指令:

sudo nano /etc/docker/daemon.json

写入:

{"registry-mirrors": [ "https://docker.registry.cyou","https://hub.uuuadc.top","https://docker.anyhub.us.kg","https://dockerhub.jobcher.com","https://docker.ckyl.me","https://docker.awsl9527.cn","https://docker-cf.registry.cyou","https://dockercf.jsdelivr.fyi","https://docker.jsdelivr.fyi","https://dockertest.jsdelivr.fyi","https://mirror.aliyuncs.com","https://dockerproxy.com","https://mirror.baidubce.com","https://docker.m.daocloud.io","https://docker.nju.edu.cn","https://docker.mirrors.sjtug.sjtu.edu.cn","https://docker.mirrors.ustc.edu.cn","https://mirror.iscas.ac.cn","https://docker.rainbond.cc"]
}

使用的是 nano,可以通过按 Ctrl + O 保存文件,然后按 Enter 确认文件名,最后按 Ctrl + X 退出编辑器。
输入:

sudo systemctl daemon-reload  
sudo systemctl restart docker

二、验证

终端输入:

sudo docker run hello-world

出现:

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete 
Digest: sha256:63421b18c1443a9a85139225293fae7541fb40b7832d9deff80b6a9a75ce3604
Status: Downloaded newer image for hello-world:latestHello from Docker!
This message shows that your installation appears to be working correctly.To generate this message, Docker took the following steps:1. The Docker client contacted the Docker daemon.2. The Docker daemon pulled the "hello-world" image from the Docker Hub.(amd64)3. The Docker daemon created a new container from that image which runs theexecutable that produces the output you are currently reading.4. The Docker daemon streamed that output to the Docker client, which sent itto your terminal.To try something more ambitious, you can run an Ubuntu container with:$ docker run -it ubuntu bashShare images, automate workflows, and more with a free Docker ID:https://hub.docker.com/For more examples and ideas, visit:https://docs.docker.com/get-started/

表示安装成功。

第三章:docker的使用

一、hello world

开始学习的第一步,必然是“hello world”!
终端输入:

sudo docker run ubuntu:18.04 /bin/echo "Hello world"

会报错。
在这里插入图片描述
应该是权限问题,增加sudo即可。

sudo docker run ubuntu:18.04 /bin/echo "Hello world"

输出:
在这里插入图片描述

各个参数解析:

  1. docker: Docker 的二进制执行文件。
  2. run: 与前面的 docker 组合来运行一个容器。
  3. ubuntu:18.04 指定要运行的镜像,Docker 首先从本地主机上查找镜像是否存在,如果不存在,Docker 就会从镜像仓库 Docker Hub 下载公共镜像。
  4. /bin/echo “Hello world”: 在启动的容器里执行的命令

以上命令完整的意思可以解释为:Docker 以 ubuntu18.04镜像创建一个新容器,然后在容器里执行 bin/echo “Hello world”,然后输出结果。

二、运行交互式的容器

这句话有点拗口,个人理解就是和docker中的容器进行交互。
输入指令:

sudo su
sudo docker run -i -t ubuntu:18.04 /bin/bash

出现root@3ba0a1b6cfce:字样
在这里插入图片描述
表示目前已经进入了新建的ubuntu18.04的docker中了。
再输入指令:

cat /proc/version
ls

在这里插入图片描述
可以看到新建的容器中该文件夹下的目录。

输入指令退出该容器,回到本机环境。

exit

在这里插入图片描述

三、启动容器

创建一个以进程方式运行的容器

sudo docker run -d ubuntu:18.04 /bin/sh -c "while true; do echo hello world; sleep 1; done"

意思是创建一个容器,每秒刷一次hello world,出现
在这里插入图片描述
确认容器有在运行,可以通过 docker ps 来查看,输入:

sudo docker ps

输出
在这里插入图片描述
1.CONTAINER ID为容器的ID
2.IMAGE:容器的镜像
3.COMMAND:执行的命令
查看容器内的标准输出
输入

sudo docker logs ec3075c464ece03d

ec3075c464ece03d:为当前容器的ID,截取前面一部分就行。
在这里插入图片描述
停止当前容器
输入:

sudo docker stop ec3075c464ece03d

使用命令查看

sudo docker ps

在这里插入图片描述

第四章、构建镜像

这一节目标是在第三节拉取的ubuntu18.04中构建python环境,以及运行一个代码示例。

一、确认基础镜像

终端输入:

sudo docker images

在这里插入图片描述
后面将使用ubuntu18.04做为基础镜像,如果没有的话,可以使用如下指令进行pull:

sudo docker pull ubuntu:18.04

二、构建自己的镜像

1.dockerfile文件编写
为了构建一个带有python3的镜像,编写dockerfile文件如下:

# Dockerfile# Base images 基础镜像FROM ubuntu:18.04# MAINTAINER 维护者信息
maintainer chenjun_1241370589@qq.com
# 设置超时
ENV PIP_DEFAULT_TIMEOUT=100 #RUN 执行以下命令
RUN apt update
RUN apt install python3 python3-pip -y
RUN pip3 install --upgrade pip setuptools
RUN pip3 cache purge
RUN pip3 install opencv-python==4.5.5.62 -i https://pypi.tuna.tsinghua.edu.cn/simple
# 安装 OpenCV  
RUN apt-get update && apt-get install -y \   libgl1-mesa-glx  # 安装 OpenGL 库  
# 其他依赖和设置RUN pip3 install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple 
RUN apt install libgl1-mesa-glx
RUN mkdir -p /data/code/#拷贝文件至工作文件夹
COPY test.py /data/code/test.py#工作目录
WORKDIR /data/code/#容器启动时执行的命令
CMD ["python3","test.py"]

2.编写测试文件
在dockerfile同级文件夹下,编写test.py文件,test.py文件中写入:

import numpy as np
import cv2
print("Useing Opencv")
# 创建一个200x200的黑色图像,默认数据类型是uint8,范围是0-255
# 这里使用0作为颜色值,表示黑色
blank_image = np.zeros((200, 200, 3), dtype="uint8")print("blank_image",blank_image)

在这里插入图片描述
3.构建指令
在dockerfile文件夹下,开启终端,输入如下指令

sudo docker build -t cj-python3:v2.0 . -f Dockerfile

接下来docker将会按照dockerfile中的内容进行构建。
在这里插入图片描述

等待构建完成。

4.测试
输入指令:

sudo docker run -i -t cj-python3:v2.0

将会输出:

在这里插入图片描述
输入指令:

sudo docker run -i -t cj-python3:v2.0 /bin/bash

cd到工作文件夹下(data/code/),输入指令:

ls

可以查看到工作文件夹下的文件情况
在这里插入图片描述
输入“exit”即可退出当前镜像。
5.删除镜像
删除镜像指令:

docker rmi <镜像ID或名称>

强制删除镜像指令:

docker rmi -f <镜像ID或名称>

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • leetcode数论(2521. 数组乘积中的不同质因数数目)-质因素分解
  • 垃圾收集器
  • 【Linux】(32)详解命名管道 | 日志管理 | 进程池2.0
  • 基于python的百度迁徙迁入、迁出数据分析(七)
  • C# 报表功能
  • Nginx隐藏欢迎页Welcome to CentOS
  • 百日筑基第四十五天-从JAVA8走到JAVA9
  • Spring的代理模式
  • Omit<T, K> 解释
  • 【电子数据取证】支持最新版微信、企业微信、钉钉等重点应用数据提取分析!
  • 网络安全知识讲解
  • C语言典型例题30
  • Vue 3 中,组件间传值有多种方式
  • 【知识】pytorch中的pinned memory和pageable memory
  • Android Fragment:详解,结合真实开发场景Navigation
  • 9月CHINA-PUB-OPENDAY技术沙龙——IPHONE
  • chrome扩展demo1-小时钟
  • conda常用的命令
  • IDEA 插件开发入门教程
  • KMP算法及优化
  • Linux Process Manage
  • Rancher-k8s加速安装文档
  • 技术攻略】php设计模式(一):简介及创建型模式
  • 看图轻松理解数据结构与算法系列(基于数组的栈)
  • 腾讯大梁:DevOps最后一棒,有效构建海量运营的持续反馈能力
  • 通信类
  • 异步
  • 译米田引理
  • 用jquery写贪吃蛇
  • 中国人寿如何基于容器搭建金融PaaS云平台
  • LevelDB 入门 —— 全面了解 LevelDB 的功能特性
  • PostgreSQL之连接数修改
  • UI设计初学者应该如何入门?
  • 阿里云API、SDK和CLI应用实践方案
  • 浅谈sql中的in与not in,exists与not exists的区别
  • # centos7下FFmpeg环境部署记录
  • # 利刃出鞘_Tomcat 核心原理解析(七)
  • # 日期待t_最值得等的SUV奥迪Q9:空间比MPV还大,或搭4.0T,香
  • #《AI中文版》V3 第 1 章 概述
  • #162 (Div. 2)
  • #快捷键# 大学四年我常用的软件快捷键大全,教你成为电脑高手!!
  • #微信小程序(布局、渲染层基础知识)
  • (3)llvm ir转换过程
  • (BAT向)Java岗常问高频面试汇总:MyBatis 微服务 Spring 分布式 MySQL等(1)
  • (C语言)strcpy与strcpy详解,与模拟实现
  • (C语言)二分查找 超详细
  • (iPhone/iPad开发)在UIWebView中自定义菜单栏
  • (Java实习生)每日10道面试题打卡——JavaWeb篇
  • (创新)基于VMD-CNN-BiLSTM的电力负荷预测—代码+数据
  • (代码示例)使用setTimeout来延迟加载JS脚本文件
  • (二开)Flink 修改源码拓展 SQL 语法
  • (接口自动化)Python3操作MySQL数据库
  • (五)activiti-modeler 编辑器初步优化
  • (一一四)第九章编程练习
  • .net core 3.0 linux,.NET Core 3.0 的新增功能