Docker是一個開源的、跨平臺的應用容器引擎,可以讓技術開發認用打包他們的應用以及一些依賴包到一個可移植的容器平臺中,發佈到任何流行的Linux操作系統上面,也可以在Windows和mac操作系統上面跑,容器是完全使用沙箱機制,容器相互之間不會存在任何接口。docker應用廣泛、安全性好、運行快、節約資源、移植性好、自動化部署快等有點。
我們在學習Docker鏡像的時候,使用系統默認的yum源安裝會非常慢,很有可能會安裝失敗,因爲docker服務器在國外,訪問速度非常慢,訪問網絡也不穩定,而且國外的docker hub官網拉取鏡像也太慢了,我們可以直接修改用國內的阿里的容器Hub服務 https://cr.console.aliyun.com的控制檯,這裏需要登錄阿里賬號或者支付寶賬號,也可以使用騰訊雲、百度雲、華爲雲、ucloud、網易雲等都有docker鏡像hub,我這裏使用的阿里雲鏡像服務;在學習docker容器的前提下還是要對Linux要有點基礎,這樣學起來就容易上手,如果完全不懂Linux,還是建議你去學習下《Linux就該這麼學》這邊教程裏面的內容非常詳細,比較適合初學者。此文章比較適合於企業應用。
# yum list docker-ce.x86_64 --showduplicates | sort -r
Loading mirror speeds from cached hostfile
docker-ce.x86_64 3:18.09.0-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.0-3.el7 @docker-ce-stable
docker-ce.x86_64 18.06.1.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.0.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.03.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 18.03.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.12.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.12.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.09.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.09.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.06.2.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.06.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.06.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.3.ce-1.el7 docker-ce-stable
docker-ce.x86_64 17.03.2.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.0.ce-1.el7.centos docker-ce-stable
# sudo yum -y install docker-ce-[VERSION]
yum install -y yum-utils device-mapper-persistent-data lvm2 -y
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
systemctl start docker.service
Docker version 18.09.0, build 4d60db4
指定主機啓動的名稱
[root@centos7 ~]# docker run -itd -h hostname 鏡像
[root@centos7 ~]# docker run -itd docker.io/ubuntu /bin/bash
[root@centos7 ~]# docker run -itd -p 0.0.0.0:80:80 --name apache docker.io/ubuntu /bin/bash
docker pull nginx //選擇合適的鏡像並拉取鏡像
docker images nginx //在本地鏡像列表裏查到 REPOSITORY 爲 nginx 的鏡像
docker images httpd //在本地鏡像列表裏查到 REPOSITORY 爲apache的鏡像
docker load -i 鏡像名稱.tar //導入鏡像
docker attach 鏡像名/鏡像ID //進入鏡像裏面
docker exec 鏡像名/鏡像ID ls /home //在執行shell命令到容器裏面
[root@centos7 ~]# docker exec elegant_bhaskara ls /home
[root@centos7 ~]# docker stop 鏡像名/鏡像ID //停止鏡像
[root@centos7 ~]# docker tag 原鏡像名 新鏡像名 //生成新的images
舉例:docker tag docker.io/ubuntu ubuntu:laste
[root@centos7 ~]# docker run -it test:ubuntu /bin/bash
[root@centos7 ~]# docker commit hopeful_carson(NAMES) ubuntu(REPOSITORY):self(tag) //提交鏡像,生成新的鏡像,鏡像裏面的配置也保留了,便於多個版本的管理
[root@centos7 ~]# docker run -itd --name test01_self ubuntu:self 用提交的鏡像再啓動新的鏡像
0f280fd95659c81fcff4069bb53ff53b07d06b28de05111dd5a9177e16865f22
[root@centos7 ~]# docker exec test01_self ls /home
5、提交運行中的容器爲一個鏡像 (這樣不會丟失在容器的各種操作)
[root@centos7 ~]# docker commit clever_haibt clever_haibt_new
### clever_haibt(運行容器名稱) clever_haibt_new(生成鏡像名稱)
[root@centos7 ~]# docker run -d -it -p 80:80 clever_haibt_new:latest /bin/bash
### 小p是自定義端口 latest 是鏡像的標籤(最好寫上專業點)
# docker run -d -it -p 80:80 httpd:latest
[root@centos7 ~]# docker ps //查看已運行的容器狀態
[root@centos7 ~]# docker system df
[root@centos7 ~]# docker stop --help
9、linux下解決docker端口映射到宿主機後外網無法訪問的問題?
[root@centos7 ~]# vim /etc/sysctl.conf
或者
[root@centos7 ~]# vim /usr/lib/sysctl.d/00-system.conf
添加如下代碼:
net.ipv4.ip_forward=1
重啓network服務
# systemctl restart network
查看是否修改成功
# sysctl net.ipv4.ip_forward
如果返回爲“net.ipv4.ip_forward = 1”
則表示成功了
[root@centos7 ~]# docker rm 鏡像名稱/容器ID
[root@centos7 ~]# docker rm -f 鏡像名稱/容器ID //強制刪除鏡像
11、導出鏡像,生成tar包,export導出的是讀寫層的文件系統
[root@centos7 ~]# docker export 鏡像名/鏡像ID > XXXX.tar
[root@centos7 ~]# docker export test01_self > test01.tar
[root@centos7 ~]# docker save docker.io/imagine10255/centos6-lnmp-php56 > lnmp.tar
[root@centos7 ~]# docker import test01.tar(鏡像包) ubuntu:self_new(新的鏡像名)
[root@centos7 ~]# docker run -itd --name ubuntu_self_new ubuntu:self_new /bin/bash
[root@centos7 ~]# docker rmi docker.io/imagine10255/centos6-lnmp-php56 //刪除
[root@centos7 ~]# docker load -i lnmp.tar //重新導入