Dockerize your development environment



Recently, a friend of mine asked me about how I set up my development so that it's easy to move onto production without many issues such as the differences between the environments, dependencies, etc. Here are a couple of questions.
  1. Deploy Dev environment + staging environment in containers?
  2. Is deploying a big container and stuffing all packages reasonable?
  3. What are the differences between Python container vs Ubuntu container? and which one to use?
  4. Can Dockerfile execute commands or scripts (e.g., automatically create users, run an existing script, etc.)?
  5. When deploying the application with the container, how are the logs collected?
  6. How to handle the versioning process?

So your company is on the way to streamline the process of developing, testing, and deploying software to production? But you are confused because there are so many tools out there such as Kubernetes, the "CI/CD universe" and stuff, ain't you? No problem. You just need to get started with the example presented in this article. I laid out a simple configuration that leverages Docker [1], Github [2], and Jenkins [3] for a Python project. All the sample source code was kept at [4]. Okay, let's go!

Repository Directory Layout

Organize your application similar to the following directory structure:

apps/
........keycloak_flask/...
tests/
....test_something.py
Dockerfile
test-Dockerfile
requirements.txt
test-requirements.txt
docker-compose.yml
  • apps/: this directory contains your application's source code
  • tests/: contains the unit tests, all test files have to be named in this form: test_xyz.py. This is the requirement of Pytest [5]. I only put the dummy test cases for demonstration purposes.
  • Dockerfile: the main file to build the Docker image of your application that can be deployed on the development or production environment (run "anywhere"). You can install any packages or run any shell scripts that needed for your application. Please read [6] for more details.
  • test-Dockerfile: the file to build the image that has all the needed packages to test your application. The CI/CD tool, Jenkins, will use this to trigger the test automatically
  • requirements.txt: all the required Python packages to run your application
  • test-requirements.txt: all the required Python packages to test your application. In this example, I only used Pytest.
  • docker-compose.yml: the template file to build and deploy your application

Create a Jenkins job to test your application

The job simply runs a set of testing commands against your source code or even deploy it into an integration testing environment with Sonarqube for example.

Build and deploy your application in the development environment

Create a new Jenkins pipeline to build your source code and deploy it into an environment for testing purposes with the latest commit. Normally the job will be triggered by watching a separate branch of your git repository such as "dev".

Deploy your application in production

Similar to the previous step but this time your job should watch for any change in a stable branch of your git repository instead. It could be the "master" or "stable" branch. Basically, after you done testing on the development and/or staging environment, fixed all the found bugs or issues, you merge the commit into the stable branch to be deployed on production.

It's just a simple setup to get you started. Of-course, in reality, you have to carefully set up your workflow following your company or project's requirements.

That's it. Hope you enjoy it & Merry Christmas!

Comments