如何创建Ubuntu docker基本镜像

时间:2019-08-20 17:58:13  来源:igfitidea点击:

此方法适用于创建所有Ubuntu版本的docker base镜像。你可以从这里找到Ubuntu的发行代号.
https://wiki.ubuntu.com/Releases

准备工作

  1. Debootstrap:我们将使用Debootstrap脚本来创建docker base镜像。
  2. Ubuntu发布代码名称:要创建docker镜像,我们需要“Ubuntu Release Code Name”,例如“Bionic Beaver”或者“Xenial Xerus”。

关于我们的系统

我们使用的是Ubuntu16.04LTS桌面来创建docker base映像,但对操作系统没有限制。你也可以从其他Linux系统创建ubuntu的docker镜像(有debootstrap脚本就行)。

创建Ubuntu docker base镜像的步骤

下面所有命令都是debian/ubuntu系统中的。

以root用户登录

root@theitroad# sudo su

为docker 镜像工作区创建目录

root@theitroad# mkdir -p /opt/docker_base_images

root@theitroad# cd /opt/docker_base_images

安装debootstrap

root@theitroad:/opt/docker_base_images# apt install debootstrap
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
debootstrap
0 upgraded, 1 newly installed, 0 to remove and 35 not upgraded.
Need to get 37.2 kB of archives.
After this operation, 262 kB of additional disk space will be used.
Get:1 http://in.archive.ubuntu.com/ubuntu xenial-updates/main amd64 debootstrap all 1.0.78+nmu1ubuntu1.6 [37.2 kB]
Fetched 37.2 kB in 0s (60.1 kB/s)      
Selecting previously unselected package debootstrap.
(Reading database ... 380627 files and directories currently installed.)
Preparing to unpack .../debootstrap_1.0.78+nmu1ubuntu1.6_all.deb ...
Unpacking debootstrap (1.0.78+nmu1ubuntu1.6) ...
Processing triggers for man-db (2.7.5-1) ...
Setting up debootstrap (1.0.78+nmu1ubuntu1.6) ...
root@theitroad:/opt/docker_base_images#

运行debootstrap

我们将创建Ubuntu18.04 LTS docker基本映像。
所以Ubuntu发行版的代码名“Bionic”。

root@theitroad:/opt/docker_base_images# debootstrap bionic bionic > /dev/null

root@theitroad:/opt/docker_base_images# 
root@theitroad:/opt/docker_base_images# ls -l
total 4
drwxr-xr-x 21 root root 4096 Oct  8 23:22 bionic
root@theitroad:/opt/docker_base_images#

查看bionic目录的内容

root@theitroad:/opt/docker_base_images# ls bionic/
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@theitroad:/opt/docker_base_images# cat bionic/
bin/   dev/   home/  lib64/ mnt/   proc/  run/   srv/   tmp/   var/   
boot/  etc/   lib/   media/ opt/   root/  sbin/  sys/   usr/   
root@theitroad:/opt/docker_base_images# cat bionic/etc/l
ld.so.cache     ld.so.conf.d/   libaudit.conf   locale.gen      logcheck/       logrotate.conf  lsb-release
ld.so.conf      legal           locale.alias    localtime       login.defs      logrotate.d/    
root@theitroad:/opt/docker_base_images# cat bionic/etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=18.04
DISTRIB_CODENAME=bionic
DISTRIB_DESCRIPTION="Ubuntu 18.04 LTS"
root@theitroad:/opt/docker_base_images#

导入Docker

现在在本地系统导入docker镜像。

root@theitroad:/opt/docker_base_images# sudo tar -C bionic -c . | docker import - bionic
sudo: unable to resolve host theitroad
sha256:58660251bce0fc8350e03f2ea85cbdc09e2023f3fc6cde21c2a6f8b3ee039963
root@theitroad:/opt/docker_base_images#

列出docker 镜像

root@theitroad:/opt/docker_base_images# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
bionic              latest              474342d581ce        9 seconds ago       289MB
root@theitroad:/opt/docker_base_images#

验证新的docker映像

root@theitroad:/opt/docker_base_images# docker run bionic cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=18.04
DISTRIB_CODENAME=bionic
DISTRIB_DESCRIPTION="Ubuntu 18.04 LTS"
root@theitroad:/opt/docker_base_images#

在docker hub分享你的镜像

  1. 登录docker hub,注册一个账号

  2. 使用命令登录docker hub。

docker login
  1. 将镜像推送到Docker Hub

推送前先将镜像标记。

docker tag bionic theitroad/bionic:latest

docker push theitroad/bionic:latest
  1. 在Docker Hub中查看新镜像。

使用浏览器登录到你的Docker Hub主页。将能看到新创建的镜像。