Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

When trying to develop and debug in container environment, in lieu of local machine

Note

Multiple versions of docs are kept in this page in the following order:

  • Newer versions towards the top
  • Older  versions towards the end

...

Doc Version 1.1

Purposes

Solved these problems:

  1. Two containers (one for VS Code, the other one for running) appear in Docker Desktop
    1. → Would like to merge them into one
    2. → Even better, remove the container after finishing using (clean for the eye)
  2. After making changes to the code, the changes are not auto-reloaded. Must re-run docker-compose up
    1. → Enable auto-reloading
  3. Env variables not all enabled
    1. → Re-enable all previously used development env variables, with auto-reloading enabled (via volume)
    2. → Create a separate prod env variables, with auto-reloading disabled (via volume)
  4. VS Code extension Python does not (always) work
    1. → Fix the "couldn't load python tool" error

Step-by-step guide

Step 0 Preparation; Overview

Code Block
languagebash
docker volume rm $(docker volume ls -f dangling=true -q)

In terminal, run the above command to do some cleaning. (There might be some dangling volumes not garbage-collected yet by docker).

 

(Optional) In Docker Desktop, on the Container / Apps tab, delete all related containers of this project.

(Optional) In Docker Desktop, on the Images tab, except busybox, delete all related images of this project. Do NOT delete busybox.

 

Switch to git branch docker2, where the new code resides.

If everything goes swimmingly, it is recommended to merge docker2 into the branch you're working on (master, feature, etc.).

 

Some overview:

  • Python image issues:
    • The current Python image used in this project is based on alpine Linux.
    • alpine Linux does not include many necessary libs for development, explaining why Python extension fails and additional lib installation (RUN apk *) in Dockerfile
    • We're going to switch to the standard Python image to avoid all the problems.
  • VS Code and container
    • Previously, we let VS Code create a container and develop inside (remember the container name of random scientist/mathematician), hence the extra container.
    • Now, we're going to build/create/run the container by ourselves, and let VS Code attach to the container. Only ONE both for running and VS Code
    • Additionally, upon finished (developing and running), we'll shut down the container, and it will be removed from Docker Desktop > Containers Apps, making it look clean.
  • Others (if you're interested, optional)
    • Dockerfile: The base image FROM is changed to python:3.8.6, a full-feature image. You will also see many lines commented out, unnecessary for the moment.
    • docker-compose.yml : This is used for development (also the default one). You can see all env variables for development.
    • docker-compose-prod.yml: This will be for production. Not finished yet.
    • docker-compose.debug.yml.old: Just some backup. Ignore it.

Step 1 Run the container

Code Block
languagebash
# Path for online repo
backend-online/dev-backend
 
# Path for offline repo
backend-offline/dev-dashboard

Go to the path as above.

 

Code Block
languagebash
# For the first time
# Or whenever you want to update the image, e.g changing Dockerfile, requirements.txt
docker-compose up -d --build
 
# Otherwise
docker-compose up -d

Run the command.

This will build the image, create+run the container.

This could take a minute.

 

Image Added

Image Added

You should see a new container running.

Name is based on the path (not random anymore)

 

On VS Code, make sure you've installed these two extensions:

  • Remote - Containers
  • Python

Step 2 Attach VS Code to the container; Necessary extensions

On VS Code, click the Remote button at bottom left > Attach to running container > Click the container name in the menu.

For the first time, it could take a minute to load.

 

Image Added

For the first time, click File > Open and enter the path above /var/www/ . Then you will be at the working directory.

From the second time, VS Code will remember to open this path automatically.

 

Image Added

As always, make sure you've installed this Python extension, and it is enabled in container.

Click an .py file to activate it.

Choose interpreter of Python 3.8.6 64-bit

It might show loading/downloading analysis at the bottom status bar, after which it should work properly.

Step 3 Run and debug; Auto-reload

Image Added

Everything under www, if changed:

  • Will be auto-reloaded
  • Will will make change in your repo's docker_code subfolder
  • You can think of www and docker_code as two pointers to the same folder

 

Code Block
# Visit http://localhost:5000/
{
  "message": "Welcome to the Dockerized Flask MongoDB app, again and again and again (this will auto-reload)!"
}
 
# Change the message at app/views/static.py
# For instance, add "test" at the end
 
# Visit http://localhost:5000/ again
# Notice the end now includes "test"
# It's auto-reloaded
{
  "message": "Welcome to the Dockerized Flask MongoDB app, again and again and again (this will auto-reload)! test"
}

This example is only for online repo. (Offline repo does not have defined this url in repo)

Try the above to see auto-reloading is working.

Step 4 Close connexion with container; Shut down container

On VS Code, click the Remote button at bottom left > Close remote connection.

 

Code Block
languagebash
docker-compose down

In terminal, run the command above. It will shutdown and remove container (as you can see it disappears from Docker Desktop > Containers / Apps)

...

Doc Version 1.0

Purpose

Gliffy Diagram
nameDev and Debug in container
pagePin1

  1. Environment consistency: Avoiding inconsistency due to different dependency/lib on local machine v in container
  2. Centralised dependency/lib management: Only one member needs to update the config (requirements.txt) in docker, and all other members will have access to updated dependency/libs automatically after git pull
  3. Convenience: Avoiding install/keep the same dependency/lib twice, i.e. on local machine and in container

Step-by-step guide

Note: This guide is based on VS Code. I've also tried IntelliJ IDEA (including PyCharm), but unfortunately it doesn't work on my laptop. If interested, see the link below under "Read more"

...

If you run it, it should show in terminal the same output as above. Note the 2nd line Linux shows it's been run in container (not local machine of Mac OS/Windows)

Note

Everything you've coded is done in the same git repo.

...