Centos7 docker基础1-镜像

一、常用命令

安装

1
yum install -y docker

关闭服务与开启

1
2
systemctl stop docker.service 
systemctl start docker.service

开机启动:

1
systemctl enable docker.service

拉取ubuntu镜像

1
docker pull ubuntu

查看本地镜像

docker images 或者 docker image ls

1
2
REPOSITORY(仓库)          TAG(标签)           IMAGE ID(镜像ID)   CREATED(创建时间)    SIZE(大小)
docker.io/ubuntu latest 4c108a37151f 3 weeks ago 64.2 MB

运行镜像上的bash程序

1
docker run -t -i ubuntu /bin/bash

PS:输入exit 退出bash程序

查看本地运行的容器

1
2
3
4
5
docker ps -a
[root@centos7 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
(容器ID) (状态)
7b474e8ca411 ubuntu "/bin/bash" 33 minutes ago Exited (0) 32 minutes ago reverent_hamilton

删除容器:
格式:docker rm 容器ID

1
docker rm 7b474e8ca411

创建一个别名

1
2
3
4
5
docker image tag docker.io/ubuntu ruitest/ubuntu:v2 
[root@centos7 ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
ruitest/ubuntu v2 4c108a37151f (相同的ID) 3 weeks ago 64.2 MB
docker.io/ubuntu latest 4c108a37151f 3 weeks ago 64.2 MB

查看镜像的详细信息
格式:docker image inspect 镜像ID

1
docker image inspect 4c108a37151f

查找镜像:
格式:docker search 关键字

1
2
3
4
5
6
7
8
9
10
11
docker search mysql
[root@centos7 ~]# docker search mysql
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
(星级:表示受欢迎程序)) (是否官方创建) (是否自动创建))
docker.io docker.io/mysql MySQL is a widely used, open-source relati... 8388 [OK]
docker.io docker.io/mariadb MariaDB is a community-developed fork of M... 2884 [OK]
docker.io docker.io/mysql/mysql-server Optimized MySQL Server Docker images. Crea... 624 [OK]
docker.io docker.io/percona Percona Server is a fork of the MySQL rela... 438 [OK]
docker.io docker.io/centurylink/mysql Image containing mysql. Optimized to be li... 60 [OK]
docker.io docker.io/centos/mysql-57-centos7 MySQL 5.7 SQL database server 59
......略

删除tag

1
docker rmi ruitest/ubuntu:v2

注意:如果只有一个tag时,执行这条命令将会删除镜像

删除镜像
docker image rm 镜像ID

1
2
3
4
5
6
7
8
[root@centos7 ~]# docker image rm 4c108a37151f
Untagged: docker.io/ubuntu:latest
Untagged: docker.io/ubuntu@sha256:9b1702dcfe32c873a770a32cfd306dd7fc1c4fd134adfb783db68defc8894b3c
Deleted: sha256:4c108a37151f54439950335c409802e948883e00c93fdb751d206c9a9674c1f6
Deleted: sha256:7c1abf1dbbfd02a48330a7317ab45a6091d53e2e9cc062f0f3dbd2b7539947a6
Deleted: sha256:5a614dda4a54650168ee2cd30ce2e39576dad5c9a0d1907c02445687b4ea5090
Deleted: sha256:bd042113a73a5c9c6680990740446b7324afb39e243ade3d33bdaa9ffaf8d294
Deleted: sha256:ba9de9d8475e7f5e40086358a1353b3cc080994fc6d31e4272dd3acb69b0151e

二、创建镜像

  • 创建镜像三种方式:
  • 基于己有镜像的容器创建、
  • 基于本地模板导入
  • 基于Dockerfile创建

(1)、基于己有镜像的容器创建

1
2
3
4
docker pull ubuntu
docker images
docker run -ti ubuntu:latest /bin/bash
docker ps -a

使用commit命令提交为一个新的镜像:提交可以使用ID或名称

1
2
3
4
5
6
7
8
9
docker commit -m "Added a new file" -a "Docker Newbee" 0b2d5edef671 test
#-m: 注释
#-a:作者信息
# 0b2d5edef671 是容器的ID!不是镜像的ID
#test 是新镜像的名称
[root@centos7 ~]# docker images (这里的镜像ID不同)
REPOSITORY TAG IMAGE ID CREATED SIZE
test latest 8e861c8118a3 11 seconds ago 64.2 MB
docker.io/ubuntu latest 4c108a37151f 4 weeks ago 64.2 MB

(2)、基于本地模板导入

导入模板:

1
2
3
4
5
6
cat  ubuntu-14.04-x86_64-minimal.tar.gz  | docker import - ubuntu:14.04 
[root@centos7 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 14.04 40446eeefc1a 8 seconds ago 215 MB
test latest 8e861c8118a3 22 minutes ago 64.2 MB
docker.io/ubuntu latest 4c108a37151f 4 weeks ago 64.2 MB
  • B方式:存出和载入镜像

存出本地中ubuntu:14.04镜像文件为ubuntu_14.04.tar

1
2
3
docker save -o ubuntu_14.04.tar ubuntu:14.04
[root@centos7 ~]# ll -h ubuntu_14.04.tar
-rw------- 1 root root 216M 719 14:33 ubuntu_14.04.tar

估计这个大小与docker内看到是一样的。

载入镜像:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@centos7 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 14.04 40446eeefc1a 5 minutes ago 215 MB
test latest 8e861c8118a3 27 minutes ago 64.2 MB
docker.io/ubuntu latest 4c108a37151f 4 weeks ago 64.2 MB
[root@centos7 ~]# docker load < ubuntu_14.04.tar #试下在己有时,强行载入 会发生什么
Loaded image: ubuntu:14.04
[root@centos7 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 14.04 40446eeefc1a 5 minutes ago 215 MB #没有增加也没有改变
test latest 8e861c8118a3 27 minutes ago 64.2 MB
docker.io/ubuntu latest 4c108a37151f 4 weeks ago 64.2 MB
#删除原来的ubuntu:14.04
[root@centos7 ~]# docker image rm 40446eeefc1a
#再次载入:
[root@centos7 ~]# docker load < ubuntu_14.04.tar #当本地不存在时,会出现一个载入的“进度条”
fd58c1324b91: Loading layer [==================================================>] 225.8 MB/225.8 MB
Loaded image: ubuntu:14.04
[root@centos7 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 14.04 40446eeefc1a 7 minutes ago 215 MB #载入成功 镜像ID不变
test latest 8e861c8118a3 29 minutes ago 64.2 MB
docker.io/ubuntu latest 4c108a37151f 4 weeks ago 64.2 MB
[root@centos7 ~]#

(3)、基于Dockerfile创建

后面在补

三、上传镜像

https://hub.docker.com 注册一个帐号。

#估计可以自己搭建下docker hub的本地库(如同git一样)

在机器登录你的帐号

1
2
3
4
5
6
[root@centos7 ~]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: rdocker8
Password:
Login Succeeded
[root@centos7 ~]#

#PS:登录成功后机器会生成用户的配置文件:/root/.docker/config.json

push时,要新建以你的帐号的 tag

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@centos7 ~]# docker tag test:latest rdocker8/test:latest     #我的帐号名是:rdocker8
[root@centos7 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 14.04 40446eeefc1a 25 minutes ago 215 MB
rdocker8/test latest 8e861c8118a3 47 minutes ago 64.2 MB
test latest 8e861c8118a3 47 minutes ago 64.2 MB
docker.io/ubuntu latest 4c108a37151f 4 weeks ago 64.2 MB
[root@centos7 ~]# docker push rdocker8/test:latest
The push refers to a repository [docker.io/rdocker8/test]
f9e212164607: Pushed
75e70aa52609: Pushed
dda151859818: Pushed
fbd2732ad777: Pushed
ba9de9d8475e: Pushed
latest: digest: sha256:7fcb88d72723fc64ba3660d41cdced77c66a2d4060675e61db79af920f100ac3 size: 1359
[root@centos7 ~]#

查看自己的库:

docker-1-1.png

教程参考于:http://www.bdkyr.com/

# docker
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×