window11 下使用docker + vscode 搭建cpp开发环境

原本使用 vscode + vmware + ubuntu 的组合进行linux cpp的学习与开发。无奈在研究其他东西的时候需要一个docker环境,把docker安装到window环境后,出现了两个大问题:

1.idea 和 android studio 用不了了。(端口冲突问题)

2.vmware虚拟机也用不了。(与hyper-v冲突)

把第一个问题解决后,发现有了docker之后就没必要单开一个虚拟机作为cpp开发环境,何不使用更加轻量的docker来替代呢。于是开始一顿踩坑之旅。。

docker环境配置

docker安装步骤省略

背景:

在已有容器添加端口映射,为什么在已经存在的容器上修改呢,而不是将这个容器提交为镜像,然后再新的镜像基础上创建容器的时候配置参数呢。其实按照这种方式尝试解决了一下,无奈生成的镜像跑不起来新的容器。赶紧换思路解决,后期docker把玩熟了之后再回头试试这种方式。

一.修改容器配置文件

1.查看容器列表,找出要修改的容器id

 2.查看容器信息

window11 下使用docker + vscode 搭建cpp开发环境_第1张图片

 根据容器id信息搜索配置文件。

window11 下使用docker + vscode 搭建cpp开发环境_第2张图片

2.修改config.v2.json

添加容器端口配置,导出22端口

"ExposedPorts":{
            "22/tcp":{

            }
        }

window11 下使用docker + vscode 搭建cpp开发环境_第3张图片

 完整文件

{"StreamConfig":{},"State":{"Running":true,"Paused":false,"Restarting":false,"OOMKilled":false,"RemovalInProgress":false,"Dead":false,"Pid":1643,"ExitCode":0,"Error":"","StartedAt":"2022-06-19T06:01:50.9477191Z","FinishedAt":"2022-06-19T06:01:48.7374357Z","Health":null},"ID":"2635168136d879bdc4029a0e78db5d27ac829c1a0bac994053b695ee000da374","Created":"2022-06-19T01:52:21.3346116Z","Managed":false,"Path":"bash","Args":[],"Config":{"Hostname":"2635168136d8","Domainname":"","User":"","AttachStdin":false,"AttachStdout":true,"AttachStderr":true,"ExposedPorts":{"22/tcp":{}},"Tty":true,"OpenStdin":false,"StdinOnce":false,"Env":["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"],"Cmd":["bash"],"Image":"ubuntu:latest","Volumes":null,"WorkingDir":"","Entrypoint":null,"OnBuild":null,"Labels":{"desktop.docker.io/binds/0/Source":"F:\\Docker\\ubuntu","desktop.docker.io/binds/0/SourceKind":"hostFile","desktop.docker.io/binds/0/Target":"/host"}},"Image":"sha256:27941809078cc9b2802deb2b0bb6feed6c236cde01e487f200e24653533701ee","NetworkSettings":{"Bridge":"","SandboxID":"58988ba7844da9d0935f8d1b98bde42d4e2035379dbd8bbdc650d7a8b0e2f4d3","HairpinMode":false,"LinkLocalIPv6Address":"","LinkLocalIPv6PrefixLen":0,"Networks":{"bridge":{"IPAMConfig":null,"Links":null,"Aliases":null,"NetworkID":"db30eba5fc1efb33c19f3caf3f8e305f515cf9cb2ce38b9c8bf04eb705c19b34","EndpointID":"b75ebea4447c79b314c24ab66e63e59d13ce141fd127b7978e5e9ea472fe6e01","Gateway":"172.17.0.1","IPAddress":"172.17.0.2","IPPrefixLen":16,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"02:42:ac:11:00:02","DriverOpts":null,"IPAMOperational":false}},"Service":null,"Ports":{"22/tcp":[{"HostIp":"0.0.0.0","HostPort":"222"}]},"SandboxKey":"/var/run/docker/netns/58988ba7844d","SecondaryIPAddresses":null,"SecondaryIPv6Addresses":null,"IsAnonymousEndpoint":false,"HasSwarmEndpoint":false},"LogPath":"/var/lib/docker/containers/2635168136d879bdc4029a0e78db5d27ac829c1a0bac994053b695ee000da374/2635168136d879bdc4029a0e78db5d27ac829c1a0bac994053b695ee000da374-json.log","Name":"/ubuntu","Driver":"overlay2","OS":"linux","MountLabel":"","ProcessLabel":"","RestartCount":0,"HasBeenStartedBefore":true,"HasBeenManuallyStopped":false,"MountPoints":{"/host":{"Source":"/run/desktop/mnt/host/f/Docker/ubuntu","Destination":"/host","RW":true,"Name":"","Driver":"","Type":"bind","Propagation":"rprivate","Spec":{"Type":"bind","Source":"/run/desktop/mnt/host/f/Docker/ubuntu","Target":"/host"},"SkipMountpointCreation":false}},"SecretReferences":null,"ConfigReferences":null,"AppArmorProfile":"","HostnamePath":"/var/lib/docker/containers/2635168136d879bdc4029a0e78db5d27ac829c1a0bac994053b695ee000da374/hostname","HostsPath":"/var/lib/docker/containers/2635168136d879bdc4029a0e78db5d27ac829c1a0bac994053b695ee000da374/hosts","ShmPath":"","ResolvConfPath":"/var/lib/docker/containers/2635168136d879bdc4029a0e78db5d27ac829c1a0bac994053b695ee000da374/resolv.conf","SeccompProfile":"","NoNewPrivileges":false,"LocalLogCacheMeta":{"HaveNotifyEnabled":false}}

3.修改hostconfig.json

添加端口映射

"22/tcp":[
            {
                "HostIp":"",
                "HostPort":"222"
            }
        ]

window11 下使用docker + vscode 搭建cpp开发环境_第4张图片

 配置解释: ssh 映射到主机上的 ip:port 为 127.0.0.1:222

完整配置文件

{"Binds":["/run/desktop/mnt/host/f/Docker/ubuntu:/host"],"ContainerIDFile":"","LogConfig":{"Type":"json-file","Config":{}},"NetworkMode":"default","PortBindings":{"22/tcp":[{"HostIp":"","HostPort":"222"}]},"RestartPolicy":{"Name":"","MaximumRetryCount":0},"AutoRemove":false,"VolumeDriver":"","VolumesFrom":null,"CapAdd":null,"CapDrop":null,"CgroupnsMode":"host","Dns":[],"DnsOptions":[],"DnsSearch":[],"ExtraHosts":null,"GroupAdd":null,"IpcMode":"private","Cgroup":"","Links":null,"OomScoreAdj":0,"PidMode":"","Privileged":false,"PublishAllPorts":false,"ReadonlyRootfs":false,"SecurityOpt":null,"UTSMode":"","UsernsMode":"","ShmSize":67108864,"Runtime":"runc","ConsoleSize":[0,0],"Isolation":"","CpuShares":0,"Memory":0,"NanoCpus":0,"CgroupParent":"","BlkioWeight":0,"BlkioWeightDevice":null,"BlkioDeviceReadBps":null,"BlkioDeviceWriteBps":null,"BlkioDeviceReadIOps":null,"BlkioDeviceWriteIOps":null,"CpuPeriod":0,"CpuQuota":0,"CpuRealtimePeriod":0,"CpuRealtimeRuntime":0,"CpusetCpus":"","CpusetMems":"","Devices":null,"DeviceCgroupRules":null,"DeviceRequests":null,"KernelMemory":0,"KernelMemoryTCP":0,"MemoryReservation":0,"MemorySwap":0,"MemorySwappiness":null,"OomKillDisable":false,"PidsLimit":null,"Ulimits":null,"CpuCount":0,"CpuPercent":0,"IOMaximumIOps":0,"IOMaximumBandwidth":0,"MaskedPaths":["/proc/asound","/proc/acpi","/proc/kcore","/proc/keys","/proc/latency_stats","/proc/timer_list","/proc/timer_stats","/proc/sched_debug","/proc/scsi","/sys/firmware"],"ReadonlyPaths":["/proc/bus","/proc/fs","/proc/irq","/proc/sys","/proc/sysrq-trigger"]}

4.重启docker,再次通过docker inspect [containerid] 查看容器信息,验证是否配置成功。

window11 下使用docker + vscode 搭建cpp开发环境_第5张图片

二.安装ssh服务

进入docker环境

docker exec -it 2635168136d8 /bin/bash

首先切换到root账户,如果没有的root的话添加下root账户的密码

passwd

安装ssh服务

sudo apt-get install openssh-server

修改配置

vim /etc/ssh/sshd_config

添加如下内容:

PubkeyAuthentication yes
PermitRootLogin yes

启动ssh服务

service ssh start

生成sshkey 信息

ssh-keygen -t rsa      一路回车

vs开发环境配置

安装插件

安装remote ssh

window11 下使用docker + vscode 搭建cpp开发环境_第6张图片

ssh连接

window11 下使用docker + vscode 搭建cpp开发环境_第7张图片

 安装cpp插件

window11 下使用docker + vscode 搭建cpp开发环境_第8张图片

 测试环境

window11 下使用docker + vscode 搭建cpp开发环境_第9张图片

你可能感兴趣的:(Android,vscode,docker,ide)