Windows11下Docker使用记录(二)

本文详细介绍了Docker中的基本概念,包括Dockerfile中的关键指令,如FROM、RUN、COPY、ADD等,并提供了ROS镜像构建的Dockerfile示例,以及如何构建、保存和加载Docker镜像的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Docker中container,image,dockerfile
以及docker hub的关系如图所示,详细可见上一篇帖子。
本文主要记录Dockerfile相关。

在这里插入图片描述

1. 常用指令

常用指令(默认大写)功能
FROM指定基础镜像,位于Dockerfile第一行(必须有)FROM ubuntu:16.04
MAINTAINER指定维护者信息(可以不写)MAINTAINER "test<test@outlook.com>"
COPY和ADD相似,复制host中内容(必须与Dockerfile同级)至image指定路径中。COPY index.html /data/web/html/
ADD同上。此外,如果source是URL或压缩包,会自动下载或自动解压 ADD http://nginx.org/download/nginx-1.15.8.tar.gz /usr/local/src/
WORKDIR设置当前工作目录,为RUN,CMD等设置工作目录
ENV定义所需的环境变量
RUNdocker build过程中运行的程序,可以是任何命令 RUN cd ./src
EXPOSE声明容器运行的服务端口,有tcp(默认)和udp EXPOSE 90/tcp
CMD指定容器启动后默认运行的程序,CMD /usr/sbin/sshd -D(可以不写)

2. Dockerfile示例

FROM ubuntu:16.04

# install ROS kinetic
RUN apt-get update \
    && apt-get -y install curl \
    && sh -c 'echo "deb http://packages.ros.org/ros/ubuntu xenial main" > /etc/apt/sources.list.d/ros-latest.list' \
    && curl -s https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc | apt-key add - \
    && apt-get update \
    && apt-get -y install ros-kinetic-desktop-full \
    && echo "source /opt/ros/kinetic/setup.bash" >> /etc/bash.bashrc \
    && /bin/bash -c '. /etc/bash.bashrc' \
    && apt-get -y install python-rosdep python-rosinstall python-rosinstall-generator python-wstool build-essential \
    && rosdep init \
    && rosdep update 
# create catkin_ws
RUN mkdir -p ~/catkin_ws/src \
    && cd ~/catkin_ws \
    # # ...
    # # do what you need to do for example git clone something into ~/catkin_ws/src
    # # ...
    && /bin/bash -c '. /opt/ros/kinetic/setup.bash; cd ~/catkin_ws; catkin_make; . ./devel/setup.bash'

3. 构建docker image

docker build
. 非常重要

docker build -t ubuntu-16.04:kinetic .

保存docker image至本地tar文件

docker save ubuntu-16.04:kinetic > ~/kinetic.tar

加载 tar文件到镜像

docker load -i kinetic.tar

参考文献:
1. https://www.cnblogs.com/edisonchou/p/dockerfile_inside_introduction.html
2. https://www.cnblogs.com/along21/p/10243761.html
3. http://t.csdnimg.cn/itVtL
4. http://t.csdnimg.cn/brVnu

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值