docker入門(一)


docker安裝

yum install -y docker-io

[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 version

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

舊式語法

service docker start

centos7新式語法

systemctl start docker.service

配置開機啟動

舊式語法

chkconfig docker on

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM