前言
本篇只是自己学习笔记,工作需要属于速成学习,因此较为简单。宿主机Ubuntu18.04和centos7混用,可能导致部分系统命令有出入,但是docker相关的命令无影响。docker info version logs等命令不再列出。目前为一些基础操作,可能后续还会增加,可能也会鸽了。有错误或者疑问欢迎提出讨论。
首先docker内有三个名词需要了解
- 镜像(Image):root 文件系统。
- 容器(Container):镜像和容器的关系,从面向对象中的类和实例来理解,镜像是类,容器是镜像运行时的实体,也就是对象。(可以new一个对象)
- 仓库(Repository):代码控制中心,保存镜像。
镜像操作
换源
https://cr.console.aliyun.com/
首页点击“创建我的容器镜像” 得到一个专属的镜像加速地址
Docker中国区官方镜像
https://registry.docker-cn.com
网易
http://hub-mirror.c.163.com
root@ubuntu:/home/shawn# vi /etc/docker/daemon.json
root@ubuntu:/home/shawn# cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://o1260yxa.mirror.aliyuncs.com"]
}
root@ubuntu:/home/shawn# service docker restart
root@ubuntu:/home/shawn# docker info
...
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Registry Mirrors:
https://o1260yxa.mirror.aliyuncs.com/
Live Restore Enabled: false
...
搜索镜像
1、docker hub
2、docker search
使用docker search xxx[:tag]命令,也可以带tag搜索
[root@localhost /]# docker search ubuntu
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
ubuntu Ubuntu is a Debian-based Linux operating sys… 12321 [OK]
dorowu/ubuntu-desktop-lxde-vnc Docker image to provide HTML5 VNC interface … 538 [OK]
websphere-liberty WebSphere Liberty multi-architecture images … 273 [OK]
rastasheep/ubuntu-sshd Dockerized SSH service, built on top of offi… 253 [OK]
consol/ubuntu-xfce-vnc Ubuntu container with "headless" VNC session… 240 [OK]
ubuntu-upstart Upstart is an event-based replacement for th… 110 [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 50 [OK]
open-liberty Open Liberty multi-architecture images based… 46 [OK]
ubuntu-debootstrap debootstrap --variant=minbase --components=m… 44 [OK]
i386/ubuntu Ubuntu is a Debian-based Linux operating sys… 25
solita/ubuntu-systemd Ubuntu + systemd 24 [OK]
1and1internet/ubuntu-16-apache-php-5.6 ubuntu-16-apache-php-5.6 14 [OK]
拉取镜像
docker pull xxx:tag
[root@localhost /]# docker pull centos:7
7: Pulling from library/centos
2d473b07cdd5: Pull complete
Digest: sha256:0f4ec88e21daf75124b8a9e5ca03c37a5e937e0e108a255d890492430789b60e
Status: Downloaded newer image for centos:7
docker.io/library/centos:7
查看镜像
docker images
[root@localhost /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
seacms v6.61 17720ee28ffb 39 minutes ago 739MB
seacms demo bbc3a9befb03 42 minutes ago 739MB
seacms v6.1 294c32f9d5b1 18 hours ago 705MB
drupal_7.57 cve-2018-7602 7b8d148f180b 25 hours ago 405MB
drupal_8.3.0 cve-2017-6920 5c4fe0f07e90 26 hours ago 691MB
wordpress4.7 cve-2017-5487 f620e62f2ca8 26 hours ago 946MB
tobyshawn/ubuntu16_04_lamp v1 c231de98bb36 2 days ago 602MB
vulfocus/seacms-cnvd_2020_22721 latest f4dccf30c280 10 days ago 585MB
ubuntu 16.04 9ff95a467e45 2 weeks ago 135MB
secfa/docker-awvs latest cd2a9f00863a 3 weeks ago 1.06GB
centos 7 8652b9f0cb4c 6 months ago 204MB
drupal 7.57 79c9b34e01cf 3 years ago 403MB
drupal 8.3.0 4e47ab49efee 4 years ago 445MB
导出镜像
[root@localhost /]# docker save seacms:v6.61 > seacms_v6.61.tar
[root@localhost /]# ls
seacms_v6.61.tar
导入镜像
docker load < tar包
[root@localhost /]# docker load < dircms_2011-sp3-utf8.tar
239bf11460ef: Loading layer [==================================================>] 47.15MB/47.15MB
Loaded image: dircms:2011-sp3-utf8
[root@localhost /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
dircms 2011-sp3-utf8 8a9c43e51650 18 hours ago 785MB
使用load导入的镜像不支持改名
(建议save导出的镜像用load导入,export导出的镜像用import导入,不要混用)
容器操作
创建容器
docker run -it 镜像名:tag [command]
-i:交互式操作
-t:终端
-d:后台运行,不进入终端
-p:指定端口映射,如8080:80的意思是将容器的80端口映射到宿主机上,访问宿主机的8080端口即可访问容器的80端口
[command]:/bin/bash、/bin/sh,看需求,在进入容器时执行的命令,也可以自己写上指定脚本用于运行
[root@localhost /]# docker run -it -p 8080:80 centos:7
[root@87369de718c8 /]# ls
anaconda-post.log bin dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
[root@87369de718c8 /]# pwd
/
[root@87369de718c8 /]# whoami;id
root
uid=0(root) gid=0(root)
启动、停止、重启容器
启动已有容器的命令为docker start 容器id
[root@localhost /]# docker start 625dcd1bf772
625dcd1bf772
进入容器
对于后台运行的容器(-d参数创建的容器,或是start已存在的容器),需要进入容器时有两种方法
1、docker attach
docker attach 容器id #exit时会停止容器(进⼊⼀个容器正在执⾏的终端)
2、docker exec
docker exec -it 容器id /bin/bash #exit时不会停止容器(进⼊容器新开⼀个终端)
[root@localhost /]# docker exec -it 625dcd1bf772 /bin/bash
root@625dcd1bf772:/#
容器保存成镜像
docker commit 容器ID 镜像名
[root@localhost /]# docker commit 28a8e3c0c9dd dircms_demo
sha256:47249d0234ee9b962ff1121589bf5365746b614f51c72f33e78f3a51c48ca704
[root@localhost /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
dircms_demo latest 47249d0234ee 7 seconds ago 783MB
容器导出为镜像
docker export 容器id > name.tar
[root@localhost /]# docker export 625dcd1bf772 > name.tar
[root@localhost /]# ll
总用量 2994444
-rw-r--r--. 1 root root 613240832 6月 4 14:50 name.tar
容器快照文件导入为镜像
docker import 容器文件 镜像名
[root@localhost /]# docker import name.tar name:test
sha256:8feb4db4855da4aa9de16cbd57088d4946b8a2844a91737444ae3fe5ab1990c3
[root@localhost /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
name test 8feb4db4855d 6 seconds ago 602MB
使用import导入的镜像可以指定镜像名与tag
(建议save导出的镜像用load导入,export导出的镜像用import导入,不要混用)
容器改名
docker rename old_name new_name
[root@localhost /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
87369de718c8 centos:7 "/bin/bash" 22 minutes ago Exited (127) 6 seconds ago dazzling_ritchie
[root@localhost /]# docker rename dazzling_ritchie rubbish
[root@localhost /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
87369de718c8 centos:7 "/bin/bash" 23 minutes ago Exited (127) 55 seconds ago rubbis
容器与宿主机文件拷贝
docker cp 文件 容器名:容器内路径
[root@localhost /]# docker cp dircms_sp3_gbk.zip vigilant_mayer:/var/www/html
root@43dd7ab25a68:/var/www/html# ls
root@43dd7ab25a68:/var/www/html# ls
dircms_sp3_gbk.zip
容器内服务自启
制作完docker镜像后发现,每次新建容器或者重启容器,服务都不会自动起来,比如我搭建的cms,而apache2服务和mysql每次都是关闭的状态,还需要重新去开启,就很麻烦,因此希望容器内的服务能够随着容器的启动而启动。
目前情况
[root@localhost test]# docker start f1909ca90a08
f1909ca90a08
[root@localhost test]# docker exec -it f1909ca90a08 /bin/bash
root@f1909ca90a08:/# service apache2 status
* apache2 is not running
root@f1909ca90a08:/# service mysql status
* MySQL is stopped.
操作过程
在docker容器内写入sh,并赋予执行权限
命令根据自己的要求更改,最后留着/bin/bash是为了保留终端
(如果是打包集成环境,不需要终端操作而只是开启后后台运行的话,建议将/bin/bash 改成 tail -f /dev/null,因为)
root@f1909ca90a08:/# vi /etc/apache2/auto_start.sh
root@f1909ca90a08:/# cat /etc/apache2/auto_start.sh
service apache2 start
service mysql start
/bin/bash
root@f1909ca90a08:/# chmod 777 /etc/apache2/auto_start.sh
退出容器,并关闭,保存为镜像
root@f1909ca90a08:/# exit
exit
[root@localhost test]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f1909ca90a08 seacms:v6.1 "/bin/bash" About an hour ago Up 5 minutes 3306/tcp, 0.0.0.0:8080->80/tcp, :::8080->80/tcp mystifying_bose
[root@localhost test]# docker stop f1909ca90a08
f1909ca90a08
[root@localhost test]# docker commit f1909ca90a08 seacms:demo
sha256:bbc3a9befb0323272f2889d6ce9d4cf3a1c20270f3fb765325252ae6fbcc8a88
[root@localhost test]#
在一个*空目录*下,创建Dockerfile
FROM 指定基础镜像
[root@localhost test]# ls
[root@localhost test]# vi Dockerfile
[root@localhost test]# cat Dockerfile
From seacms:demo
CMD ["/bin/sh","/etc/apache2/auto_start.sh"]
[root@localhost test]#
通过Dockerfile构建新的镜像
-t指定最终镜像名,需要跟地址,这里使用的是 “.” ,也就是当前目录
[root@localhost test]# docker build -t seacms:v6.61 .
Sending build context to Docker daemon 2.048kB
Step 1/2 : From seacms:demo
---> bbc3a9befb03
Step 2/2 : CMD ["/bin/sh","/etc/apache2/auto_start.sh"]
---> Running in ec97dd1c1b8d
Removing intermediate container ec97dd1c1b8d
---> 17720ee28ffb
Successfully built 17720ee28ffb
Successfully tagged seacms:v6.61
[root@localhost test]#
结果
用新的镜像创建容器
[root@localhost test]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
seacms v6.61 17720ee28ffb About a minute ago 739MB
seacms demo bbc3a9befb03 4 minutes ago 739MB
seacms v6.1 294c32f9d5b1 17 hours ago 705MB
[root@localhost test]# docker run -it -p 8080:80 seacms:v6.61
* Starting web server apache2 AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
*
* Starting MySQL database server mysqld [ OK ]
* Checking for tables which need an upgrade, are corrupt or were
not closed cleanly.
root@87da08c8d005:/# service apache2 status
* apache2 is running
root@87da08c8d005:/# service mysql status
* /usr/bin/mysqladmin Ver 8.42 Distrib 5.5.60, for debian-linux-gnu on x86_64
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Server version 5.5.60-0ubuntu0.14.04.1
Protocol version 10
Connection Localhost via UNIX socket
UNIX socket /var/run/mysqld/mysqld.sock
Uptime: 17 sec
Threads: 1 Questions: 212 Slow queries: 0 Opens: 82 Flush tables: 1 Open tables: 75 Queries per second avg: 12.470
root@87da08c8d005:/#
此时服务已经随着容器的启动而启动了