docker入門(一)

docker安裝

yum install -y docker-iolinux

[root@centos ~]# yum install -y docker-io
已加載插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
 * extras: mirrors.163.com
 * updates: mirrors.163.com
正在解決依賴關係
--> 正在檢查事務
---> 軟件包 docker.x86_64.0.1.10.3-46.el7.centos.14 將被 安裝
--> 正在處理依賴關係 docker-common = 1.10.3-46.el7.centos.14,它被軟件包 
.....

docker版本查詢

docker versiondocker

Client:
 Version:         1.10.3
 API version:     1.22
 Package version: docker-common-1.10.3-46.el7.centos.14.x86_64
 Go version:      go1.6.3
 Git commit:      cb079f6-unsupported
 Built:           Fri Sep 16 13:24:25 2016
 OS/Arch:         linux/amd64

Server:
 Version:         1.10.3
 API version:     1.22
 Package version: docker-common-1.10.3-46.el7.centos.14.x86_64
 Go version:      go1.6.3
 Git commit:      cb079f6-unsupported
 Built:           Fri Sep 16 13:24:25 2016
 OS/Arch:         linux/amd64

啓動docker

舊式語法json

service docker startwindows

centos7新式語法centos

systemctl start docker.service測試

配置開機啓動

舊式語法ui

chkconfig docker on阿里雲

centos7新式語法centos7

systemctl enable docker.service插件

centos7 啓動docker失敗--selinux-enabled=false(時間2018-07-13)

當前新版本爲1.13,默認安裝好後啓動失敗。
如不想作過多設置,能夠關閉selinux-enabled=false,就能夠正常啓動。
從新編輯docker配置文件:
vi /etc/sysconfig/docker
找到如下這行
OPTIONS='--selinux-enabled --log-driver=journald --signature-verification=false'
將--selinux-enabled改成--selinux-enabled=false

配置加速器(阿里雲)

阿里雲的介紹:

配置Docker加速器

您可使用以下的腳本將mirror的配置添加到docker daemon的啓動參數中。
系統要求 CentOS 7 以上,Docker 1.9 以上。 (其它linux請詳見阿里雲介紹)

sudo cp -n /lib/systemd/system/docker.service /etc/systemd/system/docker.service

sudo sed -i "s|ExecStart=/usr/bin/docker daemon|ExecStart=/usr/bin/docker daemon --registry-mirror=https://ir6ei990.mirror.aliyuncs.com|g" /etc/systemd/system/docker.service

sudo systemctl daemon-reload

sudo service docker restart

執行後不起做用,也許由於docker版本不一致,本人是手動修改爲功的

找到這行

ExecStart=/usr/bin/docker-current daemon
將本身的加速地址加進去,最後修改成

ExecStart=/usr/bin/docker-current daemon --registry-mirror=https://ir6ei990.mirror.aliyuncs.com
而後重啓docker

systemctl daemon-reload

systemctl restart docker.service

下載鏡像(測試用,找個小的鏡像)

docker pull busybox

Using default tag: latest
Trying to pull repository docker.io/library/busybox ... 
latest: Pulling from docker.io/library/busybox
56bec22e3559: Pull complete 
Digest: sha256:29f5d56d12684887bdfa50dcd29fc31eea4aaf4ad3bec43daf19026a7ce69912
Status: Downloaded newer image for docker.io/busybox:latest

查看已下載的鏡像

docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/busybox   latest              e02e811dd08f        4 weeks ago         1.093 MB

這就是剛纔上面下載的busybox

啓動鏡像變爲容器

docker run -d -it --name testbusybox busybox

44391ec69021707afbeba0c1dda7633c4b0507838fecdf37ae3ad56182754710

-d 後臺啓動

-i, --interactive Keep STDIN open even if not attached

-t, --tty Allocate a pseudo-TTY

進行交互式操做(例如Shell腳本),那咱們必須使用-i -t參數同容器進行數據交互。可是當經過管道同容器進行交互時,就不須要使用-t參數

--name 容器名稱(方便後續操做)

查看已經啓動的容器(鏡像)

docker ps

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
8d713216033b        busybox             "sh"                5 minutes ago       Up 5 minutes                            testbusybox

進入運行中的容器

剛纔啓動的容器爲後臺運行。咱們能夠經過一下命令進入啓動的容器中

docker exec -it testbusybox /bin/sh

/ # pwd
/
/ # ls
bin   dev   etc   home  proc  root  run   sys   tmp   usr   var

退出容器

exit

/ # 
/ # exit
[root@centos ~]#

重啓or中止容器運行

docker restart testbusybox

[root@centos ~]# docker restart testbusybox
testbusybox

docker stop testbusybox

[root@centos ~]# docker stop testbusybox 
testbusybox
[root@centos ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

發現容器關閉後docker ps 命令看不到了,默認顯示啓動的容器,咱們能夠經過加-a參數來顯示中止的docker容器

[root@centos ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                            PORTS               NAMES
8d713216033b        busybox             "sh"                     22 hours ago        Exited (137) About a minute ago                       testbusybox
44391ec69021        busybox             "sh"                     22 hours ago        Exited (137) 22 hours ago                             trusting_wright
2810cbbefdbe        busybox             "sh"                     22 hours ago        Exited (0) 22 hours ago                               drunk_bell
ddf3b42c2970        c9bd19d022f6        "/entrypoint.sh /etc/"   2 days ago          Exited (2) 2 days ago                                 stoic_brattain

這些容器都是實際存在的。

以及啓動成容器的鏡像,沒法再次run。除非刪除容器後再run。
咱們拿windows的ghost打比方:
鏡像就是ghost光盤,容器就是已經裝入電腦後的系統,ghost光盤能夠爲一臺機器安裝無限次,可是docker有點區別,已經裝了A鏡像的電腦,沒法再次安裝A的鏡像。除非

1:刪除已安裝的容器,再次run A鏡像進行安裝。
2:換一個容器名字

[root@centos ~]# docker run -d -it --name testbusybox busybox
docker: Error response from daemon: Conflict. The name "/testbusybox" is already in use by container 8d713216033bd01610aa444647ce971e9d47786c81637e12b5a85659d0006b63. You have to remove (or rename) that container to be able to reuse that name..
See '/usr/bin/docker-current run --help'.

換了容器名後啓動成功

[root@centos ~]# docker run -d -it --name testbusybox1 busybox
3ec4539e98a82d07fa362c7d90ff1a27547130a642662a1b8904742ce4ed2419

刪除容器

docker rm -f testbusybox1

[root@centos ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS               NAMES
3ec4539e98a8        busybox             "sh"                     57 seconds ago      Up 56 seconds                                     testbusybox1
8d713216033b        busybox             "sh"                     22 hours ago        Exited (137) 11 minutes ago                       testbusybox
44391ec69021        busybox             "sh"                     22 hours ago        Exited (137) 22 hours ago                         trusting_wright
2810cbbefdbe        busybox             "sh"                     22 hours ago        Exited (0) 22 hours ago                           drunk_bell
ddf3b42c2970        c9bd19d022f6        "/entrypoint.sh /etc/"   2 days ago          Exited (2) 2 days ago                             stoic_brattain
[root@centos ~]# 
[root@centos ~]# docker rm -f testbusybox1
testbusybox1
[root@centos ~]# docker rm -f testbusybox
testbusybox
[root@centos ~]# docker rm -f trusting_wright
trusting_wright
[root@centos ~]# docker rm -f drunk_bell
drunk_bell
[root@centos ~]# docker rm -f stoic_brattain
stoic_brattain
[root@centos ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

修改鏡像tag

docker tag busybox registry.cn-hangzhou.aliyuncs.com/wzgdxg/testbusybox:1.0.0

docker images

docker.io/busybox                                      latest              e02e811dd08f        4 weeks ago         1.093 MB
registry.cn-hangzhou.aliyuncs.com/wzgdxg/testbusybox   1.0.0               e02e811dd08f        4 weeks ago         1.093 MB

將busybox修改成
registry.cn-hangzhou.aliyuncs.com/wzgdxg/testbusybox:1.0.0

推送到阿里雲倉庫

前面已經修改了對應的阿里雲倉庫名/鏡像名:Tag

推送的時候就會發送到registry.cn-hangzhou.aliyuncs.com/wzgdxg/testbusybox這個倉庫中去。

具體阿里雲docker鏡像倉庫能夠自行查閱阿里雲docker的幫助文檔,很簡單的。

發送前須要登陸阿里雲docker倉庫

docker login --username=wzgdxg registry.cn-hangzhou.aliyuncs.com

Password: 
WARNING: login credentials saved in /root/.docker/config.json
Login Succeeded

推送

docker push registry.cn-hangzhou.aliyuncs.com/wzgdxg/testbusybox:1.0.0

The push refers to a repository [registry.cn-hangzhou.aliyuncs.com/wzgdxg/testbusybox]
e88b3f82283b: Pushed 
1.0.0: digest: sha256:9393222c6789842b16bcf7306b6eb4b486d81a48d3b8b8f206589b5d1d5a6101 size: 505

刪除鏡像

docker rmi -f registry.cn-hangzhou.aliyuncs.com/wzgdxg/testbusybox:1.0.0

Untagged: registry.cn-hangzhou.aliyuncs.com/wzgdxg/testbusybox:1.0.0
[root@centos ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/busybox   latest              e02e811dd08f        4 weeks ago         1.093 MB

從阿里雲docker倉庫拉取鏡像(剛纔推送上去的鏡像)

docker pull registry.cn-hangzhou.aliyuncs.com/wzgdxg/testbusybox:1.0.0

Trying to pull repository registry.cn-hangzhou.aliyuncs.com/wzgdxg/testbusybox ... 
1.0.0: Pulling from registry.cn-hangzhou.aliyuncs.com/wzgdxg/testbusybox
Digest: sha256:9393222c6789842b16bcf7306b6eb4b486d81a48d3b8b8f206589b5d1d5a6101
Status: Downloaded newer image for registry.cn-hangzhou.aliyuncs.com/wzgdxg/testbusybox:1.0.0
[root@centos ~]# docker images
REPOSITORY                                             TAG                 IMAGE ID            CREATED             SIZE
docker.io/busybox                                      latest              e02e811dd08f        4 weeks ago         1.093 MB
registry.cn-hangzhou.aliyuncs.com/wzgdxg/testbusybox   1.0.0               e02e811dd08f        4 weeks ago         1.093 MB
相關文章
相關標籤/搜索