Learn how to use dockerfile
The following content is partly generated by gpt. There may be problems with the description, but I will test the code part.
1. Demand
For a docker, such as python, we need to have np when it is built into a container. There are two methods:
- Pull python, and after running, pip install numpy in it, and then package and save the container.
- When pulling python, run pip install numpy together. When running, there will be numpy.
2. dockerfile和docker-compose
Note: In order to achieve the above functions, dockerfile is used to define and build the instruction script of a single Docker image, that is, pull; docker-compose is used to define and run the tools and configuration files of multi-container Docker applications, providing a unified way to run.
Dockerfile:
Purpose: A Dockerfile is used to define the content and configuration of a single container. It provides a way to create a new Docker image from scratch, or customize a new image based on an existing Docker image.
Main operations: Define, build and configure a Docker container. Example uses: installing software, setting environment variables, defining working directories, setting entry points, etc. Usage: Use the docker
build command to create a new Docker image from a Dockerfile.
docker-compose:
Purpose: docker-compose is used to define and run multi-container Docker applications. It provides a declarative way to define a set of associated containers, networking between them, volumes and other configuration.
Main operations: Define, connect and manage multiple containers.
Example uses: running multi-service applications (such as frontend, backend, and database) in a local development environment, setting up network connections between services, defining and mounting volumes, and more.
Usage: Use the docker-compose up command to start the service based on the docker-compose.yml file.
3. Dockerfile
Use Dockerfile to implement python with numpy
- Create a Dockerfile, the name must be strict
# 使用python:latest作为基础镜像
FROM python:latest
# 安装numpy
RUN pip install numpy
- Create a mirror of the above content
# docker build -t image-name:latest <Dockerfile的路径>
docker build -t py-with-np:v1 .
# 使用 docker images查看已有镜像
- Run the above docker
docker run -it -v /opt/appdata/python:/py --name pytest py-with-np:v1
# 使用docker ps -a查看运行的容器
4. docker-compose
The above dockerfile can only create images first and then run them, which is not elegant.
docker-compose solves this problem
- Create Dockerfile:
# 使用python:latest作为基础镜像
FROM python:latest
# 安装numpy
RUN pip install numpy
- Create docker-compose.yml:
Simple version, start python directly after running
version: '3'
services:
hello-service:
build:
context: .
dockerfile: Dockerfile
image: py-with-np:v6
volumes:
- /opt/appdata/python:/py
ports:
- "8080:8080"
command: python /py/test.py
stdin_open: true
tty: true
Explanation
: build: . instructs docker-compose to build the Docker image based on the Dockerfile in the current directory. image:
py-with-np:v1 specifies the name and label of the built image. volumes is a volume mapping, the same as the -v parameter in the previous docker run command
.
If you want to run a container in docker-compose and keep it interactive, such as an interactive Python shell, you should add a stdin_open and tty attribute to the docker-compose.yml file, both of which are set to true. This corresponds to the -i and -t options of docker run.
- Run the container using docker-compose:
First, go to the directory where docker-compose.yml is located on the command line. Next, start the service using the following command:
docker-compose up --build
docker-compose up starts the service.
The –build parameter ensures that the service’s image is built or rebuilt before running the service.
4. Question
-What should I do?
Because stdin_open: true tty: true was added to docker-compose.yml, but it still cannot be started. You can
only use docker run -it image name, which is used for the time being.
5. Delete unnecessary images and containers
# 查看容器
docker ps -a
# 停止容器
docker stop ID
# 删除容器
docker rm ID
# 查看镜像
docker images
# 删除镜像
docker rmi ID # 但是可能两个images有相同的ID
docker rmi name:V1