本篇文章开始给大家介绍Docker,这个是我非常喜欢的一个服务,介绍给大家,希望大家也能喜欢!
目录
八、二进制安装docker在Linux系统(CentOS,Ubuntu)
一、搭建私有的yum仓库,实现docker的私有仓库部署,要求可以指定版本
何为容器
所谓的容器技术就是能够将计算机的程序及其所依赖的库进行打包的一种技术手段,方便进行传输,部署,运行;容器可以实现帮我们将程序和其所依赖的库文件进行打包。
Docker环境安装部署
一、yum安装(CentOS7.9)
1、卸载旧的docker版本
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
2、配置docker软件源
[root@Docker01 ~]# curl -s -o /etc/yum.repos.d/docker-ce.repo https://download.docker.com/linux/centos/docker-ce.repo
3、安装docker
[root@Docker01 ~]# yum -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
4、启动docker服务
[root@Docker01 ~]# systemctl enable --now docker
5、查看docker服务
[root@Docker01 ~]# docker version #在服务端查看详细信息
Client: Docker Engine - Community
Version: 24.0.2
API version: 1.43
Go version: go1.20.4
Git commit: cb74dfc
Built: Thu May 25 21:55:21 2023
OS/Arch: linux/amd64
Context: default
Server: Docker Engine - Community
Engine:
Version: 24.0.2
API version: 1.43 (minimum version 1.12)
Go version: go1.20.4
Git commit: 659604f
Built: Thu May 25 21:54:24 2023
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.6.21
GitCommit: 3dce8eb055cbb6872793272b4f20ed16117344f8
runc:
Version: 1.1.7
GitCommit: v1.1.7-0-g860f061
docker-init:
Version: 0.19.0
GitCommit: de40ad0
[root@Docker01 ~]# docker -v #在本地查看是否有docker
Docker version 24.0.2, build cb74dfc
6、验证docker是否安装成功
[root@Docker01 ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
719385e32844: Pull complete
Digest: sha256:fc6cf906cbfa013e80938cdf0bb199fbdbb86d6e3e013783e5a766f50f5dbce0
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.
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/
7、新手必备自动补全功能
docker --help可以查看使用方式,安装自动补全后按tab即可
[root@Docker01 ~]# yum -y install bash-completion
[root@Docker01 ~]# source /usr/share/bash-completion/bash_completion
[root@Docker01 ~]# docker #按tab键即显示
attach help pause stack
build history plugin start
builder image port stats
buildx images ps stop
commit import pull swarm
compose info push system
config inspect rename tag
container kill restart top
context load rm trust
cp login rmi unpause
create logout run update
diff logs save version
events manifest search volume
exec network secret wait
export node service
8、配置镜像加速
参考链接:https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors
[root@Docker01 ~]# mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://xg3udbel.mirror.aliyuncs.com"]
}
EOF
[root@Docker01 ~]# systemctl daemon-reload
[root@Docker01 ~]# systemctl restart docker
9、查看镜像加速是否配置成功
[root@Docker02 ~]# docker info | grep "Registry Mirrors" -A 1
Registry Mirrors:
https://xg3udbel.mirror.aliyuncs.com/
二、rpm包安装(CentOS)
1、下载相应的rpm包
rpm下载链接:https://download.docker.com/linux/centos/7/x86_64/stable/Packages/
可以利用它做本地yum仓库,我这边准备了24.0.0.2的rpm压缩包,下载链接放在文末
2、基于现成的rpm包进行安装
[root@Docker02 ~]# unzip koten-docker-24_0_2.zip && yum -y localinstall koten-docker-24_0_2/*.rpm
3、启动docker服务
[root@Docker02 ~]# systemctl enable --now docker
4、开启自动补全功能
安装的rpm包里有就不用下载了
[root@Docker02 ~]# source /usr/share/bash-completion/bash_completion
5、配置镜像加速
[root@Docker02 ~]# mkdir -p /etc/docker
[root@Docker02 ~]# tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://xg3udbel.mirror.aliyuncs.com"]
}
EOF
[root@Docker02 ~]# systemctl daemon-reload
[root@Docker02 ~]# systemctl restart docker
6、查看镜像加速是否配置成功
[root@Docker02 ~]# docker info | grep "Registry Mirrors" -A 1
Registry Mirrors:
https://xg3udbel.mirror.aliyuncs.com/
三、卸载docker环境
1、卸载软件包
[root@Docker01 ~]# yum -y remove docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras
2、删除数据目录
[root@Docker01 ~]# rm -rf /var/lib/docker /var/lib/containerd
四、安装指定的docker版本
1、添加docker-ce的软件源
[root@Docker01 ~]# curl -s -o /etc/yum.repos.d/docker-ce.repo https://download.docker.com/linux/centos/docker-ce.repo
2、查看软件源中的docker版本
[root@Docker01 ~]# yum list docker-ce --showduplicates | sort -r
3、安装指定的docker版本
[root@Docker01 ~]# yum -y install docker-ce-20.10.24 docker-ce-cli-20.10.24 containerd.io
4、配置镜像加速(同上)
5、配置自动补全
[root@Docker01 ~]# yum -y install bash-completion
[root@Docker01 ~]# source /usr/share/bash-completion/bash_completion
6、启动docker服务
[root@Docker01 ~]# systemctl enable --now docker
7、查看docker版本
[root@Docker01 ~]# docker version
Client: Docker Engine - Community
Version: 20.10.24
API version: 1.41
Go version: go1.19.7
Git commit: 297e128
Built: Tue Apr 4 18:22:57 2023
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.24
API version: 1.41 (minimum version 1.12)
Go version: go1.19.7
Git commit: 5d6db84
Built: Tue Apr 4 18:21:02 2023
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.6.21
GitCommit: 3dce8eb055cbb6872793272b4f20ed16117344f8
runc:
Version: 1.1.7
GitCommit: v1.1.7-0-g860f061
docker-init:
Version: 0.19.0
GitCommit: de40ad0
[root@Docker01 ~]# docker -v #等同于docker --version
Docker version 20.10.24, build 297e128
五、Ubuntu安装docker环境
1、卸载旧的docker版本
root@docker03:~# for pkg in docker.io docker-doc docker-compose podman-docker containerd runc; do apt-get remove $pkg; done
2、切换国内的软件源
root@docker03:~# cat > /etc/apt/sources.list <<EOF
deb https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
# deb https://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
# deb-src https://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
EOF
3、更新软件源及证书
root@docker03:~# apt-get update
root@docker03:~# apt-get install ca-certificates curl gnupg
4、添加Docker的官方GPG密钥
root@docker03:~# install -m 0755 -d /etc/apt/keyrings
root@docker03:~# curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
File '/etc/apt/keyrings/docker.gpg' exists. Overwrite? (y/N) y
root@docker03:~# chmod a+r /etc/apt/keyrings/docker.gpg
5、配置Ubuntu的docker软件源
root@docker03:~# cat > /etc/apt/sources.list.d/docker.list <<EOF
deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu focal stable
EOF
6、更新软件源
root@docker03:~# apt-get update
7、安装软件源
root@docker03:~# apt-get -y install docker-ce docker-ce-cli containerd.io
8、配置镜像加速(同上)
root@docker03:~# mkdir -p /etc/docker
root@docker03:~# tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://xg3udbel.mirror.aliyuncs.com"]
}
EOF
root@docker03:~# systemctl daemon-reload
root@docker03:~# systemctl restart docker
9、设置开机启动docker服务
root@docker03:~# systemctl enable --now docker
10、验证docker版本
root@docker03:~# docker -v
Docker version 24.0.2, build cb74dfc
六、使用deb安装docker
deb下载链接:https://download.docker.com/linux/ubuntu/dists/focal/pool/stable/amd64/
deb类似于CentOS的rpm包,不再演示。https://download.docker.com/linux/ubuntu/dists/focal/pool/stable/amd64/
七、Ubuntu卸载docker环境
1、卸载软件包
root@docker03:~# apt-get -y purge docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras
2、删除数据目录
root@docker03:~# rm -rf /var/lib/docker /var/lib/containerd
八、二进制安装docker在Linux系统(CentOS,Ubuntu)
1、部署流程
安装就下载解压安装包,卸载的话直接删除软件包即可
1、下载软件包
root@docker03:~# wget https://download.docker.com/linux/static/stable/x86_64/docker-20.10.24.tgz
2、解压软件包
root@docker03:~# mkdir -pv /koten/softwares
mkdir: created directory '/koten'
mkdir: created directory '/koten/softwares'
root@docker03:~# tar xf docker-20.10.24.tgz -C /koten/softwares/
3、创建符号链接到环境变量
root@docker03:~# ln -sv /koten/softwares/docker/* /usr/bin/
'/usr/bin/containerd' -> '/koten/softwares/docker/containerd'
'/usr/bin/containerd-shim' -> '/koten/softwares/docker/containerd-shim'
'/usr/bin/containerd-shim-runc-v2' -> '/koten/softwares/docker/containerd-shim-runc-v2'
'/usr/bin/ctr' -> '/koten/softwares/docker/ctr'
'/usr/bin/docker' -> '/koten/softwares/docker/docker'
'/usr/bin/dockerd' -> '/koten/softwares/docker/dockerd'
'/usr/bin/docker-init' -> '/koten/softwares/docker/docker-init'
'/usr/bin/docker-proxy' -> '/koten/softwares/docker/docker-proxy'
'/usr/bin/runc' -> '/koten/softwares/docker/runc'
4、启动docker服务
root@docker03:~# nohup dockerd &>/koten/softwares/docker/docker.log &
[1] 35332
5、测试docker是否正常
root@docker03:~# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
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.
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/
2、二进制部署脚本
通过运行此脚本,可以实现自动部署docker,可以通过简单修改脚本实现自定义docker版本,注意在centos可以通过sh运行,但是ubuntu需要用bash或者路径运行
[root@Docker00 ~]# mkdir download
[root@Docker00 ~]# mkdir /koten/softwares
[root@Docker01 download]# ls #准备自动补全、镜像加速、开机自启动脚本
daemon.json docker docker.service
[root@Docker00 ~]# cat install-docker.sh
#!/bin/bash
DOCKER_VERSION=19.03.15 #可以自定义docker版本
FILENAME=docker-${DOCKER_VERSION}.tgz
URL=https://download.docker.com/linux/static/stable/x86_64
DOWNLOAD=./download
BASE_DIR=/koten/softwares
# 定义安装函数
function InstallDocker(){
# 判断文件是否存在,若不存在则下载软件包
if [ ! -f ${DOWNLOAD}/${FILENAME} ]; then
wget ${URL}/${FILENAME} -O ${DOWNLOAD}/${FILENAME}
fi
# 判断解压路径是否存在
if [ ! -d ${BASE_DIR} ]; then
install -d ${BASE_DIR}
fi
# 解压软件包到指定目录
tar xf ${DOWNLOAD}/${FILENAME} -C ${BASE_DIR}
# 创建软连接
ln -svf ${BASE_DIR}/docker/* /usr/bin/
# 自动补全功能
cp $DOWNLOAD/docker /usr/share/bash-completion/completions/docker
source /usr/share/bash-completion/completions/docker
# 配置镜像加速
install -d /etc/docker
cp $DOWNLOAD/daemon.json /etc/docker/daemon.json
# 开机自启动脚本
cp download/docker.service /usr/lib/systemd/system/docker.service
systemctl daemon-reload
systemctl enable --now docker
docker version
tput setaf 3
echo "安装成功!"
tput setaf 2
}
# 卸载docker
function UninstallDocker(){
# 停止docker服务
systemctl disable --now docker
# 卸载启动脚本
rm -f /usr/lib/systemd/system/docker.service
# 清空程序目录
rm -rf ${BASE_DIR}/docker
# 清空数据目录
rm -rf /var/lib/{docker,containerd}
# 清除符号链接
rm -f /usr/bin/{containerd,containerd-shim,containerd-shim-runc-v2,ctr,docker,dockerd,docker-init,docker-proxy,runc}
# 使得终端变粉色
tput setaf 5
echo "卸载成功!"
tput setaf 7
}
# 程序的入口函数
function main(){
# 判断传递的参数
case $1 in
"install")
InstallDocker
;;
"remove")
UninstallDocker
;;
*)
echo "Invalid parameter, Usage: $0 install|remove"
;;
esac
}
# 向入口函数传参
main $1
[root@Docker00 ~]# sh deploy.sh install
[root@Docker00 ~]# docker -v
Docker version 19.03.15, build 99e3ed8
Docker镜像管理基础
一、查看镜像
查看现有的镜像列表
root@docker03:~# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 20 months ago 13.3kB
root@docker03:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 20 months ago 13.3kB
只查看镜像的ID
root@docker03:~# docker image ls -q
feb5d9fea6a5
二、给镜像打标签
root@docker03:~# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 20 months ago 13.3kB
root@docker03:~# docker image tag hello-world koten-hello-world:v1.1
root@docker03:~# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 20 months ago 13.3kB
koten-hello-world v1.1 feb5d9fea6a5 20 months ago 13.3kB
三、本地镜像迁移(导入导出)
导出镜像到一个tar包的两种方式
root@docker03:~# docker image save koten-hello-world:v1.1 > docker-koten-hello-world.tar.gz
root@docker03:~# docker image save --output docker-koten-hello-world2.tar.gz koten-hello-world:v1.1
root@docker03:~# ls
docker-koten-hello-world2.tar.gz
docker-koten-hello-world.tar.gz
从一个tar包导入镜像的两种方式
root@docker03:~# scp *.tar.gz 10.0.0.201:/root/
[root@Docker01 ~]# docker image load < docker-koten-hello-world.tar.gz
e07ee1baac5f: Loading layer 14.85kB/14.85kB
Loaded image: koten-hello-world:v1.1
[root@Docker01 ~]# docker image load -i docker-koten-hello-world2.tar.gz
Loaded image: koten-hello-world:v1.1
[root@Docker01 ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
koten-hello-world v1.1 feb5d9fea6a5 20 months ago 13.3kB
镜像批量导入
for i in `ls *.tar.gz`; do docker load -i $i ;done
镜像批量导出
docker image save -o xxx.tar.gz `docker images|awk 'NR>1{print $1":"$2}'`
忘记NR可以大于1了,老想着awk不能变量,还用了for循环,唉,三剑客还得练!!!
四、下载镜像
下载一个nginx版本为1.14.2的镜像
root@docker03:~# docker pull nginx:1.14.2
1.14.2: Pulling from library/nginx
27833a3ba0a5: Pull complete
0f23e58bd0b7: Pull complete
8ca774778e85: Pull complete
Digest: sha256:f7988fb6c02e0ce69257d9bd9cf37ae20a60f1df7563c3a2a6abe24160306b8d
Status: Downloaded newer image for nginx:1.14.2
docker.io/library/nginx:1.14.2
下载一个mysql版本号为8.0的镜像
root@docker03:~# docker pull mysql:8.0
下载一个tomcat9的镜像
root@docker03:~# docker pull tomcat:9
也可以去第三方镜像仓库下载镜像
root@docker03:~# docker pull docker.elastic.co/elasticsearch/elasticsearch:7.17.5
五、删除镜像
删除指定的镜像的名称
root@docker03:~# docker image rm hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
基于镜像ID强制删除镜像,尽管该镜像被其他镜像或容器引用
root@docker03:~# docker image rm -f feb5d9fea6a5
Untagged: koten-hello-world:v1.1
Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
删除本地的所有镜像
root@docker03:~# docker image rm -f `docker images -q`
Untagged: nginx:1.14.2
Untagged: nginx@sha256:f7988fb6c02e0ce69257d9bd9cf37ae20a60f1df7563c3a2a6abe24160306b8d
Deleted: sha256:295c7be079025306c4f1d65997fcf7adb411c88f139ad1d34b537164aa060369
Deleted: sha256:19606512dfe192788a55d7c1efb9ec02041b4e318587632f755c5112f927e0e3
Deleted: sha256:0b83495b3ad3db8663870c3babeba503a35740537a5e25acdf61ce6a8bdad06f
Deleted: sha256:5dacd731af1b0386ead06c8b1feff9f65d9e0bdfec032d2cd0bc03690698feda
root@docker03:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
可以把删除本地的所有镜像这种常用的命令写入Xshell会话的快速命令
如果勾选追加CR,点击后就会输入命令会自动回车,如果不勾选就只会输入命令停留,所以新建危险的快速命令不推荐勾选
添加后快速命令栏会显示,使用时记得命令行中不要有其他命令,因为快速命令是在当前命令后面追加并不是替换
点击以下即可自动运行
六、自定义镜像
自己做个镜像出来,后面文章再写!!
容器管理常用命令
一、查看容器
查看现有的容器列表
root@docker03:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
查看所有容器运行的所有状态
root@docker03:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a027a219bddc hello-world "/hello" 31 seconds ago Exited (0) 30 seconds ago pensive_villani
6351a82d0715 hello-world "/hello" 24 minutes ago Exited (0) 24 minutes ago wizardly_bardeen
查看最新的容器id
root@docker03:~# docker ps -ql
6351a82d0715
查看最新的一个容器创建的信息,无论该容器处于什么状态均可查看
root@docker03:~# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a027a219bddc hello-world "/hello" 44 seconds ago Exited (0) 43 seconds ago pensive_villani
二、运行容器
将容器放在前台运行
[root@Docker01 ~]# docker run nginx:1.14.2
[root@Docker01 ~]# docker container inspect `docker container ps -lq` #查看容器IP地址为172.17.0.3
[root@Docker01 ~]# curl 172.17.0.3
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@Docker01 ~]# docker run nginx:1.14.2
172.17.0.1 - - [06/Jun/2023:13:54:12 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
......
将容器放在后台运行
[root@Docker01 ~]# docker run -d nginx:1.14.2
交互式运行
#-i表示交互式,有了-i才不会闪退,-t表示分配终端,有了-t才是横着显示命令行
[root@Docker01 ~]# docker container run -i -t centos:7
Unable to find image 'centos:7' locally
7: Pulling from library/centos
2d473b07cdd5: Pull complete
Digest: sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987
Status: Downloaded newer image for centos:7
[root@e7ea9bc74cdd /]# ls /
anaconda-post.log etc lib64 opt run sys var
bin home media proc sbin tmp
dev lib mnt root srv usr
[root@e7ea9bc74cdd /]# pwd
/
[root@e7ea9bc74cdd /]# exit
exit
[root@Docker01 ~]#
给容器起名字
[root@Docker01 ~]# docker container run -itd --name linux centos:7
1c9b80bcb071c2ad342ac9c6744c6b03a688870fd0561608e4cd269fcfe8fa3c
[root@Docker01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1c9b80bcb071 centos:7 "/bin/bash" 36 seconds ago Up 35 seconds linux
-e表示向容器传递环境变量
[root@Docker01 ~]# docker run -itd -e NAME=koten -e HOBBY=docker --name linux01 centos:7
b04b31e444a4e14f05722a6151f0421fa2740f788435adea9adc05738c3f3ed
[root@Docker01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b04b31e444a4 centos:7 "/bin/bash" 45 seconds ago Up 44 seconds linux01
1c9b80bcb071 centos:7 "/bin/bash" 2 minutes ago Up 2 minutes linux
可以不进入容器,也可以在外面执行,类似于ssh执行命令,也类似于不进入mysql,在mysql外面执行命令
[root@Docker01 ~]# docker run centos:7 env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=98c175050f7b
HOME=/root
将nginx容器的80端口通过10.0.0.201网卡暴露在外面,底层是iptables做的地址映射
[root@Docker01 ~]# docker run -d -p 10.0.0.201:80:80 nginx:1.14.2
2f6a69025cf3abe92232b4da3b893dd5b9e4a3f51ad9708be61cd8572fd6163b
三、连接到指定的容器
[root@Docker01 ~]# docker container exec -it linux bash
[root@1c9b80bcb071 /]# exit
exit
[root@Docker01 ~]#
四、删除所有的容器
与上面删除所有的镜像一样,也可以加入快速命令
docker container rm -f `docker container ps -qa`
五、查看容器的IP地址,查看IPAddress字段即可
# docker container inspect 容器名称|容器的ID
[root@Docker01 ~]# docker container inspect `docker container ps -lq`
......
"NetworkSettings": {
"Bridge": "",
"SandboxID": "830197d273f10fced77e650b41b2159ec7bebcc49d2acaa8c885b48c30e76434",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"80/tcp": null
},
"SandboxKey": "/var/run/docker/netns/830197d273f1",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "a6b3bb8e725ed536a98b009e36a25a2718b17dc17d1882170cbceea56bcb373c",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:03",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "b5012c44124488b1cb84aee6927b4a040e8a6db82b09096991518bec79c74cf9",
"EndpointID": "a6b3bb8e725ed536a98b009e36a25a2718b17dc17d1882170cbceea56bcb373c",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:03",
"DriverOpts": null
}
}
}
}
]
六、案例:使用docker部署ES节点
参考连接:https://www.elastic.co/guide/en/elasticsearch/reference/7.17/docker.html
下载镜像
docker pull docker.elastic.co/elascsearch/elasticsearch:7.17.5
启动ES服务
docker run -d --name koten-es7 -p 9200:9200 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:7.17.5
扩展
一、搭建私有的yum仓库,实现docker的私有仓库部署,要求可以指定版本
可以参考我之前写的文章:【运维知识进阶篇】一键部署yum本地仓库_一键安装yum-CSDN博客
真正的yum仓库有两点要求,一个是可以安装任意版本,一个是可以解决依赖问题。
我们可以通过运行一个脚本自动部署本地yum仓库,但是考虑到是部署docker的yum仓库,所以我们需要增加docker的rpm包在我们的仓库,其他的软件可以不需要。
1、配置虚拟机
2、下载docker的rpm包到/Packages目录,还有一些依赖包和docker补全的包
[root@Docker00 ~]# mkdir /Packages/
[root@Docker00 ~]# cd /Packages/
[root@Docker00 Packages]# wget -r -l 1 -nd -nc -np -A rpm https://download.docker.com/linux/centos/7/x86_64/stable/Packages/ #下载该网址下的所有rpm包
[root@Docker00 test]# ls /Packages/
......
docker-scan-plugin-0.21.0-3.el7.x86_64.rpm
docker-scan-plugin-0.23.0-1.el7.x86_64.rpm
docker-scan-plugin-0.23.0-3.el7.x86_64.rpm
docker-scan-plugin-0.7.0-3.el7.x86_64.rpm
docker-scan-plugin-0.8.0-3.el7.x86_64.rpm
docker-scan-plugin-0.9.0-3.el7.x86_64.rpm
[root@Docker00 ~]# ls /Packages|wc -l
259
3、编写脚本并执行
[root@Docker00 ~]# cat deploy_docker.sh
#!/bin/bash
# 安装epel源
yum -y install epel-release
# 安装nginx
yum -y install nginx
# 创建目录,准备Nginx默认站点
mkdir /share
chown -R nginx:nginx /share
# 删除Nginx默认站点,并在其目录中创建yum.conf文件进行yum仓库目录索引
rm /etc/nginx/conf.d/default.conf
cat > /etc/nginx/conf.d/yum.conf <<EOF
server {
listen 80 default_server;
root /share;
index index.html index.htm;
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;
}
EOF
# 准备yum仓库存储目录
mkdir /share/packages
chown -R nginx:nginx /share/packages
# 安装createrepo
yum -y install createrepo
# 复制rpm包到本地yum仓库
cp -a /Packages/* /share/packages/
# 构建yum仓库
createrepo /share/packages/
# 启动nginx服务,并将服务加入开机启动项中
systemctl start nginx
systemctl enable nginx
[root@Docker00 ~]# sh deploy_docker.sh
......
Spawning worker 0 with 259 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete
4、浏览器访问,可以看到我们的rpm包
5、客户端配置yum源,使用本地yum仓库测试安装指定版本
[root@Docker02 ~]# cat /etc/yum.repos.d/docker_local.repo
[local]
name=docker_local
baseurl=http://10.0.0.200/packages
enabled=1
[root@Docker04 ~]# yum -y install docker-ce-24.0.0 docker-ce-cli-24.0.0 containerd.io
......
Dependencies Resolved
=====================================================
Package Arch Version Repository
Size
=====================================================
Installing:
containerd.io x86_64 1.6.21-3.1.el7 local 34 M
docker-ce x86_64 3:24.0.0-1.el7 local 24 M
docker-ce-cli x86_64 1:24.0.0-1.el7 local 13 M
Installing for dependencies:
docker-buildx-plugin
x86_64 0.10.5-1.el7 local 12 M
docker-ce-rootless-extras
x86_64 24.0.2-1.el7 local 9.1 M
docker-compose-plugin
x86_64 2.18.1-1.el7 local 12 M
Transaction Summary
=====================================================
......
Complete!
二、在windows上安装docker环境
其实windows没有办法用docker,能用是因为安装了Linux内核,注意安装环境和后续使用都需要开启Hyper-V,在控制面板,程序和功能,启用或关闭windows功能。开启后需要重启windows电脑
注意开启它可能会影响VMware的正常使用,所以用VM虚拟机的使用记得关闭它哈,docker在windows上用的少,可能打镜像包会用到,主要还是在linux上用,打镜像包可以把代码传到自己虚拟机上,在虚拟机上再拉下来,这样速度也很快
之前镜像加速器的网址有在windows安装docker的方法,可以作为参考
打开这个软件,修改安装路径,一路next即可
也可以直接在docker官网找到docker的windows版本下载!
Docker-24.0.2-rpm包下载链接:https://pan.baidu.com/s/1qAzlLiSQW59Hhsq5GUaW-A?pwd=2uox
我是koten,10年运维经验,持续分享运维干货,感谢大家的阅读和关注!