文章目录
- Docker 的运行环境
- Docker 在 Mac OS的安装(如果启动软件,在终端运行 docker run hello-world 报错,则选择一个低版本的安装)
- Windows 环境的安装
- Docker 在Cent OS 上的安装
- 系统版本要求
- 多种安装方式的选择
- Yum 安装 Docker
- Docker 引擎服务的启动和关闭
- Docker 引擎的卸载
- Docker 更多命令行查看
- docker exec -it xxxx /bin/bash
- 参考资料
Docker 的运行环境
Docker 可以运行在 Linux、Windows 、Mac OS 操作系统上。
Docker 官方建议安装在 Linux 操作系统。
Docker 在 Mac OS的安装(如果启动软件,在终端运行 docker run hello-world 报错,则选择一个低版本的安装)
/ => /get-docker/
- 配置Docker-Engine 的 registry-mirrors ,进行镜像加速
{
"builder": {
"features": {
"buildkit": true
},
"gc": {
"defaultKeepStorage": "20GB",
"enabled": true
}
},
"experimental": false,
"registry-mirrors": [
"."
]
}
Windows 环境的安装
·安装完毕后需要关联虚拟机:/p/06009c2fa7f2
1、windows环境下有专门的docker工具
可以从阿里云镜像下载安装包:/docker-
toolbox/windows/docker-toolbox/
windows 找不到
Default Boot2Docker ISO is out-of-date-断网即可跳过
This error may also indicate that the docker daemon is not running.
This computer doesn’t have VT-X/AMD-v enabled .Enabling it in the BIOS is mandatory
2、镜像加速
鉴于国内网络问题,后续拉取 Docker 镜像十分缓慢,我们可以需要配置加速器来解决,网易的镜像地址:.
也可以使用阿里云,它会给你提供一个独立的域名
新版的 Docker 使用 /etc/docker/(Linux) 或者 %programdata%\docker\config
\(Windows) 来配置 Daemon。
请在该配置文件中加入(没有该文件的话,请先建一个):
{
"registry-mirrors": ["."]
}
Docker 在Cent OS 上的安装
系统版本要求
- Cent OS 版本需要为 及以上
- Cent OS 需为 64位操作系统
多种安装方式的选择
Docker 有多种安装方式,比如 Yum 安装、RPM 安装、Shell 安装等。
Yum 安装 Docker
注意:需要
root
权限,本文使用root
用户登陆,如非 root 用户请在命令行前添加sudo
前缀。
- 更新yum包
注意:该步骤不是必须的,需要花费一些时间
yum update
- 添加 yum 的 Docker 包仓库
tee /etc// <<-'EOF'
[dockerrepo]
name=Docker Repository
baseurl=/repo/main/centos/7/
enabled=1
gpgcheck=1
gpgkey=/gpg
EOF
- 安装 Docker 引擎
注意:中途有两个确认 输入 y 然后按 Enter:Is this ok [y/d/N]: 和 Is this ok [y/N]:
yum install docker-engine
安装完 Docker 之后,可以查看安装版本!
- 查看 Docker 版本
docker version
Docker 引擎服务的启动和关闭
- 启动 Docker 引擎服务
首次启动自动创建文件夹 /var/lib/docker
/var/lib/docker/image/overlay/ 镜像目录
/var/lib/docker/containers 存储具体镜像
service docker start
- 关闭 Docker 引擎服务
service docker stop
Docker 引擎的卸载
卸载 Docker 引擎不会删除镜像、容器、卷及自定义的配置文件!
- 卸载 Docker 引擎
yum -y remove docker-engine
- 删除镜像、容器、卷及自定义的配置文件
rm -rf /var/lib/docker
Dockerfile 镜像构建参考文档:/engine/reference/builder/?spm=5176.8351553.0.0.40f61991NPF2oT
Docker 更多命令行查看
比如查看 docker 容器日志
docker logs 容器id
比如,查看容器详情
docker inspect 容器id
比如执行命令
docker exec xxx xxx
docker exec -it 容器id /bin/bash //进入容器内部
➜ ~ docker
Usage: docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default "/Users/xxx/.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 "/Users/xxx/.docker/")
--tlscert string Path to TLS certificate file (default "/Users/xxx/.docker/")
--tlskey string Path to TLS key file (default "/Users/xxx/.docker/")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
Management Commands:
builder Manage builds
buildx* Docker Buildx (Docker Inc., v0.10.4)
compose* Docker Compose (Docker Inc., v2.17.2)
config Manage Docker configs
container Manage containers
context Manage contexts
dev* Docker Dev Environments (Docker Inc., v0.1.0)
extension* Manages Docker extensions (Docker Inc., v0.2.19)
image Manage images
init* Creates Docker-related starter files for your project (Docker Inc., v0.1.0-beta.2)
manifest Manage Docker image manifests and manifest lists
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
sbom* View the packaged-based Software Bill Of Materials (SBOM) for an image (Anchore Inc., 0.6.0)
scan* Docker Scan (Docker Inc., v0.25.0)
scout* Command line tool for Docker Scout (Docker Inc., v0.9.0)
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's 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's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's 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
docker exec -it xxxx /bin/bash
docker exec -it xxxx /bin/bash 命令的意思是在名为 xxxx 的容器中启动一个交互式的 Bash shell。这个命令通常用于调试正在运行的容器,或者在容器内部执行临时的任务。
例如,如果你有一个正在运行的容器 my_container,并且想要进入该容器的 shell,你可以这样执行:
sh
docker exec -it my_container /bin/bash
一旦执行这个命令,你会看到 Bash 提示符,并且可以在容器内部执行任何你有权执行的命令。注意,如果你想在容器中永久性地修改文件或设置环境变量等,你可能需要使用具有适当权限的用户来执行这些操作。
如果不加 /bin/bash 可以吗
当然可以。docker exec 命令后面跟随的命令可以是你想要在容器中执行的任何命令。/bin/bash 只是其中一个常见的用例,因为它允许你进入一个交互式的 shell 会话。
如果你不想启动一个新的 shell,而是直接执行某个特定的命令,你可以直接将该命令放在 docker exec 之后。例如:
sh
docker exec -it my_container ls -l
这条命令将会在容器 my_container 中列出当前目录下的文件列表。
示例
假设你想要在容器 my_container 中运行 ls -l 命令来查看目录内容,你可以这样做:
sh
docker exec -it my_container ls -l
如果命令本身不需要交互式输入,那么 -i 和 -t 选项就不一定是必需的。但是,如果你希望命令能够接收输入,比如 cat 命令用来读取管道输入或者 nano 编辑器来编辑文件,那么 -i 和 -t 就是有用的。
更多示例
查看容器中的日志文件:
sh
docker exec -it my_container cat /var/log/syslog
在容器中运行一个 Python 脚本:
sh
docker exec -it my_container python /path/to/
在容器中启动一个服务并保持前台运行:
sh
docker exec -it my_container /usr/sbin/nginx -g 'daemon off;'
总之,docker exec 后面跟随的命令可以根据你的需求来指定,无论是简单的命令还是复杂的脚本都可以。如果你希望获得交互式的 shell 会话,那么加上 /bin/bash 或者容器中安装的其他 shell(如 /bin/sh 或 /bin/zsh)是合适的选择。
参考资料
[1]、