docker使用
:
本文在Windows系统下开启了wsl(Windows Subsystem for Linux),安装了Ubuntu 20.04 LTS作为Docker的运行环境
1 安装使用
1.1 运行环境
Linux配置:参考安装 WSL | Microsoft Learn
如果你成功的配置了wsl系统,在Windows终端中键入如下命令进入Linux环境
# 开启
wsl
# 关闭,不关闭的话可能会占用较高内存
wsl --shutdown
# 查看当前各个安装的Linux发行版的运行情况
wsl -l -v
查看当前环境
docker安装:非桌面版Install Docker Engine on Ubuntu | Docker Documentation
# 查看docker安装情况
docker info
# 如果发现docker服务器未能连接,则开启docker服务器
service docker start
# 关闭方式
service docker stop
# 其他应用可以通过类似命令开启和关闭服务
service mysql start
service mysql stop
service redis-server start
service redis-server stop
如果不熟悉bash命令,推荐Bash/Shell Courses & Tutorials | Codecademy进行学习,或者基础命令查找使用Shell脚本
1.2 运行一个容器
启动一个ubuntu容器,并运行这个容器的/bin/bash程序
# -i interactive: input with the keyboard
# -t tty: output to the terminal
docker run -i -t ubuntu /bin/bash
# 退出
exit
如果本地没有ubuntu容器,Docker会执行docker pull ubuntu
自动拉取registry上的ubuntu image
删除这个镜像
# 查看所有本地镜像
docker images
# 删除
docker rmi <name+tag>
# 如果该镜像正被容器引用,则删除该引用容器
docker rm <containerID>
如果遇到容器正在运行当中,则需要停止再删除(或强制删除)
# 查看容器状态
# -a all
docker ps -a
# 查看运行中的容器ID
# -q quiet only display container IDs
docker ps -q
# 停止
docker stop <containerID>
# 强制终止
docker kill <containerID>
1.3 容器化一个应用
1.3.1 获取app
git clone https://github.com/docker/getting-started.git
1.3.2 构建容器镜像
创建Dockerfile
# 在app目录下创建Dockerfile
cd getting-started/app/
touch Dockerfile
添加内容(了解2.3 Dockerfile)
# syntax=docker/dockerfile:1
FROM node:18-alpine
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]
EXPOSE 3000
通过Dockerfile构建一个新的容器镜像
# -t tag: named the image getting-started
# it should look for the Dockerfile in the current directory.
docker build -t getting-started .
1.3.3 启动容器
我们已经成功构建自己的镜像
在容器中运行它
# -d detached: run in the background
# -p port: create a mapping between the host’s port 3000 to the container’s port 3000
docker run -dp 3000:3000 getting-started
打开网址http://localhost:3000,就可以查看自己运行起来的app了
1.3.4 更新源码
更新src/static/js/app.js
文件第56行
- <p className="text-center">No items yet! Add one above!</p>
+ <p className="text-center">You have no todo items yet! Add one above!</p>
重新构建、运行
docker build -t getting-started .
docker run -dp 3000:3000 getting-started
旧容器还在运行中,新容器使用旧的端口号3000,导致报错
# 移除旧容器
docker stop <containerID>
docker rm <containerID>
# 或者直接强制删除
# -f force
docker rm -f <containerID>
重新运行
刷新http://localhost:3000就可以看到我们的更新
1.3.5 分享镜像给别人
使用Registry分享镜像(如果你还没有的话,注册Docker Hub账户),创建仓库getting-started,命令行登录
docker login -u <your_account_name>
更改getting-started镜像的tag
docker tag getting-started your_account_name/getting-started
上传getting-started镜像到仓库
docker push your_account_name/getting-started
大家就可以使用你刚刚上传的镜像啦!
也可以在Play with Docker中试试!
docker run -dp 3000:3000 your_account_name/getting-started
1.3.6 容器数据持久化
1)容器的文件系统
The container's filesystem
Each container starts from the image definition each time it starts. While containers can create, update, and delete files, those changes are lost when you remove the container and Docker isolates all changes to that container.
启动一个Ubuntu容器
# shuf -i 1-10000 -n 1 -o /data.txt
# 随机写入一个1-10000范围的数字到data.txt文件
# tail -f /dev/null"
# 监视一个文件(使该容器保持运行)
docker run -d ubuntu bash -c "shuf -i 1-10000 -n 1 -o /data.txt && tail -f /dev/null"
访问正在运行的这个容器
docker exec <container-id> cat /data.txt
启动另一个Ubuntu容器,查看/
目录,发现并没有data.txt文件
docker run -it ubuntu ls /
2)数据卷
Container volumes
Volumes provide the ability to connect specific filesystem paths of the container back to the host machine.
If you mount a directory in the container, changes in that directory are also seen on the host machine. If you mount that same directory across container restarts, you’d see the same files.
创建一个数据卷,挂载它,并将容器数据存放在该目录下,实现容器数据持久化
# Create a volume
docker volume create todo-db
# --mount: specify a volume mount
docker run -dp 3000:3000 --mount type=volume,src=todo-db,target=/etc/todos getting-started
增加一些todo list)进行测试
删除该容器,启动一个新容器进行查看,发现数据内容一致
查看Docker将volume存放在哪儿
docker volume inspect <volume_name>
1.4 使用过程中可能出现的问题
WARNING: No blkio throttle.read_bps_device support
- 场景:运行命令
sudo docker into
查看安装好的docker信息 - 报错:Warning
- 原因:了解blkio
- 解决:不需要解决此警告
Cannot create /tmp/remote-wsl-loc.txt: Permission denied
- 场景:运行命令
code .
打开vscode - 报错:
- 原因:曾经用root用户执行
code
命令,创建了文件"/tmp/remote-wsl-loc.txt",普通用户使用时权限不够 - 解决:可以更改文件权限,也可以直接删除该文件,然后重新在普通用户环境执行
code .
sudo rm -f /tmp/remote-wsl-loc.txt
code .
ERROR: failed to solve: process "/bin/sh -c yarn install --production" did not complete successfully: exit code: 1
- 运行新容器的构建命令
docker build -t getting-started .
- 报错
- 原因:向上查看报错信息,发现python下载失败,应该是gitbub网站访问失败导致的
- 解决:想办法访问到github
2 Doceker架构
Docker,C/S架构
客户端(docker),可以是命令行(本文选择),也可以是Docker Desktop。
服务端(dockered),一个守护程序,响应客户端的请求,跟踪容器的生命周期。
Registry,Docker Hub是Docker用于映像管理的默认公共注册表,也可以创建和使用专用的Docker注册表
When you use Docker, you are creating and using images, containers, networks, volumes, plugins, and other objects.
Docker是一个开源的引擎,可以轻松的为任何应用创建一个轻量级的、可移植的、自给自足的容器
2.1 镜像
An image is a read-only template with instructions for creating a Docker container.
镜像,是一个只读模板,含创建Docker容器的说明,它与操作系统的安装光盘有点像。(镜像是一个特殊的文件系统,除了提供容器运行时所需的程序、库、资源、配置等文件外,还包含了一些为运行时准备的配置参数)
2.2 容器
A container is a runnable instance of an image.
A container is a sandboxed process on your machine that is isolated from all other processes on the host machine.
容器是一种沙盒技术,主要目的是为了将应用运行在其中,与外界隔离
2.3 Dockerfile
To build your own image, you create a Dockerfile with a simple syntax for defining the steps needed to create the image and run it.
Each instruction in a Dockerfile creates a layer in the image. When you change the Dockerfile and rebuild the image, only those layers which have changed are rebuilt.
This is part of what makes images so lightweight, small, and fast, when compared to other virtualization technologies.
一个Dockerfile示例:
# syntax=docker/dockerfile:1
FROM node:18-alpine
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]
EXPOSE 3000
只有第一行可以指定解析器,类似shebang
# syntax=[remote image reference] : defines the location of the Dockerfile syntax that is used to build the Dockerfile.
# escape=\ : defines the escape characters
The FROM
instruction specifies the Parent Image from which you are building.
FROM [--platform=<platform>] <image>[:<tag>] [AS <name>]
The WORKDIR
instruction sets the working directory for any RUN
, CMD
, ENTRYPOINT
, COPY
and ADD
instructions that follow it in the Dockerfile.
WORKDIR /path/to/workdir
The COPY
instruction copies new files or directories from <src>
and adds them to the filesystem of the container at the path <dest>
.
COPY [--chown=<user>:<group>] [--chmod=<perms>] <src>... <dest>
The RUN
instruction will execute any commands in a new layer on top of the current image and commit the results.The resulting committed image will be used for the next step in the Dockerfile.
RUN <command>
The main purpose of a CMD
is to provide defaults for an executing container.
There can only be one CMD
instruction in a Dockerfile. If you list more than one CMD
then only the last CMD
will take effect.
CMD ["executable","param1","param2"]
The EXPOSE
instruction informs Docker that the container listens on the specified network ports at runtime. You can specify whether the port listens on TCP or UDP, and the default is TCP if the protocol is not specified.
EXPOSE <port> [<port>/<protocol>...]
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)