Docker cheat sheet

While developing a docker backend urcap it is possible that something goes wrong, and you need to debug the container. This page will give a few tips for debugging a docker container.

The following section will go over a few common steps to debug containers like

How to ...                                       COMMAND
-----------------------------------------------------------------------------------------------------                  
Enter a container with the terminal              docker exec -it [container_id] bash
Run specific command inside a container          docker exec [container_id] [command]
Show all running containers                      docker ps
Show all running and exited containers           docker ps -a
Get log from container                           docker logs [container_id]
Get continuous log from running container        docker logs -f [container_id]

Q: I want to check if my container is running

When you have installed your urcap (following install), it is possible to verify that the container is running by using the command

docker ps

This will show you a view similar to

CONTAINER ID   IMAGE                     COMMAND                  CREATED        STATUS         PORTS                                              NAMES
363e4c9ecff1   ur/falcon-runtime:8.5.4   "run-docker.sh /bin/…"   1 minute ago   Up 5 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp, 2375-2376/tcp   ursim-polyscopex-runtime-1

This shows that a container with the id: 363e4c9ecff1 is the simulator. Your backend urcap should be installed inside this container. So now you can enter the container and then do the docker ps

docker exec -it 363e4c9ecff1 bash

This opens the container in your terminal which you can exit it again by writing: exit. Now that you are inside the PolyScope X simulator you can write the docker ps command:

CONTAINER ID   IMAGE                                                           COMMAND                  CREATED              STATUS                        PORTS                                                                                                                            NAMES
c74eeddf729e   universal-robots_ur-cloud_ur-cloud:latest                       "gunicorn '-b :5000'…"   55 seconds ago       Up 54 seconds                 5000/tcp                                                                                                                         universal-robots_ur-cloud_ur-cloud
66f0933c5126   universal-robots_ros2-web-bridge_ros2-web-bridge:latest         "/venv/bin/python3 m…"   57 seconds ago       Up 57 seconds                                                                                                                                                  universal-robots_ros2-web-bridge_ros2-web-bridge
3521cba2fbe0   universal-robots_java-backend_java-backend-prod:latest          "/usr/local/s2i/run"     About a minute ago   Up About a minute             8080/tcp, 8443/tcp, 127.0.0.1:5005->5005/tcp, 30015/tcp                                                                          universal-robots_java-backend_java-backend
35606367cf13   universal-robots_web-frontend-app_web-recording-engine:latest   "flask run --host 0.…"   About a minute ago   Up About a minute             52762/tcp                                                                                                                        universal-robots_web-frontend-app_web-recording-engine
87285506c629   ur/web-bootstrapper:latest                                      "./bootstrapper_entr…"   About a minute ago   Up About a minute                                                                                                                                              root-web-bootstrapper-1
bedbaaf4ec26   docker.urrd.dk/urservice:13.0.12                                "./urservice"            About a minute ago   Up About a minute (healthy)   8181/tcp                                                                                                                         root-urservice-1
5df34bbc465b   docker.urrd.dk/tool/controller-artifact:67.8.0                  "/entrypoint --robot…"   About a minute ago   Up About a minute             0.0.0.0:30001-30002->30001-30002/tcp, 0.0.0.0:30004->30004/tcp, 0.0.0.0:30011-30013->30011-30013/tcp, 0.0.0.0:30020->30020/tcp   root-urcontrol-primary-1
697d887cc958   docker.urrd.dk/nginx:1.22.1                                     "/docker-entrypoint.…"   About a minute ago   Up About a minute             0.0.0.0:80->80/tcp                                                                                                               root-nginx-1
6835292e2c78   docker.urrd.dk/ur/ur-auth-in-docker:1.3.0                       "java -jar -XX:+UseS…"   About a minute ago   Up About a minute             0.0.0.0:28999->28999/tcp                                                                                                         root-ur-auth-1
363e4c9ecff1   my_first_sleeping_urcap:1.0.0                                   "sleep 3600"             About 2 minute ago   Up About a minute                                                                                                                      

This shows all the containers that are running - and have not exited - in the PolyScope X simulator.

Shortcut

It is possible to skip entering the PolyScope X simulator and execute the docker ps command by combining with the docker exec command. We leverage the knowledge that per default the PolyScope X simulator is started with the name ursim-polyscope-runtime-1 which we can use instead of the container id for the docker exec.

docker exec ursim-polyscopex-runtime-1 docker ps

Q: I am not able to see my container

If you installed the urcap, but it doesn’t show up it might be because it exited for some unknown reason. In that case you can add -a to docker ps like docker ps -a. This will print the containers also with the exit status.

CONTAINER ID   IMAGE                           COMMAND        CREATED              STATUS                       PORTS     NAMES
...
363e4c9ecff1   my_first_sleeping_urcap:1.0.0   "sleep 1234"   About a minute ago   Exited (137) 36 seconds ago            

Q: Why did my container exit?

With the above command we can try and get the logs from the container. To do that we want to use the docker logs [container_id] command. But inside the container where the urcap is installed.

docker exec ursim-polyscope-runtime-1 docker logs 363e4c9ecff1

Which will then print out the logs from the container before it shuts down. Now it is then possible to see if there was anything out of the ordinary in the logs. If you are unable to find any apparent reason for the shutdown it might be necessary to add debug statements which can be followed in the log and re-run with the container.

Shortcut

You might have noticed that instead of a [container_id] we have been using ursim-polyscope-runtime-1, when referring to the simulator. Likewise, you can do the same when referring to your installed URCap. So you can get the logs from the URCap with

docker exec ursim-polyscope-runtime-1 docker logs [vendor_id]/[urcap_id]/[artefact_id]

Which is the tags that you have defined when you created the URCap. Since the [container_id] can change when making changes to the URCap and re-installing it, using the [vendor_id]/[urcap_id]/[artefact_id] could be user while developing.

Q: I added my debug statements and want to follow the logs while running

To be able to follow the logs while a container is running the optional follow flag -f can be used with docker logs for a combined docker logs -f [container_id]. So after adding debug statements to the URCap and installing it you can now write

docker exec ursim-polyscope-runtime-1 docker logs -f 363e4c9ecff1

Which will then continuously print the logs from the container until you do a CTRL+C to stop running the command. As an example the logs could look something like the following where the container prints out the first available time that it starts up. Since the purpose of this container is to sleep, it also prints out that it is about to do that. And then every 10 seconds. Which we notice it does so twice before it shuts down.

ursim-polyscopex-runtime-1  | my_first_sleeping_urcap-1 | 2023/12/24 18:00:00 Debug: Just entering my script
ursim-polyscopex-runtime-1  | my_first_sleeping_urcap-1 | 2023/12/24 18:00:01 Debug: About to call the sleep command
ursim-polyscopex-runtime-1  | my_first_sleeping_urcap-1 | 2023/12/24 18:00:12 Debug: I have now slept for 10 seconds
ursim-polyscopex-runtime-1  | my_first_sleeping_urcap-1 | 2023/12/24 18:00:22 Debug: I have now slept for 10 seconds
ursim-polyscopex-runtime-1  | my_first_sleeping_urcap-1 | 2023/12/24 18:00:28 Called exit with status 137