Docker實踐:Centos下安裝Docker並簡單的使用

1 環境介紹

 

版本信息:linux

[root@localhost]#  cat /proc/versiongit

Linux version 3.10.0-514.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC) ) #1 SMP Tue Nov 22 16:42:41 UTC 2016docker

[root@localhost jenkins]# cat  /etc/redhat-releaseubuntu

CentOS Linux release 7.3.1611 (Core)centos

 

# Centos系統是離線的,可是能夠經過代理訪問網絡,不然,就沒法pull Image了.bash

# 查看yum list中是否有docker安裝包:網絡

[root@localhost home]# yum list | grep dockerui

docker.x86_64                              2:1.12.6-32.git88a4867.el7.centosdebug

docker-client.x86_64                       2:1.12.6-32.git88a4867.el7.centos3d

docker-common.x86_64                       2:1.12.6-32.git88a4867.el7.centos

[root@localhost home]#

2安裝Docker

# install

sudo yum install -y docker.x86_64

#  yum install docker

 

# 查看docker是否安裝成功

docker --version

 

# 開機自啓動, 好像開機啓動並非很靈

sudo chkconfig docker on

# 查看docker daemon是否在運行

ps aux | grep docker

3設置代理

# following link is to resolve docker-runc not found issue,proxy is not necessary *#

ln -s /usr/libexec/docker/docker-proxy-current /usr/bin/docker-proxy

ln -s /usr/libexec/docker/docker-runc-current /usr/bin/docker-runc

 

#中止docker service(設置代理前,須要中止)

service docker stop

 

# 設置代理, 成功後,會自動啓動docker service

sudo HTTP_PROXY=http://ip:port /usr/bin/dockerd --userland-proxy=false &

4 Docker Service

# 啓動Docker Service

# 注: 設置代理時,Docker Service會自動啓動

service docker start

# 或者這種啓動方式

systemctl start docker.service

 

#中止docker service(設置代理前,須要中止)

service docker stop

# 或者這種中止方式

systemctl stop docker.service

 

# 查看服務狀態

systemctl status docker.service

 

# 查看docker daemon是否在運行

ps aux | grep docker

 

 

5 Image

# 下載Image,  默認從docker.io下載,速度會比較慢

#           能夠從daocloud.io中下載,速度會快一些.

# 我在公司, Centos上感受不明顯, 在家裏用ubuntu感受特別明顯

docker pull centos

 

# 查詢倉庫中的Image

# --automated=false:       僅顯示自動建立的鏡像

# --no-trunc=false:   不截斷顯示

# -s, --starts=0:        指定星級以上

docker search -s 10 centos

 

# 查詢本地Image

docker images

 

# 刪除Image

 docker rmi docker.io/Ubuntu

6 運行Image

# 運行Image

 # 注意: 運行Image時, 須要關閉代理,重啓docker service,

 #      不然會遇到錯誤:container_linux.go:247: starting container process caused

 #      "process_linux.go:334: running prestart hook 1 caused \"error running hook: exit status 1, stdout: , stderr: \""

#          /usr/bin/docker-current: Error response from daemon: invalid header field value "oci runtime error:

#          container_linux.go:247: starting container process caused \"process_linux.go:334:

#           running prestart hook 1 caused \\\"error running hook: exit status 1, stdout: , stderr: \\\"\"\n".

 

# 方式1

sudo docker  run centos /bin/echo 'Hello World'

# 帶調試信息

sudo docker --debug=true run centos /bin/echo 'Hello World'

 

 

#方式2

[root@localhost home]# docker create centos

3e8185a9fb586e1b3f12fb1c69c2a22225df1e5505c9315e3b62743d489d2904

 

# 查看容器信息

[root@localhost home]# docker ps -a

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

3e8185a9fb58        centos              "/bin/bash"         5 seconds ago       Created                                 gigantic_franklin

[root@localhost home]# docker start 3e8185a9fb58

 

docker run -idt --name pcta-demon matthewx:pcta-matthewx

 

# 進入容器

docker attach pcta-demon

 

# 刪除容器

docker rm 3e8185a9fb58

 

7 卸載Docker

#centos卸載

yum list installed | grep docker

yum -y remove docker.x86_64 

yum -y remove docker-client.x86_64 

yum -y remove docker-common.x86_64 

# 若是/var/lib/docker刪除失敗,

# 請使用lsof |grep docker看看有哪些進程在使用,kill掉這些進程.

# 此處刪除應該是刪除pull的image等,因此也能夠不刪除.

rm -rf /var/lib/docker

lsof |grep docker

相關文章
相關標籤/搜索