Skip to main content

How to create a Docker image?

Creating a Docker image is usually done through a Dockerfile. The process consists of three steps:


1. Create a Dockerfile

For example, for a simple Python application:

Dockerfile
FROM python:3.10 WORKDIR /app COPY . . RUN pip install -r requirements.txt CMD ["python", "app.py"]

2. Build the image with a command

While in the folder with the Dockerfile, run:

bash
docker build -t myapp:1.0 .

Where:

  • docker build - the build command
  • -t myapp:1.0 - the name and tag of the image
  • . - the build context (the current folder)

3. Check and run the container

Running a container from the built image:

bash
docker run --name mycontainer myapp:1.0

Summary

ActionCommand
Create an imagedocker build -t name:tag .
List imagesdocker images
Run a containerdocker run name:tag

If needed, I can show an even simpler example, an example for Node.js, or explain each line of the Dockerfile one by one. Which one shall we go with?

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.