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

Explain basic Docker usage workflow

Câu trả lời

To explain the basic Docker usage workflow, let's break down the process into several key steps. This workflow typically involves coding your application, creating Dockerfiles, building Docker images, running containers, and managing these containers. Here’s a detailed explanation:

1. Code Your Application

The first step in the Docker workflow is to develop your application. This involves writing the source code and ensuring that it works as expected in your local development environment.

2. Write Dockerfile(s)

A Dockerfile is a text file that contains a series of instructions on how to build a Docker image for your application. It specifies the base image, dependencies, configuration settings, and the commands to run your application. Here’s an example of a simple Dockerfile for a Python application:

# syntax=docker/dockerfile:1
FROM python:3.10-alpine
WORKDIR /code
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "app.py"]

3. Build Docker Images

Once you have your Dockerfile, you can build a Docker image using the docker build command. This image is a snapshot of your application and its environment, which can be used to create containers.

docker build -t my-python-app .

4. Run Containers

With the Docker image built, you can now run it as a container. A container is an instance of the Docker image that runs your application in an isolated environment. Use the docker run command to start a container:

docker run -d -p 5000:5000 my-python-app

In this command:

  • -d runs the container in detached mode (in the background).
  • -p 5000:5000 maps port 5000 of the host to port 5000 of the container.

5. Test Your Application

After running the container, you can test your application to ensure it works as expected. This involves accessing the application through the mapped port and verifying its functionality.

6. Manage Containers

Docker provides several commands to manage running containers. For example:

  • docker ps lists all running containers.
  • docker stop <container_id> stops a running container.
  • docker rm <container_id> removes a stopped container.

7. Push to Repository

If you want to share your Docker image, you can push it to a Docker registry like Docker Hub. First, tag your image, then push it:

docker tag my-python-app myusername...
middle

middle

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

senior

What is the difference between Docker RUN, CMD and ENTRYPOINT?

middle

What is the difference between “expose” and “publish” in Docker?

middle

What is the purpose of EXPOSE command in Dockerfile?

Bình luận

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

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