|
| 1 | +RocketPy with docker |
| 2 | +===================== |
| 3 | + |
| 4 | +RocketPy does not provide an official docker image, but you can build one |
| 5 | +yourself using the provided `Dockerfile` and `docker-compose.yml` files. |
| 6 | + |
| 7 | +Benefits |
| 8 | +-------- |
| 9 | + |
| 10 | +Docker allows you to run applications in containers. The main benefits of |
| 11 | +using docker are: |
| 12 | + |
| 13 | +1. **Isolation**: run RocketPy in a fresh environment, without |
| 14 | + worrying about dependencies. |
| 15 | +2. **Portability**: run RocketPy on any operational system that supports |
| 16 | + docker, including the 3 main operational systems (Windows, Linux and Mac). |
| 17 | +3. **Reproducibility**: ensure that tour code is working regardless of the |
| 18 | + operational system. |
| 19 | + |
| 20 | +Using docker will be specially important when you are not sure if the code |
| 21 | +additions will still run on different operational systems. |
| 22 | + |
| 23 | +Although we have a set of GitHub actions to test the code on different |
| 24 | +operational systems every time a pull request is made, it is important to |
| 25 | +submit a PR only after you are sure that the code will run flawlessly, |
| 26 | +otherwise quota limits may be reached on GitHub. |
| 27 | + |
| 28 | +Requirements |
| 29 | +------------- |
| 30 | + |
| 31 | +Before you start, you need to install on your machine: |
| 32 | + |
| 33 | +1. `Docker <https://docs.docker.com/get-docker/>`__, to build and run the image. |
| 34 | +2. `Docker Compose <https://docs.docker.com/compose/install/>`__, to compose multiple images at once. |
| 35 | +3. Also, make sure you have cloned the RocketPy repository in your machine! |
| 36 | + |
| 37 | +Build the image |
| 38 | +---------------- |
| 39 | + |
| 40 | +To build the image, run the following command on your terminal: |
| 41 | + |
| 42 | +.. code-block:: console |
| 43 | + |
| 44 | + docker build -t rocketpy-image -f Dockerfile . |
| 45 | +
|
| 46 | +
|
| 47 | +This will build the image and tag it as `rocketpy-image` (you can apply another |
| 48 | +name of your preference if you want). |
| 49 | + |
| 50 | +An image is a read-only template with instructions for creating a Docker |
| 51 | +container (see `Docker docs <https://docs.docker.com/get-started/overview/#docker-objects>`__). |
| 52 | + |
| 53 | +This process may take a while, since it will create an image that could easily |
| 54 | +be 1.5 GB in size. |
| 55 | +But don't worry, you just need to build the image once. |
| 56 | + |
| 57 | +Run the container |
| 58 | +----------------- |
| 59 | + |
| 60 | +Now that you have the image, you can run it as a container: |
| 61 | + |
| 62 | +.. code-block:: console |
| 63 | +
|
| 64 | + docker run -it --entrypoint /bin/bash rocketpy-image |
| 65 | +
|
| 66 | +
|
| 67 | +This will run the container and open a bash terminal inside it. |
| 68 | +If you are using VSCode, you can even integrate the running container into your |
| 69 | +IDE, allowing you to code and test directly within the container environment. |
| 70 | +This is particularly useful if you want to maintain your usual development setup |
| 71 | +while ensuring consistency in the execution environment. |
| 72 | +For more details on how to do this, refer to the |
| 73 | +`VSCode docs <https://code.visualstudio.com/docs/remote/containers>`__ |
| 74 | +on developing inside a container. |
| 75 | + |
| 76 | +Indeed, vscode offers a full support for docker, read the |
| 77 | +`vscode docs <https://code.visualstudio.com/docs/containers/overview#_installation>`__ |
| 78 | +for more details |
| 79 | + |
| 80 | + |
| 81 | +Run the unit tests |
| 82 | +-------------------- |
| 83 | + |
| 84 | +You might have noticed that the container is running in an isolated environment |
| 85 | +with no access to your machine's files, but the `Dockerfile` already copied the |
| 86 | +RocketPy repository to the container. |
| 87 | +This means that you can run tests (and simulations!) as if you were running |
| 88 | +RocketPy on your machine. |
| 89 | + |
| 90 | +As simple as that, you can run the unit tests: |
| 91 | + |
| 92 | +.. code-block:: console |
| 93 | + |
| 94 | + pytest |
| 95 | +
|
| 96 | +
|
| 97 | +To access a list of all available execution options, see the |
| 98 | +`pytest docs <https://docs.pytest.org/en/latest/how-to/usage.html>`__. |
| 99 | + |
| 100 | +Compose docker images |
| 101 | +--------------------- |
| 102 | + |
| 103 | +We also made available a `docker-compose.yml` file that allows you to compose |
| 104 | +multiple docker images at once. |
| 105 | +Unfortunately, this file will not allow you to test the code on different |
| 106 | +operational systems at once, since docker images inherits from the host |
| 107 | +operational system. |
| 108 | +However, it is still useful to run the unit tests on different python versions. |
| 109 | + |
| 110 | +Currently, the `docker-compose.yml` file is configured to run the unit tests |
| 111 | +on python 3.8 and 3.12. |
| 112 | + |
| 113 | +To run the unit tests on both python versions, run the following command |
| 114 | +**on your machine**: |
| 115 | + |
| 116 | +.. code-block:: console |
| 117 | + |
| 118 | + docker-compose up |
| 119 | +
|
| 120 | +Also, you can check the logs of the containers by running: |
| 121 | + |
| 122 | +.. code-block:: console |
| 123 | + |
| 124 | + docker-compose logs |
| 125 | +
|
| 126 | +
|
| 127 | +This command is especially useful for debugging if any issues occur during the |
| 128 | +build process or when running the containers. |
| 129 | + |
| 130 | +After you're done testing, or if you wish to stop the containers and remove the |
| 131 | +services, use the command: |
| 132 | + |
| 133 | +.. code-block:: console |
| 134 | + |
| 135 | + docker-compose down |
| 136 | +
|
| 137 | +
|
| 138 | +This will stop the running containers and remove the networks, volumes, and |
| 139 | +images created by up. |
| 140 | + |
| 141 | + |
| 142 | +Changing to other operational systems |
| 143 | +------------------------------------- |
| 144 | + |
| 145 | +The default image in the `Dockerfile` is based on a Linux distribution. |
| 146 | +However, you can alter the base image to use different operating systems, though |
| 147 | +the process may require additional steps depending on the OS's compatibility |
| 148 | +with your project setup. |
| 149 | +For instance, certain dependencies or scripts may behave differently or require |
| 150 | +different installation procedures, so use it with caution. |
| 151 | + |
| 152 | +To change the base image, you will need to modify the `FROM` statement in the |
| 153 | +`Dockerfile`. |
| 154 | +For example, to use a Windows-based image, you might change: |
| 155 | + |
| 156 | +.. code-block:: Dockerfile |
| 157 | + |
| 158 | + FROM python:latest |
| 159 | +
|
| 160 | +
|
| 161 | +to |
| 162 | + |
| 163 | +.. code-block:: Dockerfile |
| 164 | + |
| 165 | + FROM mcr.microsoft.com/windows/servercore:ltsc2019 |
| 166 | +
|
| 167 | +
|
| 168 | +Please note, the above is just an example, and using a different OS may require |
| 169 | +further adjustments in the `Dockerfile`. |
| 170 | +We recommend you to see the official Python images on the Docker Hub for |
| 171 | +different OS options: `Docker Hub Python Tags <https://hub.docker.com/_/python/tags>`__. |
| 172 | + |
| 173 | +Be aware that switching to a non-Linux image can lead to larger image sizes and |
| 174 | +longer pull times. |
0 commit comments