Docker 在 Linux 系统使用

2023年 10月 13日 51.3k 0

Docker教程

github.com/yeasy/docke…

1. 查看系统中是否已经安装好了 docker , 输入 docker -v , 如果没有安装先进行 docker 安装,下面为 docker 17.06 版本

gyw@gyw:~$ docker -v 
Docker version 17.06.0-ce, build 02c1d87

2. 进入项目根目录,运行 docker run -ti --rm --name TestProject -p 8081:8081 -p 5000:5000 -v $(pwd):/workspace sgdockerfilebox/project_cpu bash 命令,进入容器,效果如下

gyw@gyw:~/Desktop/code/project/TestProject$ docker run -ti --rm --name TestProject -p 8081:8081 -p 5000:5000 -v $(pwd):/workspace sgdockerfilebox/project_cpu bash
gg-ai@448a6dc3f155:/$ ls
bin   dev  home  lib64  mnt  proc     root  sbin  srv  tmp  var
boot  etc  lib   media  opt  project  run   src   sys  usr  workspace

3. 进入前端项目根目录,执行 npm run dev 运行前端程序,效果如下

gg-ai@448a6dc3f155:/$ cd workspace/
gg-ai@448a6dc3f155:/workspace$ ls
Dockerfile  Dockerfile_cpu  Makefile.sh  README.TestProject  src
gg-ai@448a6dc3f155:/workspace$ cd src
gg-ai@448a6dc3f155:/workspace/src$ ls
backend  frontend 
gg-ai@448a6dc3f155:/workspace/src$ cd frontend/
gg-ai@448a6dc3f155:/workspace/src/frontend$ npm run dev 

> TestProject@1.0.0 dev /workspace/src/frontend
> node build/dev-server.js

Dev server listening at http://localhost:8081

...    

webpack: Compiled successfully.

4. 重新打开一个命令窗口,执行 docker exec -ti TestProject bash 进入已经启动的容器环境,进入后端根目录,执行 python3 TestProject_server.py 运行后端程序

gyw@gyw:~$ docker exec -ti TestProject bash 
gg-ai@448a6dc3f155:/$ ls
bin   dev  home  lib64  mnt  proc     root  sbin  srv  tmp  var
boot  etc  lib   media  opt  project  run   src   sys  usr  workspace
gg-ai@448a6dc3f155:/$ cd workspace/src/backend/
gg-ai@448a6dc3f155:/workspace/src/backend$ python3 TestProject_server.py 
Using TensorFlow backend.
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)

5. 停止程序,使用快捷键 Ctrl + c ,退出容器,使用 exit ,例如前端退出效果

...
webpack: Compiled successfully.
^C
gg-ai@448a6dc3f155:/workspace/src/frontend$ exit
exit
gyw@gyw:~/Desktop/code/project/TestProject$ 

查看运行的容器列表

docker ps  

查看所有容器,包括已经停止的容器

docker ps -a

移除一个或多个容器

docker rm

删除镜像

docker rmi id 

默认构建当前文件夹下的Dockerfile

docker build -t e2e:v1  . 

搜索镜像

docker search image

启动一个或多个停止的容器

docker start [option] CONTAINER [CONTAINER...]

停止一个或多个运行的容器

docker stop [OPTIONS] CONTAINER [CONTAINER...]

在正在运行的容器中执行命令

docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

以交互形式进入一个运行的容器

docker exec -it container bash

运行一个容器,并进入交互模式

docker run -ti --name wewo -v $(pwd):/workspace sgdockerfilebox/wewo_cpu:v1 bash

进入运行状态的 docker 交互界面

docker attach dockerName 

退出交互界面快捷键,但不停止

ctrl + shift + p + q
ctrl + p + q

退出交互界面,但不停止

exit

将容器的文件系统导出为tar存档

docker export [OPTIONS] CONTAINER

eg.
docker export node-env > node-env-1.tar
docker export --output="node-env-1.tar" node-env

导入 tar 文件创建新镜像

docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]

eg.
docker import node-env-1.tar node-1

运行容器,指定 ip 形式

docker run -it --name frontend -p 23006:23006 -p 8081:8081 -p 23002:23002 -p 3000:3000 -p 23007:23007 -p 23005:23005 -v $(pwd):/workspace 172.16.10.10:5000/sg-ai/node:v9.2.1 bash

docker 修改 repository 名称 docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG] (这里是 tag 复制)

$ docker tag --help 

Usage:	docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

获取容器日志

docker logs CONTAINER

查看 Docker 容器信息,返回 json 格式信息

docker inspect CONTAINER

上传容器到私有仓库 (注意一个协议问题,如果服务器限制http协议上传,那么本次上传到服务器会出现)

docker push 
$ docker tag node:latest 172.16.10.10:5000/node-10.12.0:v1

$ docker images  
REPOSITORY                               TAG                 IMAGE ID            CREATED             SIZE
172.16.10.10:5000/node-10.12.0           v1                  a2b9536415c2        3 weeks ago         674MB

$ docker tag node:latest 172.16.10.10:5000/node-10.12.0:v1

$ docker push 172.16.10.10:5000/node-10.12.0:v1
The push refers to repository [172.16.10.10:5000/node-10.12.0]
0d2922b53ccb: Mounted from node-yarn-1.10.1 
c6af84086075: Mounted from node-yarn-1.10.1 
fae583f1d4e5: Mounted from node-yarn-1.10.1 
9d22e51f0c6d: Mounted from node-yarn-1.10.1 
58b8d417193d: Mounted from node-yarn-1.10.1 
704a8634956f: Mounted from node-yarn-1.10.1 
05f0b6bcfa5c: Mounted from node-yarn-1.10.1 
0972cc82b682: Mounted from node-yarn-1.10.1 
v1: digest: sha256:65b285a4a7a5eae3de7b7fbf935e6b6c2aa529b601a0e96671bd65f4b43a8d72 size: 2007

在服务器部署前端工程出现的问题

在前端工程目录下执行 docker run --rm -v $(pwd):/root/projects_123/frontend node-10.12.0 yarn install 命令后没出现 node_modules 文件夹

[frontend]$ ls
babel.config.js  package.json  package-lock.json  postcss.config.js  public  README.md  src  yarn.lock
[frontend]$ docker run --rm -v $(pwd):/root/projects_123/frontend node-10.12.0 yarn install
yarn install v1.10.1
info No lockfile found.
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Saved lockfile.
Done in 0.08s.
[frontend]$ ls
babel.config.js  package.json  package-lock.json  postcss.config.js  public  README.md  src  yarn.lock

解决方式

使用 -w 参数,指定 yarn install 命名在容器内容的工作目录

[frontend]$ docker run --help

Usage:	docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Run a command in a new container

Options:
  -w, --workdir string                 Working directory inside the container
...
[frontend]$ docker run --rm -v $(pwd):/root/projects_123/frontend -w /root/projects_123/frontend  node-10.12.0:v1 yarn install
yarn install v1.10.1
warning package-lock.json found. Your project contains lock files generated by tools other than Yarn. It is advised not to mix package managers in order to avoid resolution inconsistencies caused by unsynchronized lock files. To clear this warning, remove package-lock.json.
[1/4] Resolving packages...
[2/4] Fetching packages...
info fsevents@1.2.4: The platform "linux" is incompatible with this module.
info "fsevents@1.2.4" is an optional dependency and failed compatibility check. Excluding it from installation.
[3/4] Linking dependencies...
warning "@vue/cli-plugin-babel > babel-loader@8.0.4" has incorrect peer dependency "@babel/core@^7.0.0".
warning "@vue/cli-plugin-babel > babel-loader@8.0.4" has unmet peer dependency "webpack@>=2".
warning "@vue/cli-plugin-eslint > eslint-loader@2.1.1" has unmet peer dependency "webpack@>=2.0.0  workbox-webpack-plugin@3.6.3" has unmet peer dependency "webpack@^2.0.0 || ^3.0.0 || ^4.0.0".
warning "@vue/eslint-config-standard > eslint-config-standard@12.0.0" has unmet peer dependency "eslint@>=5.0.0".
warning "@vue/eslint-config-standard > eslint-config-standard@12.0.0" has incorrect peer dependency "eslint-plugin-node@>=7.0.0".
warning "@vue/eslint-config-standard > eslint-config-standard@12.0.0" has incorrect peer dependency "eslint-plugin-promise@>=4.0.0".
warning "@vue/eslint-config-standard > eslint-config-standard@12.0.0" has incorrect peer dependency "eslint-plugin-standard@>=4.0.0".
warning "@vue/eslint-config-standard > eslint-plugin-import@2.14.0" has unmet peer dependency "eslint@2.x - 5.x".
warning "@vue/eslint-config-standard > eslint-plugin-node@6.0.1" has unmet peer dependency "eslint@>=3.1.0".
warning "@vue/eslint-config-standard > eslint-plugin-standard@3.1.0" has unmet peer dependency "eslint@>=3.19.0".
[4/4] Building fresh packages...
Done in 42.71s.
[frontend]$ ls
babel.config.js  node_modules  package.json  package-lock.json  postcss.config.js  public  README.md  src  yarn.lock

相关文章

服务器端口转发,带你了解服务器端口转发
服务器开放端口,服务器开放端口的步骤
产品推荐:7月受欢迎AI容器镜像来了,有Qwen系列大模型镜像
如何使用 WinGet 下载 Microsoft Store 应用
百度搜索:蓝易云 – 熟悉ubuntu apt-get命令详解
百度搜索:蓝易云 – 域名解析成功但ping不通解决方案

发布评论