我的博客是在docker中搭建的策州,docker好處自然不需要多說(shuō),高效宫仗,安全,而且方便搬家旁仿。
將docker加入用戶組
Docker 需要用戶具有 sudo 權(quán)限藕夫,為了避免每次命令都輸入sudo
,可以把用戶加入 Docker 用戶組(官方文檔)枯冈。
$ sudo usermod -aG docker $USER
Docker 是服務(wù)器----客戶端架構(gòu)毅贮。命令行運(yùn)行docker
命令的時(shí)候,需要本機(jī)有 Docker 服務(wù)尘奏。如果這項(xiàng)服務(wù)沒(méi)有啟動(dòng)滩褥,可以用下面的命令啟動(dòng)(官方文檔)。
docker命令的基礎(chǔ)用法
這里只記錄我常用的一些命令
service 命令的用法
$ sudo service docker start
systemctl 命令的用法
$ sudo systemctl start docker
列出本機(jī)的所有 image 文件炫加。
$ docker image ls
刪除 image 文件
$ docker image rm [imageName]
一些運(yùn)行實(shí)例
$ docker image pull library/hello-world
$ docker container run hello-world
#現(xiàn)在docker好像直接run就可以自己pull
有些docker提供的是一種服務(wù)瑰煎,不會(huì)終止
$ docker container run -it ubuntu bash
$ docker container kill [containID]
# 列出本機(jī)正在運(yùn)行的容器
$ docker container ls
# 列出本機(jī)所有容器铺然,包括終止運(yùn)行的容器
$ docker container ls --all
#刪除
$docker container rm [containerID]
#向docker中拷貝文件
docker cp 本地路徑 容器長(zhǎng)ID:容器路徑
#從docker中拷貝文件出來(lái)
docker cp [CONTAINER_ID]:[SRC_PATH] [DEST_PATH]
#停止正在運(yùn)行的容器
docker stop test
#將運(yùn)行的容器commit為image
docker commit test testnow
#備份
docker save -o ~/Desktop/backup.tar backup(容器ID)
#回復(fù)
docker load -i ~/backup.tar
一些常見坑點(diǎn)
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中文編碼的一個(gè)坑
vi /etc/bashrc
export LC_ALL='zh_CN.UTF-8'
docker 開發(fā)
docker可以將內(nèi)部服務(wù)端口映射出來(lái)
docker run -d -p 80:80 --name test Image
-d代表后臺(tái)運(yùn)行
-p 映射到宿主電腦的端口: docker內(nèi)部的端口
--name 啟動(dòng)容器的名稱
Image 選擇的鏡像
做好端口映射后,在docker內(nèi)啟動(dòng)服務(wù)酒甸,然后進(jìn)入容器
docker exec -it test bash
就類似正常的linux命令行操作魄健,當(dāng)然也可以寫個(gè)腳本將外面做好的服務(wù)實(shí)時(shí)的更新到docker相應(yīng)文件。
如果想嘗試在docker中搭建hexo博客插勤,我推薦幾個(gè)鏈接
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/
應(yīng)急相應(yīng)
docker有時(shí)候可能因?yàn)樾薷奈募鵁o(wú)法正常運(yùn)行沽瘦,啟動(dòng)直接退出,這里以我一次應(yīng)急相應(yīng)為例:
┌─[thekingofnight@parrot]─[~]
└──? $docker run -itd test2 /bin/bash
4fd13cae1eefe96623a53ccf1fca2440a2983cf122c0ef36e47033d560b42461
┌─[thekingofnight@parrot]─[~]
并沒(méi)有進(jìn)入終端农尖,解決方案如下
┌─[?]─[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)
.....
根據(jù)錯(cuò)誤信息去修復(fù)文件析恋,當(dāng)然最簡(jiǎn)單的方法就是就是將文件拷貝出來(lái),然后進(jìn)行重新生成容器復(fù)制進(jìn)去盛卡。也可以直接修改配置文件助隧,重新啟動(dòng)。
┌─[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/
如果想深入窟扑,可以繼續(xù)查閱如下文章
參考
http://www.ruanyifeng.com/blog/2018/02/docker-tutorial.html
http://www.ruanyifeng.com/blog/2018/02/docker-wordpress-tutorial.html