Dockerfile records

Overview

Dockerfile examples

Keywords

  • FROM: specifies the base image upon which the new image will be built. Every Dockerfile must start with a FROM instruction. For example, FROM ubuntu:20.04 would instruct Docker to use the Ubuntu 20.04 base image.

  • RUN: This keyword executes commands within the container during the image build process. For example, RUN apt-get update && apt-get install -y python3 would update the package list and install Python 3 inside the container.

  • COPY / ADD: These keywords are used to copy files from the host machine into the container. COPY is preferred for simple file copying, while ADD has some additional features like unpacking tar archives. For example, COPY . /app would copy the contents of the current directory into a directory named /app in the container.

  • WORKDIR: This sets the working directory for any RUN, CMD, ENTRYPOINT, COPY, and ADD instructions that follow it in the Dockerfile. For example, WORKDIR /app would set the working directory to /app.

  • CMD: This specifies the default command to run when the container starts. Unlike RUN, which runs during the build process, CMD is executed when the container is launched. You can only have one CMD instruction in a Dockerfile. For example, CMD ["python3", "app.py"] would run the Python script app.py when the container starts.

  • ENTRYPOINT: Similar to CMD, ENTRYPOINT specifies the command to run when the container starts. However, it differs in that it cannot be overridden at runtime, whereas CMD can be overridden by passing arguments to docker run. For example, ENTRYPOINT ["python3", "app.py"] would run the Python script app.py when the container starts, and additional arguments could be appended to this command when running the container.

  • EXPOSE: This keyword informs Docker that the container listens on specific network ports at runtime. It does not actually publish the ports. For example, EXPOSE 80 would indicate that the container listens on port 80.

Dockerfile with Conda

Install and activate a Conda virtual envrionment.

 1ARG ARCH_IMG
 2ARG CI_PROJECT_DIR
 3FROM $ARCH_IMG
 4ARG PACKAGE
 5ARG LAUNCHFILE
 6
 7ENV env_launchfile=${LAUNCHFILE}
 8ENV env_launchdir=${LAUNCH_DIR}
 9ENV env_package=${PACKAGE}
10
11ENV DEBIAN_FRONTEND=noninteractive
12
13RUN apt-get update && apt-get install -y wget python3-pip libxext6 ffmpeg libsm6  libffi-dev \
14     ros-noetic-jsk-recognition-msgs ros-noetic-sensor-msgs python3-rospy python3-roslaunch \
15     ros-noetic-cv-bridge wget ros-noetic-jsk-rviz-plugins ros-noetic-roslaunch &&\
16     rm -rf /var/lib/apt/lists/*
17
18# Install and activate minicoda
19RUN mkdir -p ~/miniconda3 &&\
20     wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda3/miniconda.sh &&\
21     bash ~/miniconda3/miniconda.sh -b -u -p ~/miniconda3
22     rm -rf ~/miniconda3/miniconda.sh
23
24RUN ~/miniconda3/bin/conda init bash
25
26ENV PATH /root/miniconda3/envs/py37/bin:$PATH
27ENV PATH /root/miniconda3/bin:$PATH
28
29RUN conda create -n py37 python=3.7 &&\
30     conda init &&\
31     echo "conda activate py37" >> ~/.bashrc
32
33# Make RUN commands use the new environment:
34SHELL ["conda", "run", "-n", "py37", "/bin/bash", "-c"]
35
36RUN pip install --upgrade pip &&\
37     pip install torchvision==0.11.1 torch==1.10.0 &&\
38     pip install einops prettytable rospkg cv_bridge matplotlib netifaces &&\
39     pip install mmcv-full==1.3.16 -f https://download.openmmlab.com/mmcv/dist/cu113/torch1.10/index.html
40
41WORKDIR /app
42COPY install install
43
44# Define HOW to run
45ENTRYPOINT [ "/bin/bash", "-c"]
46# Define WHAT to run
47CMD [ "source /opt/ros/noetic/setup.bash && \
48    source install/setup_all.bash && \
49    source ~/.bashrc && \
50    roslaunch --wait /app/install/ganav_terrain_segmentation/share/ganav_terrain_segmentation/launch/ganav_terrain_segmentation.launch" ]

Known issue: the launched docker with a docker-compose file can only activate the base conda environment.

Dockerfile with specified Python version 3.7

 1ARG ARCH_IMG
 2ARG CI_PROJECT_DIR
 3FROM $ARCH_IMG
 4ARG PACKAGE
 5ARG LAUNCHFILE
 6
 7ENV env_launchfile=${LAUNCHFILE}
 8ENV env_package=${PACKAGE}
 9
10ENV DEBIAN_FRONTEND=noninteractive
11
12# Install required system dependencies
13RUN apt-get update && \
14    apt-get install -y wget libxext6 ffmpeg libsm6 libffi-dev
15
16# Install Python 3.7
17RUN apt-get install -y build-essential zlib1g-dev libncurses5-dev libgdbm-dev \
18     libnss3-dev libssl-dev libsqlite3-dev libreadline-dev libffi-dev liblzma-dev && \
19     cd /usr/src &&\
20     wget https://www.python.org/ftp/python/3.7.17/Python-3.7.17.tgz &&\
21     tar xzf Python-3.7.17.tgz &&\
22     cd Python-3.7.17 &&\
23     ./configure --enable-optimizations &&\
24     make altinstall &&\
25     ln -s -f /usr/local/bin/python3.7 /usr/local/bin/python
26
27# Link Python3 to Python3.7
28RUN rm /usr/bin/python3
29RUN ln -s /usr/local/bin/python3.7 /usr/bin/python3
30
31# Install Python 3.7 pip
32RUN curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py && \
33    python3.7 get-pip.py && \
34    ln -s -f /usr/local/bin/pip3.7 /usr/local/bin/pip
35
36# Install requirements
37RUN pip3.7 install --upgrade pip &&\
38     pip3.7 install netifaces matplotlib cv_bridge rospkg prettytable einops \
39     backports.lzma defusedxml\
40     torchvision==0.11.1 torch==1.10.0 
41
42# Install MMCV
43RUN cd ~ && git clone https://github.com/open-mmlab/mmcv.git &&\
44     cd mmcv &&\
45     git checkout v1.3.16 &&\
46     MMCV_WITH_OPS=1 pip install -e . 
47
48# Replace lzma.py file, otherwise has error
49RUN rm /usr/local/lib/python3.7/lzma.py
50COPY ./<path>/<to>/lzma.py /usr/local/lib/python3.7/lzma.py
51
52RUN apt-get install -y \
53     ros-noetic-jsk-recognition-msgs ros-noetic-sensor-msgs python3-rospy python3-roslaunch \
54     ros-noetic-cv-bridge wget ros-noetic-jsk-rviz-plugins ros-noetic-roslaunch
55
56CMD source /opt/ros/noetic/setup.bash
57
58RUN rm -rf /var/lib/apt/lists/*
59
60WORKDIR /app
61COPY install install
62
63# Define HOW to run
64ENTRYPOINT [ "/bin/bash", "-c" ]
65# Define WHAT to run
66CMD [ "source /opt/ros/noetic/setup.bash && \
67    source install/setup_deps.bash && \
68    roslaunch --wait ${env_package} ${env_launchfile}" ]

The Docker image uses Python3.7 instead of default version which usually Python 3.8.

Saved Docker script, install python3.7 and use pip3.7 as default.

 1# RUN apt-get update && \
 2#      # Install system dependencies
 3#      apt-get install -y wget libxext6 ffmpeg libsm6 libffi-dev && \
 4#      # Install Python 3.7
 5#      apt-get install -y build-essential zlib1g-dev libncurses5-dev libgdbm-dev \
 6#      libnss3-dev libssl-dev libsqlite3-dev libreadline-dev libffi-dev liblzma-dev && \
 7#      cd /usr/src &&\
 8#      wget https://www.python.org/ftp/python/3.7.17/Python-3.7.17.tgz &&\
 9#      tar xzf Python-3.7.17.tgz &&\
10#      cd Python-3.7.17 &&\
11#      ./configure --enable-optimizations &&\
12#      make altinstall &&\
13#      # Link Python and Python3 to Python3.7
14#      ln -s -f /usr/local/bin/python3.7 /usr/local/bin/python &&\
15#      rm /usr/bin/python3 &&\
16#      ln -s /usr/local/bin/python3.7 /usr/bin/python3 &&\
17#      # Install Python 3.7 pip
18#      curl https://bootstrap.pypa.io/pip/3.7/get-pip.py -o get-pip.py && \
19#      python3.7 get-pip.py && \
20#      ln -s -f /usr/local/bin/pip3.7 /usr/local/bin/pip && \
21#      # Install pip requirements
22#      pip3.7 install --upgrade pip && \
23#      pip3.7 install netifaces==0.11.0 matplotlib==3.5.3 cv_bridge rospkg==1.5.1 prettytable==3.7.0 einops==0.6.1 \
24#      backports.lzma defusedxml ftfy==6.1.1 regex==2024.4.16 && \
25#      pip3.7 install torch torchvision torchaudio --force-reinstall  --extra-index-url https://download.pytorch.org/whl/cu116 && \
26#      # Install mmcv, mmsegmentation
27#      pip3.7 install mmcv==2.0.0rc4 -f https://download.openmmlab.com/mmcv/dist/cu113/torch1.10/index.html && \
28#      pip3.7 install mmsegmentation==1.2.2 && \
29#      # Install mmcv from source code
30#      # cd ~ && git clone https://github.com/open-mmlab/mmcv.git && \
31#      # cd mmcv && \
32#      # git checkout v1.3.16 && \
33#      # MMCV_WITH_OPS=1 pip install -e . && \
34#      # Remove the file to be replaced
35#      rm /usr/local/lib/python3.7/lzma.py
comments powered by Disqus

Translations: