Skip to content

Developing inside a Docker container

Published: at 12:00 AM

Containerization offers us a way to create a development environment perfectly tailored per project, with all the necessary tools and libraries pre-installed and readily available. No scrambling for dependencies or battling compatibility issues (say between OSes oe even two projects).

Trade-offs

  1. time put into setting up the environment
    • in this article we’ll be using a VS Code extension called Dev Containers which simplifies the process by leaps
    • little learning curve even after things have been setup
  2. additional resource usage and disk space
    • the containers and images have to be manually deleted when you’re done with the project

Dockerized Development

Docker uses containers, lightweight and portable environments, to package your application and all its dependencies. This creates a consistent and isolated development experience across different machines, regardless of the underlying operating system.

Benefits of Dockerized Development:

Getting Started with Dockerized Development:

We’ll be using:

  1. Docker Desktop
  2. VS Code
  3. Dev Containers extension

Points to bear in mind

Tutorial

We’ll work with a simple python application.

  1. In VS Code, open the Command palette (F1) and search and select for “Dev Containers: New Dev Container…“. select a new container

  2. Select “Python” in the next screen. select python

  3. Next, “Create Dev Container”. create dev container You may explore the option “Additional Options”, under which you’d find which verion of python you want to use.

    Give VS Code some time to:

    • create the dev container file
    • start/connect to docker
    • create a container
    • build an environment (linux)
    • create a workspace with appropriate permissions
    • add mounts to the container
    • connect VS Code to the container
  4. Check the docker dashboard for the created container. It’ll look something like in the image below with a random name.

    check docker dashboard

  5. At the bottom left of VS code look for the connection indicator.

    check vs code connection

  6. In VS Code, in the root of your workspace, create a new python file and write the following code and save it as hello.py.

    print("Hello World!")
    
  7. Find the same file show up in the Docker container (Docker Desktop > Volumes > Python)

    hello world in container

  8. To run the python file in the container:

    • Open a new terminal inside VS Code.
    • It will open the active workspace folder. To list the files you may execute ls command.
    • Execute python3 hello.py

    run python in container

You should see the output “Hello World!”


Previous Post
Docker on Windows vs. Linux vs. Mac
Next Post
Service-Oriented vs Microservice Architectures