skip to Main Content

I have to build a simple app which reads a text file and process it’s content (like remove multiple spaces, process words etc) but my I am confused about the first part of my homework.

"Initialize a git repository in a docker container then implement an app…."

I use Debian, I installed docker and git and I studied about it. From what I read I have to create a Dockerfile which will contain some instructions then I build the image and then run the container, run?

But I am still confused, what is the order of these thigs? Can I go firstly and write the app in Intelij and then to create that Dockerfiler? Or I have to create first the container then to code the app? But how I build the container? I read a lot about this, can you give me some advice? I mention that after every app "task" (read text file, process text etc) I have to execute git add, git commit and git push (if it helps for answer)

2

Answers


  1. If the instruction says to "Initialize a Git repository in a docker container" then you are expected to:

    1. run e.g. a Debian container
    2. if Git is not present install it
    3. initialize the repo
    4. write your app
    5. submit homework

    You could:

    docker run 
    --interactive --tty --rm 
    --name=homework 
    --volume=${PWD}/homework:/homework 
    --workdir=/homework 
    debian:buster-slim
    

    This will run a Debian "buster" image as a container and should (!) give you a shell prompt in the container.

    A directory /homework in the container will be mapped to your host machine’s ${PWD}/homework and you will be in the /homework directory when the container starts. This means that you won’t lose your work if you exit the container.

    From within the container’s prompt:

    # pwd
    /homework
    
    # git
    bash: git: command not found
    
    # apt update && apt install -y git
    ...
    done.
    
    # git
    usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
               [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
               [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
               [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
               <command> [<args>]
    
    # git init
    Initialized empty Git repository in /homework/.git/
    

    Notes

    • If you exit the container, you can rerun the docker run ... command to return to it.
    • When you exist the container, you can ls -la ${PWD}/homework to see the .git directory (at least) persisted on your host.
    • Ensure you run it from the same directory where it created ${PWD}/homework. Or revise the --volume=...
    Login or Signup to reply.
  2. I’d recommend an overall workflow of

    1. Build the application, without Docker; then
    2. Package it in a Docker image (if it makes sense to).

    You should be able to build the application totally normally. Whatever language you’re using to build the application, make sure to use its normal packaging tools. For example, your package.json/Gemfile/requirements.txt/go.mod should list out all of the library dependencies your application needs to run. Run it locally, write appropriate unit tests for it, and generally build something that works.

    Once it works, then push it into Docker. You’ll need to write a Dockerfile that builds the image. A generic recipe for this is

    FROM language-base-image     # python:3.9, node:14, ...
    WORKDIR /app
    COPY dependencies-file .     # requirements.txt, package.json, ...
    RUN install the dependencies # pip install, npm install, ...
    COPY . .
    RUN build the application    # npm run build, ...
    CMD ./the_application        # npm run start, ...
    

    You should then be able to docker build an image, and docker run a container from the resulting image. The Docker documentation includes a sample application that runs through this sequence.

    Note in particular that the problem task of "read a text file" is substantially harder in Docker than without. You need to use a bind mount to give access to the host filesystem to the container, and then refer to the container-side path. For example,

    docker run --rm -v $PWD/data:/data my-image 
      ./the_application --input /data/file.txt
    

    I would not bother trying to use Docker as my primary development environment, especially for an introductory project. Docker is designed as an isolation system and it’s intentionally tricky to work with host files from a container, and vice versa. Especially if you can use a fairly routine programming language that you can easily install with apt-get or brew, and you don’t have tricky host-library dependencies, it’s substantially easier to do most of your development in an ordinary host build environment use Docker only at a late stage.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search