赞
踩
下面是几个Docker容器操作的自动脚本,以简化docker容器操作的复杂命令,创建文件,复制,编辑,保存,添加可执行权限,运行即可使用。
创建容器脚本
#!/bin/bash # @filename make_container.sh echo "-- input container name" read CONTAINER_NAME echo "-- input image name" read IMAHE_NAME echo "-- SUMMARY --" echo "-- CONTAINER_NAME : $CONTAINER_NAME" echo "-- IMAHE_NAME : $IMAHE_NAME" echo "" echo "-- removing exist container" docker stop ${CONTAINER_NAME} >/dev/null 2>&1 docker rm ${CONTAINER_NAME} >/dev/null 2>&1 echo "-- making a workspace $CONTAINER_NAME" mkdir -p $CONTAINER_NAME chmod -R a+rw $CONTAINER_NAME echo "-- adding xhost" xhost + >/dev/null echo "-- creating" docker run \ -it \ -v /tmp/.X11-unix:/tmp/X11-unix \ -e DISPLAY=unix${DISPLAY} \ -e GDK_SCALE \ -e GDK_DPI_SCALE \ --name=${CONTAINER_NAME} \ -v ${PWD}/$CONTAINER_NAME:/home/$CONTAINER_NAME \ --network=host \ ${IMAHE_NAME} \ /bin/bash if [ $? -eq 0 ]; then echo "-- done" else echo "-- failed" fi
PS:
-v /tmp/.X11-unix:/tmp/X11-unix -e DISPLAY=unix${DISPLAY} -e GDK_SCALE -e GDK_DPI_SCALE
目的是可视化显示界面(可去除)--network=host
与主机共享网络-v ${PWD}/$CONTAINER_NAME:/home/$CONTAINER_NAME
将主机的文件夹映射到容器指定目录中--rm
参数可将该容器设置为临时容器,即运行完最后的命令后将该容器删除#!/bin/bash # @filename into_container.sh xhost + > /dev/null docker ps -a echo "-- Which container name to be get in?" read CONTAINER_NAME echo "-- command. default /bin/bash" read COMMAND if [ ! $COMMAND ]; then COMMAND="/bin/bash" fi docker start ${CONTAINER_NAME} >/dev/null docker exec -it ${CONTAINER_NAME} ${COMMAND}
#!/bin/bash # @filename remove_container.sh docker ps -a echo "-- Which container name to be moved?" read CONTAINER_NAME docker stop ${CONTAINER_NAME} >/dev/null docker rm ${CONTAINER_NAME} >/dev/null echo "-- Do you want to remove the workspace also?" echo "-- 'y' or 'n'. default : 'n'" read is_ws_remove if [ $is_ws_remove = "y" ];then rm -r ./${CONTAINER_NAME} fi
下面是ros-humble-core的Dockerfile文件
# This is an auto generated Dockerfile for ros:ros-core # generated from docker_images_ros2/create_ros_core_image.Dockerfile.em FROM ubuntu:jammy # setup timezone RUN echo 'Etc/UTC' > /etc/timezone && \ ln -s /usr/share/zoneinfo/Etc/UTC /etc/localtime && \ apt-get update && \ apt-get install -q -y --no-install-recommends tzdata && \ rm -rf /var/lib/apt/lists/* # install packages RUN apt-get update && apt-get install -q -y --no-install-recommends \ dirmngr \ gnupg2 \ && rm -rf /var/lib/apt/lists/* # setup sources.list RUN echo "deb http://packages.ros.org/ros2/ubuntu jammy main" > /etc/apt/sources.list.d/ros2-latest.list # setup keys RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654 # setup environment ENV LANG C.UTF-8 ENV LC_ALL C.UTF-8 ENV ROS_DISTRO humble # install ros2 packages RUN apt-get update && apt-get install -y --no-install-recommends \ ros-humble-ros-core=0.10.0-1* \ && rm -rf /var/lib/apt/lists/* # setup entrypoint COPY ./ros_entrypoint.sh / ENTRYPOINT ["/ros_entrypoint.sh"] CMD ["bash"]
PS
FROM
基于该镜像进行创建RUN
运行该命令ENV
设置环境变量COPY
拷贝指定文件到要创建的镜像中ENTRYPOINT
启动后自动运行CMD
运行的指令创建docker镜像
docker build -t ros2-humble-core:v1 .
ros2-humble-core:v1
是创建后镜像的名字+版本,冒号:
前为名字,后面为镜像的版本
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。