一、准備安裝環境
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@hqs lib]# yum remove docker-buildx-plugin
# 檢查
[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
# 有時多值選項月可以使用更復雜的值字符串
# 后台運行容器
[root@hqs ~]# docker run -itd ubuntu /bin/bash
a62e6d0644d5d7f620be25310d76bb7c90e6254e077cec1261677fcc31890a70
# 登錄容器
[root@hqs ~]# docker exec -ti a62e6d0644d5 /bin/bash
root@a62e6d0644d5:/# ls
任務1:docker安裝好,能夠運行hello-world,能開機自動啟動
任務2:查看學習一個docker的常用命令,運行一個centos容器並嘗試連接進去