Writing a Docker image involves creating a Dockerfile, which is a text document that contains instructions for building an image. Once the Dockerfile is created, it can be used to build a Docker image using the docker build command. Here are the steps to create a Docker image:
Step 1: Create a Dockerfile
Create a new file named Dockerfile in a directory where you have your code files. Open the Dockerfile in a text editor and add the following lines:
FROM python:3.9-slim-buster
WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "app.py"]
Here, we are creating an image based on the python:3.9-slim-buster image, setting the working directory to /app, copying the requirements.txt file, installing the Python dependencies, copying the rest of the files, and setting the command to run app.py with Python.
Step 2: Build the Docker image
Open a terminal or command prompt and navigate to the directory where the Dockerfile is located. Run the following command to build the Docker image:
docker build -t my-image .
This command will create a new Docker image named my-image based on the Dockerfile in the current directory.
Step 3: Verify the Docker image
Once the build process completes successfully, you can verify that the Docker image is created by running the following command:
docker images
This command will show you a list of all the Docker images on your system. You should be able to see the my-image image in the list.
That's it! You have successfully created a Docker image. You can now use this image to create Docker containers and run your application.
Top comments (0)