0

I have a Python project and now I am trying to create a Makefile that should run specific commands, such as apt-get, and access variable values that are passed to make commands as arguments. Below is my Makefile code:

VENV = venvs
PYTHON = $(VENV)/bin/python3
PIP = $(VENV)/bin/pip

run : $(VENV)/bin/activate 
    $(PYTHON) jobs/first_file.py

$(VENV)/bin/activate: 
    docker run -it python:3.8-buster /bin/bash
    python3 -m venv $(VENV)
    $(PIP) install --upgrade pip
    $(PIP) install -r requirements.txt

clean :
    rm -rf __pycache__
    rm -rf $(VENV)   

Now, my intention is to invoke the docker image and run the pip commands and later on all the further commands on that docker image. It also includes connecting to an AWS account whose values would be passed in the make command as arguments.

But when I run make in the project's root directory. It just connects to the docker bash and does nothing. What exactly am I missing here?

RushHour
  • 494
  • 6
  • 25
  • See: https://stackoverflow.com/a/30412922/939557 – MadScientist May 15 '23 at 18:26
  • Hello @RushHour, do you have an example about how are you exactly running your makefile? – Franco Gil May 15 '23 at 18:41
  • You might look at Docker's image system and writing a Dockerfile to produce a custom image. Since a Docker image is temporary, it doesn't usually make sense to install software in a running container, since that work will get lost as soon as the container exits. Best-practice workflows don't generally run additional commands in containers, any more than you'd "go inside" `make` and run additional administrative commands there. – David Maze May 15 '23 at 19:30
  • Your point makes absolute sense @DavidMaze but right now I have to fix the problem then I can go ahead with the best practice as suggested by you – RushHour May 16 '23 at 09:38
  • @FrancoGil I am running `make` command inside the root directory of my project. – RushHour May 16 '23 at 09:38

3 Answers3

1

Pass commands as input to another command (su, ssh, sh, etc) explains the basic problem with your syntax. The other commands will run after bash exits. A minimal fix would look like

$(VENV)/bin/activate: 
    docker run -it python:3.8-buster /bin/bash -c '\
    python3 -m venv $(VENV); \
    $(PIP) install --upgrade pip; \
    $(PIP) install -r requirements.txt'

However, you also need to understand that docker run creates a new container, runs the commands, and then exits the container. All your changes will be lost after that.

If I'm able to guess your intentions correctly, these commands should simply go in your Dockerfile instead. That will create an image with those changes which you can then docker run in a new container as many times as you like.

Alternatively, create scripts (or a Makefile if you like) inside the container, start it up once, with docker run (make sure its starting point CMD is a command or script which runs forever, or until you separately tell it to shut down) and run them with docker exec containername make foo or whatever.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • I think I will have to go with the docker file solution since there are multiple targets that have bash commands interdependent on each other so I don't think `-c` will work. – RushHour May 16 '23 at 09:41
0

Yes I think it cannot work, just like when you want to run a bunch of linux commands after a ssh call, you need to pass a script file. The easiest way to do that I think, would be to put all the command in a bash files, and call docker with something like that :

docker run -it python:3.8-buster /bin/bash -c script.sh

I didn't try, might be some syntax error in the exact command I put.

Meeds
  • 41
  • 3
0

You're taking a rather interesting approach here. What you should consider instead is creating a Dockerfile to build your image, and put those commands into the dockerfile itself.

Robert Hafner
  • 3,364
  • 18
  • 23
  • Can you help me with any links or samples? I am pretty much new so any reference would help. – RushHour May 16 '23 at 09:39
  • Here's a couple of tutorials- https://www.howtogeek.com/733522/docker-for-beginners-everything-you-need-to-know/ https://naiveskill.com/dockerfile-tutorial-for-beginners/ – Robert Hafner May 16 '23 at 12:42