docker安裝+使用總結
一.Docker安裝
此處以CentOS下安裝Docker為例:
Installusing the repository
Before you install Docker CE for the first time on a new hostmachine, you need to set up the Docker repository. Afterward, you can installand update Docker from the repository.
SET UP THEREPOSITORY
1. Install required packages. yum-utils
provides the
yum-config-manager
utility, anddevice-mapper-persistent-data
and lvm2
are required by the devicemapper
storage driver.2.
$ sudoyum install
-yyum-utils
\
3.
device-mapper-persistent-data
\
4.
lvm2
5. Use the following command to set up the stable repository. You always need the stablerepository,even if you want to install builds from the edge
6.
$ sudoyum-config-manager
\
7.
--add-repo\
8.
https://download.docker.com/linux/centos/docker-ce.repo
9. Optional: Enable the edge and test repositories. These repositories areincluded in thedocker.repo
file above but are disabled by default. You can enable themalongside the stable repository.
10.
$ sudoyum-config-manager
--enabledocker-ce-edge
11.
$ sudoyum-config-manager
--enabledocker-ce-test
You can disable the edge or test repository by running the yum-config-manager
command with the --disable
flag. To re-enable it, use the --enable
flag. The following command disables theedge repository.
$ sudoyum-config-manager
--disabledocker-ce-edge
Note: Starting with Docker17.06, stable releases are also pushed to the edge and testrepositories.
stableandedgebuilds.
INSTALL DOCKER CE
1. Install the latestversion of Docker CE,or go to the next step to install a specific version:
$ sudo yum install docker-ce
2. Start Docker.
$ sudo systemctl start docker
3. Verify that docker
is installed correctly by running the hello-world
image.
$ sudo docker run hello-world
二.更改Docker映象預設儲存路徑
預設情況下,docker映象的預設儲存路徑是/var/lib/docker,這相當於直接掛載系統目錄下,而一般在搭系統時,這個區都不會太大,所以如果長期使用docker開發應用,就需要把預設的路徑更改到/home路徑下(這個區一般會分得比較多)
命令列:
vi/usr/lib/systemd/system/docker.service
在文字內容ExecStart=/usr/bin/dockerd 後面新增如下內容:
--graph <yourpath>使得變成:
ExecStart=/usr/bin/dockerd \
--graph <your path>
然後重新載入配置檔案
systemctldaemon-reload
最後重啟即可
systemctlrestart docker
注:更改過後,之後解除安裝docker並刪除所有映象和容器的目錄,就要rm -rf <your path>
三.常用命令總結
#構建映象(當前目錄下需要有Dockerfile檔案)
dockerbuild -t showdoc ./
#檢視映象
docker images
#刪除映象
dockerrmi 映象名稱
#檢視容器(預設檢視執行中的容器)
docker ps
#檢視所有容器
docker ps -a
#刪除容器(刪除前需要確保容器停止執行)
docker rm 容器名
#執行一個web應用
#前面我們執行的容器並沒有一些什麼特別的用處。
#接下來讓我們嘗試使用 docker 構建一個 web 應用程式。
#引數說明:
#-d:讓容器在後臺執行。
#-P:將容器內部使用的網路埠對映到我們使用的主機上。
#--name指定容器名稱
dockerrun -d --name showdoc -p 4999:80 showdoc