1.1 CentOS安裝Docker容器

Dockers簡介

軟件開發中最爲麻煩的事情可能就是配置環境了。由於用戶使用的操作系統具有多樣性,即便是使用跨平臺的開發語言都不能保證代碼能夠在各種平臺下都可以正常運轉,而且可能在不同的環境下我們的軟件需要依賴的其他軟件包也是不一樣的。

Docker屬於對Linux容器技術的一種封裝(利用了Linux的namespace和cgroup技術),它提供了簡單易用的容器使用接口,是目前最爲流行的Linux容器解決方案。Docker將應用程序與該應用程序的依賴打包在一個文件裏面,運行這個文件,就會生成一個虛擬容器。程序在這個虛擬容器裏面運行,就好像在真實的物理機上運行一樣。有了Docker就再也不用擔心環境問題了。

目前,Docker主要用於以下幾個方面:
1、提供一次性的環境。
2、提供彈性的雲服務(利用Docker很容易的實現擴容和收縮)。
3、實踐微服務架構(隔離真是環境在容器中運行多個服務)。

安裝Docker

0、確定操作系統內核版本(CentOS 7要求爲64位,內核版本3.10+;CentOS 6要求64位,內核版本2.6+)

[root@localhost ~]# uname -r
3.10.0-1062.4.1.el7.x86_64

1、在CentOS下使用yum安裝Docker並啓動

[root@localhost ~]# yum -y install docker-io
[root@localhost ~]# systemctl start docker

2、查看Docker的信息和版本

[root@localhost ~]# docker version
[root@localhost ~]# docker info

3、運行Hello-World項目來測試Docker。第一次運行時由於本地沒有hello-world的鏡像,因此需要聯網進行下載。

[root@localhost ~]# docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

4、關於鏡像文件的下載運行查看以及刪除等。

1)下載鏡像

[root@localhost ~]# docker pull <name>

2)運行鏡像

[root@localhost ~]# docker run <name>
[root@localhost ~]# docker run -p <port1>:<port> <name>

3)查看鏡像文件

[root@localhost ~]# docker image ls
[root@localhost ~]# docker image

4)刪除鏡像文件——首先要停止所有的container,如果不停止就會報錯

[root@localhost ~]# docker stop $(docker ps -a -q)

如果提示鏡像被container加載着,繼續使用如下命令

[root@localhost ~]# docker rm $(docker ps -a -q)

然後使用以下命令即可

[root@localhost ~]# docker rmi <name>

5、容器的查看以及停止

1)查看正在運行的容器

[root@localhost ~]# docker ps

2)停止運行的容器

[root@localhost ~]# docker stop <container-id>
[root@localhost ~]# docker stop <name>

3)對於那些不會自動終止的容器,就可以用下面的方式來停止

[root@localhost ~]# docker container kill <container-id>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章