Windows Docker笔记-在容器中运行项目

devtools/2025/2/8 13:18:53/

在文章《Windows Docker笔记-Docker容器操作》中,已经成功创建了容器,也就是建好了工厂,接下来就应该要安装流水线设备,即运行项目达到生产的目的。

在Ubuntu容器中新建项目

这里要新建一个简单的C++项目,步骤如下:

1. 安装g++编译环境,

    1. 使用 docker start myUbuntu:启动myUbuntu容器
    1. 使用 docker ps -a:查看容器运行状态
    1. 使用 docker exec -it myUbuntu /bin/bash:进入容器终端
    1. 使用 apt-get update:更新软件包索引
    1. 使用 apt-get install g++:安装g++软件包
    1. 使用 g++ --version:查看安装的g++版本
D:\本周未完成工作\docker>docker start myUbuntu
myUbuntuD:\本周未完成工作\docker>docker ps -a
CONTAINER ID   IMAGE           COMMAND       CREATED        STATUS         PORTS     NAMES
da3b54c08bd5   ubuntu:latest   "/bin/bash"   18 hours ago   Up 3 seconds             myUbuntuD:\本周未完成工作\docker>docker exec -it myUbuntu /bin/bash
root@da3b54c08bd5:/# apt-get update
Get:1 http://archive.ubuntu.com/ubuntu noble InRelease [256 kB]
Get:2 http://security.ubuntu.com/ubuntu noble-security InRelease [126 kB]
>>>>>>>>>>>>>[此处省略部分过程代码]<<<<<<<<<<<<<<
Fetched 28.1 MB in 19s (1487 kB/s)
Reading package lists... Done
root@da3b54c08bd5:/# apt-get install g++
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
>>>>>>>>>>>>>[此处省略部分过程代码]<<<<<<<<<<<<<<
The following NEW packages will be upgraded:g++ g++-13 g++-13-x86-64-linux-gnu g++-x86-64-linux-gnu gcc libstdc++-13-dev
0 upgraded, 6 newly installed, 0 to remove and 12 not upgraded.
Need to get 14.6 MB of archives.
After this operation, 56.6 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
>>>>>>>>>>>>>[此处省略部分过程代码]<<<<<<<<<<<<<<
Setting up gcc (4:13.2.0-7ubuntu1) ...
Setting up libstdc++-13-dev:amd64 (13.3.0-6ubuntu2~24.04) ...
Setting up g++-13-x86-64-linux-gnu (13.3.0-6ubuntu2~24.04) ...
Setting up g++-x86-64-linux-gnu (4:13.2.0-7ubuntu1) ...
Setting up g++-13 (13.3.0-6ubuntu2~24.04) ...
Setting up g++ (4:13.2.0-7ubuntu1) ...
update-alternatives: using /usr/bin/g++ to provide /usr/bin/c++ (c++) in auto mode
update-alternatives: warning: skip creation of /usr/share/man/man1/c++.1.gz because associated file /usr/share/man/man1/g++.1.gz (of link group c++) doesn't exist
root@da3b54c08bd5:/# g++ --version
g++ (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.root@da3b54c08bd5:/#

到这里 g++ 编译环境就安装好了。

2. 安装vim

vim用来编辑代码文件使用。

root@da3b54c08bd5:/# apt-get install vim
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
>>>>>>>>>>>>>[此处省略部分过程代码]<<<<<<<<<<<<<<
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vim (vim) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vimdiff (vimdiff) in auto mode
Processing triggers for libc-bin (2.39-0ubuntu8.4) ...
root@da3b54c08bd5:/#vim --version
VIM - Vi IMproved 9.1 (2024 Jan 02, compiled Jan 16 2025 20:13:18)
Included patches: 1-16, 647, 678, 697
Modified by team+vim@tracker.debian.org
Compiled by team+vim@tracker.debian.org
>>>>>>>>>>>>>[此处省略部分过程代码]<<<<<<<<<<<<<<

到这里 vim 编辑工具就安装好了。

3. 新建项目

这里的项目,直接使用文章《CMake项目中神器:CMakeLists.txt》中的第一个项目代码,如下图所示。

#include <iostream>int Func_Add(int num1, int num2)
{int nSum = num1 + num2;return nSum;
}int main()
{int numSum = Func_Add(2, 3);std::cout << "2 + 3 = " << numSum << std::endl;return 0;
}

步骤如下:

    1. 在/home/目录下新建test目录
    1. 在test目录中使用vim main.cpp:新建main.cpp文件
    1. 将上述代码编辑到main.cpp中
    1. 保存并退出文件
root@da3b54c08bd5:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@da3b54c08bd5:/# cd /home/
root@da3b54c08bd5:/home# ls
ubuntu
root@da3b54c08bd5:/home# mkdir test
root@da3b54c08bd5:/home# cd test/
root@da3b54c08bd5:/home/test# vim main.cpp
root@da3b54c08bd5:/home/test# cat main.cpp
#include <iostream>int Func_Add(int num1, int num2)
{int nSum = num1 + num2;return nSum;
}int main()
{int numSum = Func_Add(2, 3);std::cout << "2 + 3 = " << numSum << std::endl;return 0;
}
root@da3b54c08bd5:/home/test#

4. 编译项目并运行

    1. 使用g++命令编译项目文件
    1. 使用./可执行文件名 运行文件
root@da3b54c08bd5:/home/test# g++ -o main main.cpp
root@da3b54c08bd5:/home/test# ls
main  main.cpp
root@da3b54c08bd5:/home/test# ./main
2 + 3 = 5
root@da3b54c08bd5:/home/test#

代码此时已经正常运行了。运行的程序就相当于流水线设备,运行结果,就相当于到生产的玩具。

上述项目只是一个简单的例子,你可以把这个Ubuntu的容器当成一个Linux的开发环境来使用,在里面编译你实际的项目代码,然后运行。比使用虚拟机要好很多。非常的Nice

使用外部现有程序放到容器中运行

类比一下,上面的容器中新建项目运行相当于,工厂自己生产流水线设备,自给自足。另外一种情况是直接购买流水线设备,安装到工厂后直接运行。这种就相当于,将外部已经编译好的程序放到容器中运行一样。显然,docker也提供了这种方法,方便运维,这样容器就只提供运行环境就好,就不用提供编译开发环境,轻量了很多。

注意:以下操作需要先使用 e x i t 命令退出容器,在容器外部操作。 \color{red}{注意:以下操作需要先使用exit命令退出容器,在容器外部操作。} 注意:以下操作需要先使用exit命令退出容器,在容器外部操作。

这里文件演示,需要将上述生成的可执行文件 main 先保存到本地磁盘,然后删除到容器中的项目,在将本地可执行文件 main 导入容器中运行。

docker_cp__151">1. docker cp 命令说明

命令:docker cp 是 Docker 的一个命令,用于在容器和主机之间复制文件或文件夹。它类似于 Linux 的 cp 命令,但专门用于操作 Docker 容器中的文件。
格式:
docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH
docker cp [OPTIONS] SRC_PATH CONTAINER:DEST_PATH

  • CONTAINER:SRC_PATH:容器内的源文件路径。
  • DEST_PATH:主机上的目标路径。
  • SRC_PATH:主机上的源文件路径。
  • CONTAINER:DEST_PATH:容器内的目标路径。

常见用法

  1. 从容器复制文件到主机
    将容器中的文件复制到主机的指定路径:
    命令:docker cp <container_id>:/path/to/source/file /path/to/destination/on/host
    案例:docker cp my_container:/app/config.txt /home/user/config_backup.txt
  2. 从主机复制文件到容器
    将主机上的文件复制到容器的指定路径:
    命令:docker cp /path/to/source/file <container_id>:/path/to/destination/in/container
    案例:docker cp /home/user/new_config.txt my_container:/app/config.txt

1. 将编译好的可执行文件 main 保存到本地磁盘中

命令:docker cp myUbuntu:/home/test/main ./ # ./表示本地当前目录

root@da3b54c08bd5:/home/test# exit
exitD:\本周未完成工作\docker>docker ps -a
CONTAINER ID   IMAGE           COMMAND       CREATED        STATUS       PORTS     NAMES
da3b54c08bd5   ubuntu:latest   "/bin/bash"   20 hours ago   Up 2 hours             myUbuntuD:\本周未完成工作\docker>docker cp myUbuntu:/home/test/main ./
Successfully copied 18.4kB to D:\本周未完成工作\docker\.\D:\本周未完成工作\docker>dir驱动器 D 中的卷是 Data卷的序列号是 F6F0-A014D:\本周未完成工作\docker 的目录2025/02/07  15:39    <DIR>          .
2025/02/07  15:39    <DIR>          ..
2025/02/07  10:54            16,584 main
2025/01/20  17:34        75,203,584 ubuntu.tar2 个文件     75,220,168 字节2 个目录 382,717,251,584 可用字节D:\本周未完成工作\docker>

上述,退出容器后,使用docker cp 将容器中的main可执行文件保存到了本地目录:D:\本周未完成工作\docker 下面。

2. 清理容器中的项目相关文件

D:\本周未完成工作\docker>docker exec -it myUbuntu /bin/bash
root@da3b54c08bd5:/# cd /home/test/
root@da3b54c08bd5:/home/test# rm -rf *
root@da3b54c08bd5:/home/test# ls
root@da3b54c08bd5:/home/test#

3. 将本地编译好的可执行文件 main 导入容器中运行

命令:docker cp ./main myUbuntu:/home/test/

root@da3b54c08bd5:/home/test# exit
exitD:\本周未完成工作\docker>docker cp ./main myUbuntu:/home/test/
Successfully copied 18.4kB to myUbuntu:/home/test/D:\本周未完成工作\docker>docker exec -it myUbuntu /bin/bash
root@da3b54c08bd5:/# cd /home/test/
root@da3b54c08bd5:/home/test# ls
main
root@da3b54c08bd5:/home/test# ./main
2 + 3 = 5
root@da3b54c08bd5:/home/test#

可以看到,容器中有了运行环境,直接使用外部导入的程序,就可以运行了。

如果,项目较大,需要的以来很多,可以导入程序后,运行调试,根据报错信息安装依赖包即可。


上一章:Windows Docker笔记-Docker容器操作
下一章:Windows Docker笔记-制作、加载镜像


http://www.ppmy.cn/devtools/157097.html

相关文章

【开源免费】基于SpringBoot+Vue.JS智能学习平台系统(JAVA毕业设计)

本文项目编号 T 181 &#xff0c;文末自助获取源码 \color{red}{T181&#xff0c;文末自助获取源码} T181&#xff0c;文末自助获取源码 目录 一、系统介绍二、数据库设计三、配套教程3.1 启动教程3.2 讲解视频3.3 二次开发教程 四、功能截图五、文案资料5.1 选题背景5.2 国内…

python爬虫--简单登录

1&#xff0c;使用flask框架搭建一个简易网站 后端代码app.py from flask import Flask, render_template, request, redirect, url_for, sessionapp Flask(__name__) app.secret_key 123456789 # 用于加密会话数据# 模拟用户数据库 users {user1: {password: password1}…

[ESP32:Vscode+PlatformIO]添加第三方库 开源库 与Arduino导入第三方库的区别

前言 PlatformIO与Arduino在添加第三方库方面的原理存在显著差异 在PlatformIO中&#xff0c;第三方库的使用是基于项目&#xff08;工程&#xff09;的。具体而言&#xff0c;只有当你为一个特定的项目添加了某个第三方库后&#xff0c;该项目才能使用该库。这些第三方库的文…

【C++】string类的模拟实现

文章目录 Ⅰ. string类的介绍以及一些常见问题Ⅱ. string类的模拟实现类的整体框架&#xff08;简单的直接在框架实现了&#xff09;构造函数与析构函数&#xff08;重点&#xff09;现代写法的拷贝构造以及赋值运算符重载&#xff08;重点&#xff09;swap 函数reserve 函数re…

确保数据一致性:RabbitMQ 消息传递中的丢失与重复问题详解

前言 RabbitMQ 是一个常用的消息队列工具&#xff0c;虽然它能帮助高并发环境下实现高效协同&#xff0c;但我们也曾遇到过因网络波动、确认机制失效、系统故障和代码异常等原因导致消息丢失或重复消费的问题&#xff0c;本文将探讨原因及解决方案&#xff0c;希望能为大家提供…

Flutter 完整开发实战详解(二、 快速开发实战篇)_0_10_flutter dio

///页面销毁时&#xff0c;销毁控制器_tabController.dispose();super.dispose(); }override Widget build(BuildContext context) {///底部TAbBar模式return new Scaffold(///设置侧边滑出 drawer&#xff0c;不需要可以不设置drawer: _drawer,///设置悬浮按键&#xff0c;不需…

sourcetree === 使用 Git 工作

目录 从远程存储库 (Git) 提取更改 提交并推送更改 (Git) 创建分支并将其推送到远程存储库 (Git) 将更改从一个分支合并到另一个分支&#xff08;Git&#xff09; 从远程存储库 (Git) 提取更改 如果您的团队中的某个人对远程存储库进行了更改&#xff0c;您希望将这些更改提…

[Day 16]螺旋遍历二维数组

今天我们看一下力扣上的这个题目&#xff1a;146.螺旋遍历二维数组 题目描述&#xff1a; 给定一个二维数组 array&#xff0c;请返回「螺旋遍历」该数组的结果。 螺旋遍历&#xff1a;从左上角开始&#xff0c;按照 向右、向下、向左、向上 的顺序 依次 提取元素&#xff0c…