我的博客是在docker中搭建的,docker好处自然不需要多说,高效,安全,而且方便搬家。
将docker加入用户组
Docker 需要用户具有 sudo 权限,为了避免每次命令都输入sudo
,可以把用户加入 Docker 用户组(官方文档)。
$ sudo usermod -aG docker $USER
Docker 是服务器----客户端架构。命令行运行docker
命令的时候,需要本机有 Docker 服务。如果这项服务没有启动,可以用下面的命令启动(官方文档)。
docker命令的基础用法
这里只记录我常用的一些命令
service 命令的用法
$ sudo service docker start
systemctl 命令的用法
$ sudo systemctl start docker
列出本机的所有 image 文件。
$ docker image ls
删除 image 文件
$ docker image rm [imageName]
一些运行实例
$ docker image pull library/hello-world
$ docker container run hello-world
#现在docker好像直接run就可以自己pull
有些docker提供的是一种服务,不会终止
$ docker container run -it ubuntu bash
$ docker container kill [containID]
# 列出本机正在运行的容器
$ docker container ls
# 列出本机所有容器,包括终止运行的容器
$ docker container ls --all
#删除
$docker container rm [containerID]
#向docker中拷贝文件
docker cp 本地路径 容器长ID:容器路径
#从docker中拷贝文件出来
docker cp [CONTAINER_ID]:[SRC_PATH] [DEST_PATH]
#停止正在运行的容器
docker stop test
#将运行的容器commit为image
docker commit test testnow
#备份
docker save -o ~/Desktop/backup.tar backup(容器ID)
#回复
docker load -i ~/backup.tar
一些常见坑点
Error response from daemon: conflict: unable to remove repository reference
┌─[thekingofnight@parrot]─[~]
└──╼ $docker image rm test
Error response from daemon: conflict: unable to remove repository reference "test" (must force) - container be0d6de6f803 is using its referenced image 6ba6db0eca40
┌─[✗]─[thekingofnight@parrot]─[~]
└──╼ $docker rm be0d6de6f803
be0d6de6f803
┌─[thekingofnight@parrot]─[~]
└──╼ $docker image rm test
Error response from daemon: conflict: unable to remove repository reference "test" (must force) - container c2a172dd37d7 is using its referenced image 6ba6db0eca40
┌─[✗]─[thekingofnight@parrot]─[~]
└──╼ $docker rm c2a172dd37d7
c2a172dd37d7
┌─[thekingofnight@parrot]─[~]
└──╼ $docker image rm test
Untagged: test:latest
Deleted: sha256:6ba6db0eca40bf5b8b404f16f16d04afa1745340ae7df181067aa10d0d7eb36a
Deleted: sha256:9a2a4ed751f6fec8ced07e67d864501d79a6c45336311da614f9039f14e7f64e
Deleted: sha256:b114c88a042ac451eeed65593274c2b0f25bc65809fbf52b6188a4249e24170f
Deleted: sha256:984f86a21f53b1442c453442a99cf56013e0074327a102aa7996d663514e1c94
Deleted: sha256:d7d692701a3132106e5cafe0ee5c5f0dc9fa8c869a756797c9f14276f6b606e9
Deleted: sha256:f12c5e4d2355812d84642d4100f6883dc0a479a80b2b6df4692f5a7402039e67
Deleted: sha256:b6a0a6a2007ee305ec7c0deaabff7d017e6db04e7d4d69b89b783735fdd45d97
Deleted: sha256:064e5245d44cc966561c93622051d849583d2acf69acabf934dc06b8834a19ca
Deleted: sha256:fb6e10bf973b841879e08f77bbc2d2cc4972828c6d5139bdb2c5da93bf8c8937
Deleted: sha256:e78f5f2111f45006b1480d731a7bdd4dbdc7d50d8d43fd5dfa175dda97ac3934
Deleted: sha256:866aaa9eeace24981159aee755c6007aca2985ccbda0531c66e15c376e79f638
Deleted: sha256:4d1db642d0f347ec1fa0c0d5d124dd0b82c4296b07b5244873eb5ba7cc58c127
Deleted: sha256:7ec30a513de1fb2afd71282ff22c5840af2f0d8c64dadc8a3cebe5917f3c5d5b
Deleted: sha256:9f131fba0383a6aaf25ecd78bd5f37003e41a4385d7f38c3b0cde352ad7676da
Deleted: sha256:cd7100a72410606589a54b932cabd804a17f9ae5b42a1882bd56d263e02b6215
docker image REPOSITORY 为 TAG 为none的解决方法
┌─[thekingofnight@parrot]─[~]
└──╼ $docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 6ba6db0eca40 2 months ago 334MB
解决方法
┌─[✗]─[thekingofnight@parrot]─[~]
└──╼ $docker tag 6ba6db0eca40 test
└──╼ $docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
test latest 6ba6db0eca40 2 months ago 334MB
CentOS7 docker中文编码的一个坑
vi /etc/bashrc
export LC_ALL='zh_CN.UTF-8'
docker 开发
docker可以将内部服务端口映射出来
docker run -d -p 80:80 --name test Image
-d代表后台运行
-p 映射到宿主电脑的端口: docker内部的端口
--name 启动容器的名称
Image 选择的镜像
做好端口映射后,在docker内启动服务,然后进入容器
docker exec -it test bash
就类似正常的linux命令行操作,当然也可以写个脚本将外面做好的服务实时的更新到docker相应文件。
如果想尝试在docker中搭建hexo博客,我推荐几个链接
https://hub.docker.com/r/ipple1986/hexo
https://github.com/fi3ework/hexo-theme-archer
https://hub.docker.com/r/idiswy/hexo
https://www.jsdoi.com/blog/2016/02/24/blog-hexo-github/
https://www.huangtengxq.com/2017/08/19/BuildHexoOnVps/
应急相应
docker有时候可能因为修改文件而无法正常运行,启动直接退出,这里以我一次应急相应为例:
┌─[thekingofnight@parrot]─[~]
└──╼ $docker run -itd test2 /bin/bash
4fd13cae1eefe96623a53ccf1fca2440a2983cf122c0ef36e47033d560b42461
┌─[thekingofnight@parrot]─[~]
并没有进入终端,解决方案如下
┌─[✗]─[thekingofnight@parrot]─[~]
└──╼ $docker logs confident_kirch
FATAL can not read a block mapping entry; a multiline key may not be an implicit key at line 17, column 10:
permalink: :year/:month/:day/:title/
^
YAMLException: can not read a block mapping entry; a multiline key may not be an implicit key at line 17, column 10:
permalink: :year/:month/:day/:title/
^
at generateError (/opt/hexo/ipple1986/node_modules/js-yaml/lib/js-yaml/loader.js:162:10)
at throwError (/opt/hexo/ipple1986/node_modules/js-yaml/lib/js-yaml/loader.js:168:9)
at readBlockMapping (/opt/hexo/ipple1986/node_modules/js-yaml/lib/js-yaml/loader.js:1045:9)
.....
根据错误信息去修复文件,当然最简单的方法就是就是将文件拷贝出来,然后进行重新生成容器复制进去。也可以直接修改配置文件,重新启动。
┌─[root@parrot]─[/var/lib/docker]
└──╼ #find . -name '*.yml' | xargs grep -n ':year/:month/:day/:title/'
./overlay2/5dbc66df8005716fe8dadb67308228e1c4f29d9d3c374604558fbe8d52a4184d/diff/opt/hexo/ipple1986/_config.yml:17:permalink: :year/:month/:day/:title/
./overlay2/69627608d5c3abe41e63316f80c9d7beb3c5206920937cb03d853738c3d16767/diff/usr/lib/node_modules/hexo-cli/assets/_config.yml:17:permalink: :year/:month/:day/:title/
./overlay2/69627608d5c3abe41e63316f80c9d7beb3c5206920937cb03d853738c3d16767/diff/opt/hexo/ipple1986/node_modules/hexo/node_modules/hexo-cli/assets/_config.yml:17:permalink: :year/:month/:day/:title/
./overlay2/69627608d5c3abe41e63316f80c9d7beb3c5206920937cb03d853738c3d16767/diff/opt/hexo/ipple1986/_config.yml:17:permalink: :year/:month/:day/:title/
如果想深入,可以继续查阅如下文章
参考
http://www.ruanyifeng.com/blog/2018/02/docker-tutorial.html
http://www.ruanyifeng.com/blog/2018/02/docker-wordpress-tutorial.html