Câu hỏi phỏng vấn Docker
Câu hỏi

What is the difference between ‘docker run’ and ‘docker create’?

Câu trả lời

The difference between docker run and docker create lies in their functionality and the stages of the container lifecycle they address.

docker create

  • Purpose: The docker create command is used to create a new container from a specified image without starting it.
  • Functionality: When you run docker create, Docker performs the following actions:
    • Creates a writable container layer over the specified image.
    • Prepares the container for running the specified command.
    • Outputs the container ID.
  • Use Case: This command is useful when you want to set up a container configuration ahead of time without immediately starting it. For example, you might want to create a container, configure it, and then start it later when needed.
  • Example:
    docker create --name mycontainer ubuntu

docker run

  • Purpose: The docker run command is used to create and start a new container from a specified image in one step.
  • Functionality: When you run docker run, Docker performs the following actions:
    • Creates a writable container layer over the specified image (similar to docker create).
    • Starts the container by running the specified command.
    • Optionally, it can pull the image from a registry if it is not available locally.
  • Use Case: This command is used when you want to quickly create and start a container in a single step. It is the most common command for running containers.
  • Example:
    docker run --name mycontainer ubuntu

Key Differences

  • Lifecycle Stage: docker create only creates the container, while docker run creates and starts the container.
  • Immediate Execution: docker run immediately executes the container's main process, whereas docker create does not start the container automatically.
  • Command Execution: With docker run, you can specify commands and arguments to be executed when the container starts. With docker create, you need to use docker start to run the container after creation.

Example Workflow

  1. Create a Container:
    docker create --name mycontainer ubuntu
    This command creates a c...
junior

junior

Gợi ý câu hỏi phỏng vấn

middle

How will you monitor Docker in production?

senior

Can you explain dockerfile ONBUILD instruction?

junior

Do I lose my data when the Docker container exits?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào