Docker介紹和安裝
阿新 • • 發佈:2022-04-20
一、準備安裝環境
1)建立虛擬機器 安裝vmware軟體。安裝secureCRT或者xshell軟體。 下載centos7.6地址:https://mirrors.aliyun.com/centos-vault/7.6.1810/isos/x86_64/CentOS-7-x86_64-DVD-1810.iso 建立虛擬機器。網路為nat模式。 2)配置網路 修改網絡卡:/etc/sysconfig/network-scripts/ifcfg-ens33 修改和新增如下配置: BOOTPROTO=static ONBOOT=yes IPADDR=192.168.88.101 NETMASK=255.255.255.0 GATEWAY=192.168.88.2 DNS=8.8.8.8 修改dns:/etc/resolv.conf 新增如下資訊: nameserver 8.8.8.8 重啟網路: systemctl restart network 測試網路:ping www.baidu.com 3)關閉防火牆和selinux systemctl disable firewalld systemctl stop firewalld 修改selinux配置檔案:/etc/selinux/config 改為:SELINUX=disabled 讓修改生效:setenforce 0 4)設定主機名 hostnamectl set-hostname hqs 修改之後:可以重新登入或直接輸入bash即可立即生效。 5)更新yum源 curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo # 清理yum yum clean all yum makecache 5)修改為上海時間 timedatectl set-timezone "Asia/Shanghai" 檢視修改結果:timedatectl
二、設定Docker倉庫
# 1)執行命令安裝必要的包 # yum-utils包中提供yum-config-manager工具 # devicemapper儲存驅動需要device-mapper-persistent和lvm2 yum install -y yum-utils device-mapper-persistent-data lvm2 # 2)執行命令設定Docker CE穩定版的倉庫地址 # 這裡選擇設定為阿里巴巴的映象倉庫源 [root@hqs111 yum.repos.d]# yum-config-manager --add-repo \ http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo Loaded plugins: fastestmirror adding repo from: http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo grabbing file http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo repo saved to /etc/yum.repos.d/docker-ce.repo # 執行後再/etc/yum.repos.d目錄下就多了一個docker.repo檔案,它定義了倉庫地址 # 預設只有穩定版被啟用。 # 如果要啟用Nightly版本: yum-config-manager --enable docker-ce-nightly (不用執行) # 如果要啟用test版本: yum-config-manager --enable docker-ce-test (不用執行) # 禁用倉庫方法: yum-config-manager --disable docker-ce-test (不用執行) # 將阿里雲的源改為docker官方源: yum-config-manager --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo (不用執行)
三、安裝Docker CE
# 準備工作:清理老的docker yum remove docker docker-client docker-client-latest docker-common \ docker-latest docker-latest-logrotate docker-logrotate docker-engine # 1)最簡單的方法是執行以下命令安裝最新版本的Docker CE和containerd yum install docker-ce docker-ce-cli containerd.io # 檢視當前版本 [root@hqs111 yum.repos.d]# docker --version Docker version 20.10.12, build e91ed57 # 2)解除安裝已安裝的docker-ce及其關聯依賴 [root@hqs111 yum.repos.d]# yum remove docker-ce [root@hqs111 yum.repos.d]# yum remove docker-ce-cli [root@hqs111 yum.repos.d]# yum remove docker-selinux [root@hqs111 yum.repos.d]# yum remove docker-engine-selinux # 檢查 [root@hqs111 yum.repos.d]# docker --version bash: /usr/bin/docker: No such file or directory # 3)安裝指定版本的docker-ce [root@hqs111 yum.repos.d]# yum install -y docker-ce-19.03.2-3.el7 docker-ce-cli-19.03.2-3.el7 containerd.io [root@hqs111 yum.repos.d]# docker --version Docker version 19.03.2, build 6a30dfc # 4)檢視可用的docker版本 # 預設是按照版本從低到高排列 [root@hqs111 yum.repos.d]# yum list docker-ce --showduplicates # 新增sort -r 是將結果按照版本從高到低排列 [root@hqs111 yum.repos.d]# yum list docker-ce --showduplicates | sort -r 軟體包名稱 版本字串 倉庫的名稱(軟體包儲存的位置) docker-ce.x86_64 3:19.03.6-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.5-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.4-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.3-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.2-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.2-3.el7 @docker-ce-stable(有@說明是本機已經安裝的版本) docker-ce.x86_64 3:19.03.15-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.14-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.1-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.13-3.el7 docker-ce-stable
四、啟動Docker並測試
# 啟動docker服務
[root@hqs ~]# systemctl start docker
# 檢視docker服務狀態
[root@hqs ~]# systemctl status docker
1、測試docker
# 執行hello-world映象
[root@hqs ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:97a379f4f88575512824f3b352bc03cd75e239179eea0fecc38e597b2209f49a
Status: Downloaded newer image for hello-world:latest
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.
2、解除安裝Docker
# 執行解除安裝命令
yum remove docker-ce docker-ce-cli containerd.io
# 解除安裝後,主機上的映象、容器等不會被刪除,刪除命令如下:
rm -rf /var/lib/docker
# 管理員必須手動刪除任何已編輯的配置檔案
五、安裝Docker之後的配置
1、配置Docker開機自動啟動
[root@hqs docker]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
# 禁用開機啟動:systemctl disable docker
2、以非root使用者管理docker
# 建立docker組——centos安裝docker後預設有docker組
[root@hqs docker]# sudo groupadd docker
groupadd: group 'docker' already exists
# 向docker組新增使用者
[root@hqs docker]# useradd hqs
[root@hqs docker]# sudo usermod -aG docker hqs
3、開啟Docker遠端訪問
# 編輯docker.service單元配置檔案
[root@hqs docker]# systemctl edit docker.service
# 輸入以下資訊:
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H unix:///var/run/docker.sock -H tcp://0.0
.0.0:2375
# 重新載入systemctl配置
[root@hqs docker]# systemctl daemon-reload
# 重啟docker
[root@hqs docker]# systemctl restart docker
# 檢查確認Docker守護程序是否在所配置的埠上偵聽
# 安裝網路監測工具
[root@hqs docker]# yum install -y net-tools
# 監測docker守護程序
[root@hqs docker]# netstat -lntp | grep dockerd
tcp6 0 0 :::2375 :::* LISTEN 2122/dockerd
# 在Docker客戶端命令中通過-H選項指定要連線的遠端主機
[root@hqs docker]# docker -H tcp://192.168.88.101:2375 info
六、Docker命令列
1、Docker命令列介面型別
- 引擎命令列介面(Engine CLI):docker主要的命令,包含docker和dockerd命令。
- 容器編排命令列介面(Compose CLI):Docker Compose提供,讓使用者構建並執行多容器應用。
- 機器命令列介面(Machine CLI):用來配置和管理遠端docker主機。
- DTR命令列介面:部署管理docker可信註冊中心。
- UCP命令列介面:部署和管理通用面板(universal control plane)。
2、docker命令
[root@hqs docker]# docker
Usage: docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default # 客戶端配置檔案
"/root/.docker")
-c, --context string Name of the context to use to connect to the
daemon (overrides DOCKER_HOST env var and
default context set with "docker context use")
-D, --debug Enable debug mode # 啟動除錯模式
-H, --host list Daemon socket(s) to connect to
-l, --log-level string Set the logging level
("debug"|"info"|"warn"|"error"|"fatal") (default
"info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default
"/root/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default
"/root/.docker/cert.pem")
--tlskey string Path to TLS key file (default
"/root/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit # 輸出版本資訊並退出
Management Commands: # 管理命令列表
builder Manage builds # 管構建
config Manage Docker configs # 管配置
container Manage containers # 管容器
context Manage contexts # 管上下文
engine Manage the docker engine # 管引擎
image Manage images
network Manage networks
node Manage Swarm nodes # 管swarm節點
plugin Manage plugins
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
trust Manage trust on Docker images
volume Manage volumes
Commands: # 操作命令列表
attach Attach local standard input, output, and error streams to a running container
build Build an image from a Dockerfile # 構建映象
commit Create a new image from a container changes # 從變動的容器建立映象
cp Copy files/folders between a container and the local filesystem # 本地和容器間複製
create Create a new container
diff Inspect changes to files or directories on a container filesystem
events Get real time events from the server
exec Run a command in a running container # 在執行中的容器中執行命令
export Export a container filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information # 顯示系統資訊
inspect Return low-level information on Docker objects # 返回容器的詳細資訊
kill Kill one or more running containers # 強制停止一個容器
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container # 獲取容器的日誌
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers # 檢視容器列表
pull Pull an image or a repository from a registry # 拉取映象
push Push an image or a repository to a registry # 推送映象
rename Rename a container
restart Restart one or more containers # 重啟容器
rm Remove one or more containers # 刪除容器
rmi Remove one or more images # 刪除映象
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes
Run 'docker COMMAND --help' for more information on a command.
3、docker基本語法
# 基本語法
docker [選項] 命令
# 短格式的為一個連字元(-)加上單個字元,如-d、-v。
[root@hqs docker]# docker -v
Docker version 19.03.2, build 6a30dfc
[root@hqs docker]# docker run -i -t -d ubuntu /bin/bash
# -d 引數來實現:Docker 容器在後臺以守護態(Daemonized)形式執行。
# -t 選項:讓Docker分配一個偽終端(pseudo-tty)並繫結到容器的標準輸入上。
# -i 則讓容器的標準輸入保持開啟
21810e97ff7cbc5d84476cd2ac7e8ccfa7736431abc1850ee6a10ae4ba3570ab
[root@hqs docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
21810e97ff7c ubuntu "/bin/bash" 11 seconds ago Up 10 seconds
# 長格式為兩個連字元加上字串,如--daemon、, --version。
[root@hqs docker]# docker --version
Docker version 19.03.2, build 6a30dfc
# 短格式的單字元選項可以組合在一起使用
[root@hqs docker]# docker run -itd ubuntu /bin/bash
d2e3f069366bf2245a796ad436b66cdb0366a1c8e3bd3ebc9197dd6547432c2c
[root@hqs docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d2e3f069366b ubuntu "/bin/bash" 2 seconds ago Up 2 seconds
# 多值選項
[root@hqs docker]# docker run -a stdin -a stdout -astderr ubuntu /bin/ls
bin
boot
...
sys
tmp
usr
var
# 有時多值選項月可以使用更復雜的值字串
任務1:docker安裝好,能夠執行hello-world,能開機自動啟動
任務2:檢視學習一個docker的常用命令,執行一個centos容器並嘗試連線進去