skip to Main Content

I have a directory named docker which contains the following two files:

docker:
      |_Dockerfile
      |_main.py

The Dockerfile looks like this:

#Specifying the base image
FROM python:3.10
#here the dockerfile is pulling the python 3.10 from docker hub which already has python installed so we have all the things we need to have python in our container.

ADD main.py.
#Here we added the python file that we want to run in docker and define its location.

RUN pip install requests pygame
#Here we installed the dependencies, we are using the pygame library in our main.py file so we have to use the pip command for installing the library

CMD [ "python3" "./main.py" ]
#lastly we specified the entry command this line is simply running python ./main.py in our container terminal

The main.py file looks like this:

# Simple pygame program
# Import and initialize the pygame library
import pygame
pygame.init()

# Set up the drawing window
screen = pygame.display.set_mode([500, 500])

# Running the game until the user asks to quit
running = True
while running:

    # creating an if statement for the close button
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # Filling the background color with a gray
    screen.fill((55, 55, 55))

    # Drawing a solid purple circle in the center
    pygame.draw.circle(screen, (0, 0, 251), (251, 251), 74)
    # Flip the display
    pygame.display.flip()

# Done! Time to quit.
pygame.quit()

I opened my cmd (I’m on Windows 10) and navigated to the docker directory and executed the following command:

docker image build -t python-game .

This is output I received

[+] Building 0.1s (2/2) FINISHED
 => [internal] load build definition from Dockerfile                                                               0.0s
 => => transferring dockerfile: 683B                                                                               0.0s
 => [internal] load .dockerignore                                                                                  0.0s
 => => transferring context: 2B                                                                                    0.0s
failed to solve with frontend dockerfile.v0: failed to create LLB definition: dockerfile parse error line 5: ADD requires at least two arguments, but only one was provided. Destination could not be determined.

2

Answers


  1. Just plenty of invalid syntax mistakes. Instead of

    ADD main.py.
    #Here we added the python file that we want to run in docker and define its location.
    
    RUN pip install requests pygame
    #Here we installed the dependencies, we are using the pygame library in our main.py file so we have to use the pip command for installing the library
    
    CMD [ "python3" "./main.py" ]
    

    Use

    ADD main.py /main.py
    
    RUN pip install requests pygame
    
    CMD [ "python3", "/main.py" ]
    
    Login or Signup to reply.
  2. You’re missing the second argument, the destination directory, in ADD. Try the following, for example ./ copies main.py to the current directory.

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