To build a Docker image in Python, you need to create a Dockerfile
in your project directory with the instructions for building the image. Here's an example Dockerfile
.
main.py521 chars21 lines
Here's what each instruction does:
FROM
specifies the base image for the Docker image. In this case, we are using the official Python 3.6 slim-stretch image.WORKDIR
sets the working directory to /app
in the container.COPY
copies the contents of the current directory (our Python code) into the /app
directory in the container.RUN
runs the specified command (pip install ...
) in the container.EXPOSE
specifies that port 80 in the container should be exposed.ENV
sets the environment variable NAME
to World
.CMD
specifies the command to run when the container is launched.To build the Docker image, navigate to your project directory in the terminal and run the following command:
26 chars2 lines
This will build the Docker image using the Dockerfile
in the current directory and tag it as myimage
.
gistlibby LogSnag