wsl2安装容器以及容器配置

安装docker

  1. 更新ubuntu

    sudo apt update && sudo apt upgrade && sudo apt full-upgrade
  2. 添加docker仓库

    # 安装的证书并允许 apt 包管理器使用命令通过 HTTPS 使用存储库
    sudo apt install apt-transport-https ca-certificates curl software-properties-common gnupg lsb-release
    ​
    # 添加 Docker 的官方 GPG 密钥
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
    ​
    # 添加 Docker 官方仓库
    echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
    ​
    # 更新 Ubuntu 源代码
    sudo apt update
  3. 安装最新的Docker CE

    sudo apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin
  4. 验证 Docker 服务是否正在运行

    systemctl status docker
  5. 如果尚未启动,请运行以下命令以启动 Docker 服务

    sudo systemctl start docker
  6. 配置 Docker 服务在每次重新启动时自动启动

    sudo systemctl enable docker
  7. 使用以下命令找到已安装的 Docker 版本

    sudo docker version
  8. 测试Docker是否正常工作

    sudo docker run hello-world

    上面的命令将下载一个测试 Docker 镜像,并在容器内执行一个示例hello_world程序。

    如果您看到如下所示的输出,恭喜!Docker在我们的Ubuntu系统中运行良好

    Unable to find image 'hello-world:latest' locally
    latest: Pulling from library/hello-world
    2db29710123e: Pull complete 
    Digest: sha256:13e367d31ae85359f42d637adf6da428f76d75dc9afeb3c21faea0d976f5c651
    Status: Downloaded newer image for hello-world:latest
    ​
    Hello from Docker!
    This message shows that your installation appears to be working correctly.
    ​
    To generate this message, Docker took the following steps:
     1. The Docker client contacted the Docker daemon.
     2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
        (amd64)
     3. The Docker daemon created a new container from that image which runs the
        executable that produces the output you are currently reading.
     4. The Docker daemon streamed that output to the Docker client, which sent it
        to your terminal.
    ​
    To try something more ambitious, you can run an Ubuntu container with:
     $ docker run -it ubuntu bash
    ​
    Share images, automate workflows, and more with a free Docker ID:
     https://hub.docker.com/
    ​
    For more examples and ideas, visit:
     https://docs.docker.com/get-started/

docker 操作

  1. 下载镜像

    # 下载最新的ubuntu
    sudo docker pull ubuntu
    # 下载ubuntu 20.04
    sudo docker pull ubuntu:20.04

  2. 查看镜像

    docker images

    REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 1f6ddc1b2547 3 weeks ago 77.8MB hello-world latest 9c7a54a9a43c 5 weeks ago 13.3kB

  3. 运行一个基于ubuntu的linux容器

    docker run --name ehome -itd ubuntu

    其中:

    –name ehome 表示给运行的窗口取名为 ehome

    -i 表示 interactive 可交互的,变即可以从标准输入与容器交互。

    -t 表示给容器分配一个虚拟终端。

    -d 这个参数表示的是在后台运行,即 –deamon。

    ubuntu 表示的是运行容器所使用的镜像名称

  4. 查看运行容器

    # 查看名称中包含ehome的容器运行状态
    docker ps -a | grep ehome

    36f84f508315 ubuntu "/bin/bash" 47 minutes ago Up 47 minutes ehome

    可以看到,后台已经运行了一个叫做 ehome 的窗口,容器 ID 的前 12 位部分是 36f84f508315

    # 查看所有容器运行状态
    docker ps -a

    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8b1e9929a945 ubuntu:20.04 "/bin/bash" 3 days ago Exited (0) 3 minutes ago ehome_v1 36f84f508315 ubuntu "/bin/bash" 4 days ago Up 3 seconds ehome

    # 查看当前正在运行的容器
    docker ps

    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 36f84f508315 ubuntu "/bin/bash" 4 days ago Up 5 minutes ehome

  5. 进入 Ubuntu Linux 容器内部

    docker exec -it ehome /bin/bash

    其中 docker exec 是固定命令,-it 是 interactive 和 tty 缩写,后面跟容器 id 或名称,/bin/bash 表示内部使用的 shell 方式,也可以简写 bash。

  6. 常用组件工具

    apt-get update 更新 apt-get 工具,这个步骤优先级最高,因为不更新很多组件安装不了 apt install vim 安装 vim 工具,初始系统中连 vi 都没有,显然很需要。另外,可以使用 apt-get 命令简写 apt 来执行安装,效果相同。 apt install wget 安装 wget 工具,如果你需要从网络上下载资源 apt install curl 安装 curl 工具,如果你需要使用 curl 来访问网络资源 apt install net-tools 如果你需要使用网络工具,比如 ifconfig 等,安装 net-tools apt install telnet 如果需要使用 telnet,安装 telnet

  7. 将本地文件复制到docker容器中

    docker cp 本地路径 容器ID/容器NAME:容器内路径
    docker cp /mnt/c/ehome/software_driver/arm-gnu-toolchain.tar.xz ehome:/home/
  8. 安装arm-none-eabi工具链

    #解压工具链
    tar -xf arm-gnu-toolchain-12.2.mpacbti-rel1-x86_64-arm-none-eabi.tar.xz
    mv arm-gnu-toolchain-12.2.mpacbti-rel1-x86_64-arm-none-eabi arm-none-eabi
    #建立软连接
    ln -s /home/arm-none-eabi/bin/arm-none-eabi-addr2line /usr/bin/arm-none-eabi-addr2line
    ln -s /home/arm-none-eabi/bin/arm-none-eabi-ar /usr/bin/arm-none-eabi-ar       
    ln -s /home/arm-none-eabi/bin/arm-none-eabi-as /usr/bin/arm-none-eabi-as
    ln -s /home/arm-none-eabi/bin/arm-none-eabi-c++ /usr/bin/arm-none-eabi-c++
    ln -s /home/arm-none-eabi/bin/arm-none-eabi-c++filt /usr/bin/arm-none-eabi-c++filt
    ln -s /home/arm-none-eabi/bin/arm-none-eabi-cpp /usr/bin/arm-none-eabi-cpp    
    ln -s /home/arm-none-eabi/bin/arm-none-eabi-elfedit /usr/bin/arm-none-eabi-elfedit
    ln -s /home/arm-none-eabi/bin/arm-none-eabi-g++ /usr/bin/arm-none-eabi-g++    
    ln -s /home/arm-none-eabi/bin/arm-none-eabi-gcc /usr/bin/arm-none-eabi-gcc
    ln -s /home/arm-none-eabi/bin/arm-none-eabi-gcc-ar /usr/bin/arm-none-eabi-gcc-ar
    ln -s /home/arm-none-eabi/bin/arm-none-eabi-gcc-nm /usr/bin/arm-none-eabi-gcc-nm
    ln -s /home/arm-none-eabi/bin/arm-none-eabi-gcov /usr/bin/arm-none-eabi-gcov
    ln -s /home/arm-none-eabi/bin/arm-none-eabi-gcov-dump /usr/bin/arm-none-eabi-gcov-dump
    ln -s /home/arm-none-eabi/bin/arm-none-eabi-gcov-tool /usr/bin/arm-none-eabi-gcov-tool
    ln -s /home/arm-none-eabi/bin/arm-none-eabi-gdb /usr/bin/arm-none-eabi-gdb      
    ln -s /home/arm-none-eabi/bin/arm-none-eabi-gdb-add-index /usr/bin/arm-none-eabi-gdb-add-index
    ln -s /home/arm-none-eabi/bin/arm-none-eabi-gfortran /usr/bin/arm-none-eabi-gfortran     
    ln -s /home/arm-none-eabi/bin/arm-none-eabi-gprof /usr/bin/arm-none-eabi-gprof   
    ln -s /home/arm-none-eabi/bin/arm-none-eabi-ld /usr/bin/arm-none-eabi-ld   
    ln -s /home/arm-none-eabi/bin/arm-none-eabi-nm /usr/bin/arm-none-eabi-nm
    ln -s /home/arm-none-eabi/bin/arm-none-eabi-objcopy /usr/bin/arm-none-eabi-objcopy
    ln -s /home/arm-none-eabi/bin/arm-none-eabi-objdump /usr/bin/arm-none-eabi-objdump
    ln -s /home/arm-none-eabi/bin/arm-none-eabi-ranlib /usr/bin/arm-none-eabi-ranlib 
    ln -s /home/arm-none-eabi/bin/arm-none-eabi-readelf /usr/bin/arm-none-eabi-readelf
    ln -s /home/arm-none-eabi/bin/arm-none-eabi-size /usr/bin/arm-none-eabi-size   
    ln -s /home/arm-none-eabi/bin/arm-none-eabi-strings /usr/bin/arm-none-eabi-strings
    ln -s /home/arm-none-eabi/bin/arm-none-eabi-strip /usr/bin/arm-none-eabi-strp
  9. 解决arm-none-eabi-gdb 依赖问题(需要安装python3.8)

    apt install libncurses-dev
    ln -s /usr/lib/x86_64-linux-gnu/libncurses.so.6 /usr/lib/x86_64-linux-gnu/libncurses.so.5
    ln -s /usr/lib/x86_64-linux-gnu/libncursesw.so.6 /usr/lib/x86_64-linux-gnu/libncursesw.so.5
    ln -s /usr/lib/x86_64-linux-gnu/libtinfo.so.6 /usr/lib/x86_64-linux-gnu/libtinfo.so.5

    检查是否正常

    arm-none-eabi-gcc --version
    arm-none-eabi-g++ --version
    arm-none-eabi-gdb --version

    发现gdb依赖了python3.8,于是安装python3.8(如果ubuntu-20.04则无需安装,默认就是3.8)

    # 安装python3.8
    sudo apt install software-properties-common
    sudo add-apt-repository ppa:deadsnakes/ppa
    sudo apt install python3.8 libpython3.8-dev python3.8-dev
    sudo apt install python3.8-distutils

    当安装python3.8时出现一些ppa超时,则更换一下软件源,间隔重复试几次基本都能解决

  10. 如果重启启动后,需要启动容器

    # 启动ehome容器
    docker start ehome
    # 停止ehome容器
    docker stop ehome

docker 本地容器打包与加载

# 查看运行的容器
docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 492ba3f91f33 ubuntu:18.04 "/bin/bash" 18 minutes ago Up 18 minutes 1804 14830a56fa7f ubuntu:20.04 "/bin/bash" 18 minutes ago Up 18 minutes 2004 faeada7bdfa6 ubuntu:22.04 "/bin/bash" 18 minutes ago Up 18 minutes 2204

# 打包容器至当前目录
docker export 1804 > container_1804.tar
# 加载容器
docker import container_1804.tar 

sha256:4fd44a2ffd2fccebe4dc670267ec1332a2ef42d5445cbdf72d3828653dccb077

# 查看镜像
docker images

REPOSITORY TAG IMAGE ID CREATED SIZE <none> <none> 4fd44a2ffd2f 56 seconds ago 168MB

发现导出的镜像TAG变为none, 此时需要我们手动打一下tag

# 手动重新打标签
docker tag 4fd44a2ffd2f ubuntu:18.04
# 再次查看镜像
docker images

REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu 18.04 4fd44a2ffd2f 5 minutes ago 168MB

此时就正常了

# 创建一个容器,注意加上/bin/bash
docker run --name 1804 -itd ubuntu:18.04 /bin/bash

1023778b7853ab61c9eff6e5feac4edc51de26bb220a08b042279052b8b23a8a

# 查看容器
docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1023778b7853 ubuntu:18.04 "/bin/bash" About a minute ago Up About a minute 1804

# 进入容器
docker exec -it 1804 /bin/bash

说明:打包容器方式会保持环境的一致,无需再次配置环境了,环境与别人是一致的

docker 本地镜像打包与加载

# 查看运行的容器
docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 492ba3f91f33 ubuntu:18.04 "/bin/bash" 18 minutes ago Up 18 minutes 1804 14830a56fa7f ubuntu:20.04 "/bin/bash" 18 minutes ago Up 18 minutes 2004 faeada7bdfa6 ubuntu:22.04 "/bin/bash" 18 minutes ago Up 18 minutes 2204

# 打包镜像至当前目录
docker save ubuntu:20.04 > image_2004.tar
# 加载镜像
docker load < image_2004.tar 

ec66d8cea54a: Loading layer [==================================================>] 75.17MB/75.17MB Loaded image: ubuntu:20.04

# 查看镜像
docker images

REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu 20.04 626a42b93d93 3 weeks ago 72.8MB

# 创建一个容器
docker run --name 2004 -itd ubuntu:20.04

973f4f906a3f19d48ca29ef80895206471454f9279de1f5422e64eff9f8c4791

# 查看容器
docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 973f4f906a3f ubuntu:20.04 "/bin/bash" 19 seconds ago Up 18 seconds 2004

# 进入容器
docker exec -it 2004 /bin/bash

Note: 打包的镜像和仓库拉下来的是一样的,环境需要重新配置,与别人不一致了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值