DEV Community

Chigozie Oduah
Chigozie Oduah

Posted on • Originally published at ghoulkingr.hashnode.dev

Developing a projects in docker containers

One of Docker’s biggest advantages is reproducible builds. With it, you can bundle your project together with the exact environment it needs to run.

This becomes invaluable when your project grows, when you’re collaborating with other developers, when you need a quick-spinup development environment, or even when you want non-developers to try your work without a complicated setup.

In this article, I’ll walk you through setting up your development environment inside a docker container to get the benefits of autocompletes and suggestions while maintaining the consistent environments that docker containers provide.

Prerequisites and assumptions

This article assumes that the IDE you want to set up to develop in the container is VS code, and that you have the Docker CLI & Desktop installed on your system and running.

What project are we working with?

Let’s start by creating a new main.py file and pasting the following into it:

from flask import Flask, request
from collections import defaultdict

app = Flask(__name__)

storage = defaultdict(str)

@app.get('/<key>')
def get_key(key: str):
   return storage[key]

@app.post('/<key>')
def store_key(key: str):
   storage[key] = request.data.decode()
   return 'success'

if __name__ == "__main__":
   app.run(debug=True, host='0.0.0.0', port=8080)
Enter fullscreen mode Exit fullscreen mode

This is a simple flask app you can get running by following these steps:

  1. Install flask:
pip install flask
Enter fullscreen mode Exit fullscreen mode
  1. Run the code
python main.py
Enter fullscreen mode Exit fullscreen mode

All this project does is it allows you to store a string of text in a route using POST requests that you can retrieve using a GET request. For example:

$ curl -X POST -d 'Show me the way' 127.0.0.1:8080/show -H "Content-Type: text/plain" 
success
$ curl http://127.0.0.1:8080/show
Show me the way
Enter fullscreen mode Exit fullscreen mode

To finish up this project, create a requirements.txt file and paste the following into it:

Flask==3.1.1
Enter fullscreen mode Exit fullscreen mode

Step 1: Writing the dockerfile

This step allows us to containerize our projects, and run it. All you need to do in this in this step is to create a Dockerfile file in the project directory and paste the following into it:

FROM python:3.13.5-bullseye

WORKDIR /code

COPY requirements.txt .
RUN pip install -r requirements.txt
EXPOSE 8080

CMD [ "sleep", "infinity" ]
Enter fullscreen mode Exit fullscreen mode

Here’s a break down of the file:

  1. Use python:3.13.5-bullseye as the project’s base container.
FROM python:3.13.5-bullseye
Enter fullscreen mode Exit fullscreen mode
  1. Create a work directory in the container called code in the container (this is where all your project files would be mounted).
WORKDIR /code
Enter fullscreen mode Exit fullscreen mode
  1. Copy the requirements.txt file to the work directory.
COPY requirements.txt .
Enter fullscreen mode Exit fullscreen mode
  1. Install the project’s dependencies.
RUN pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode
  1. Expose the port 8080 outside the container.
EXPOSE 8080
Enter fullscreen mode Exit fullscreen mode
  1. Start the container and keep awake indefinitely.
CMD [ "sleep", "infinity" ]
Enter fullscreen mode Exit fullscreen mode

Since we want to create the container for development, keeping it awake allows us to develop in it without it shutting down in the middle of development.

Step 2: Writing the compose file

The compose file allows us to configure the project container in the ways that will allow us to develop in it. So let’s begin by creating a compose.yaml file in the project directory and paste the following into it:

services:
  project:
    build: .
    ports:
      - 8080:8080
    volumes:
      - type: bind
        source: .
        target: /code
Enter fullscreen mode Exit fullscreen mode

What does this compose file do?

First, it tells docker to create a new service called project for our project.

services:
  project:
    # ...
Enter fullscreen mode Exit fullscreen mode

Next, it tells docker what folder to look for the Dockerfile in.

# …
    build: .
    # …
Enter fullscreen mode Exit fullscreen mode

Next, it exposes the container’s port 8080 to the host environment:

# …
    ports:
      - 8080:8080
    # …
Enter fullscreen mode Exit fullscreen mode

Finally, it bind-mounts the project directory into the docker container’s work directory (the “code” folder).

# …
    volumes:
      - type: bind
        source: .
        target: /code
Enter fullscreen mode Exit fullscreen mode

Step 3: Setting up the dev container

To complete this step, install the VS Code Dev Containers extension. VS Code to open your project in docker containers.

Once the extension is installed, create a new file in .devcontainer/devcontainer.json, and paste the following into it.

{
    "name": "Containerized project",
    "dockerComposeFile": "../compose.yaml",
    "workspaceFolder": "/code",
    "service": "project",
    "extensions": [
        "ms-python.python"
    ]
}
Enter fullscreen mode Exit fullscreen mode

Once you’ve pasted the contents into the file, open VS code’s command palette and type “Open Folder in Container” and select the Dev Containers option.

This will open a file picker to select the folder you want to open in the container, In this file picker, stay in (or navigate to) the project folder and open.

Next, VS Code will begin setting up the project container.

Once this is done, you will be able to work with the project files in the container like usual, and you would also have VSCode’s autocompletes, intellisense and suggestions with the packages that are installed in the container.
Conclusion
Being able to set up development environments in docker containers has saved me a lot of time in a surprising amount of scenarios, and I can’t find myself developing a multi-service project without docker.

You can learn more about how to extend Dev Container setups further by going through the docker compose documentation, or through the Dev Containers extension page.

Thanks for reading!

Top comments (0)