悉野小楼

导航

docker常用命令

跳过了docker的安装, 可以自己搜索安装.

目录下面需要存在一个 Dockerfile的文件, 内容类似如下:

FROM python:3.11-slim

WORKDIR /app

RUN pip3 install flask
RUN pip3 install Flask-Cors
RUN pip3 install Flask-SQLAlchemy
RUN pip3 install Flask-HTTPAuth
RUN pip3 install requests
RUN pip3 install gunicorn==21.2.0
RUN pip3 install gevent==23.7.0
RUN pip3 install PyMySQL==1.1.1
RUN pip3 install pycryptodome==3.18.0
RUN pip3 install PyJWT==2.6.0

COPY . .

CMD ["gunicorn", "-w 1", "-k gevent", "-t 180", "-b 0.0.0.0:9001", "app:app"]

在当前目录中运行

1. docker buildx build --platform=linux/amd64 -t myname:taghello .

将当前目录打包成一个myname:taghello的镜像,  最后的.表示当前目录

2. docker ps -a
查看所有正在运行的镜像

3.docker run -it -d -p 9991:9001 镜像名

根据镜像名直接创建一个容器并运行, 并把docker容器子系统里的9991端口映射到主机的9001端口

下面例子中已经运行一个topoftop:devapi容器, 执行打包成docker, 打包成一个新镜像, 旧镜像的REPOSITORY   TAG会变成空, 只有最新镜才用这指定的名字, 旧的镜象名字变空, 但原容器还原运行.

停了原容器, 运行新的镜像创建新容器并运行, 删除一个旧容器, 删除一个旧镜像

[ec2-user@ip-172-31-27-225 devapi]$ docker buildx build --platform=linux/amd64 -t topoftop:devapi .
[+] Building 1.9s (18/18) FINISHED                                                                                                                                                                                                                             docker:default
 => [internal] load build definition from Dockerfile                                                                                                                                                                                                                     0.0s
 => => transferring dockerfile: 533B                                                                                                                                                                                                                                     0.0s
 => [internal] load metadata for docker.io/library/python:3.11-slim                                                                                                                                                                                                      1.7s
 => [internal] load .dockerignore                                                                                                                                                                                                                                        0.0s
 => => transferring context: 2B                                                                                                                                                                                                                                          0.0s
 => [ 1/13] FROM docker.io/library/python:3.11-slim@sha256:e8381c802593deb0c4d25bd3f4e05e94382f6bf33090de22679fc7488cd68bbb                                                                                                                                              0.0s
 => => resolve docker.io/library/python:3.11-slim@sha256:e8381c802593deb0c4d25bd3f4e05e94382f6bf33090de22679fc7488cd68bbb                                                                                                                                                0.0s
 => [internal] load build context                                                                                                                                                                                                                                        0.0s
 => => transferring context: 26.32kB                                                                                                                                                                                                                                     0.0s
 => CACHED [ 2/13] WORKDIR /app                                                                                                                                                                                                                                          0.0s
 => CACHED [ 3/13] RUN pip3 install flask                                                                                                                                                                                                                                0.0s
 => CACHED [ 4/13] RUN pip3 install Flask-Cors                                                                                                                                                                                                                           0.0s
 => CACHED [ 5/13] RUN pip3 install Flask-SQLAlchemy                                                                                                                                                                                                                     0.0s
 => CACHED [ 6/13] RUN pip3 install Flask-HTTPAuth                                                                                                                                                                                                                       0.0s
 => CACHED [ 7/13] RUN pip3 install requests                                                                                                                                                                                                                             0.0s
 => CACHED [ 8/13] RUN pip3 install gunicorn==21.2.0                                                                                                                                                                                                                     0.0s
 => CACHED [ 9/13] RUN pip3 install gevent==23.7.0                                                                                                                                                                                                                       0.0s
 => CACHED [10/13] RUN pip3 install PyMySQL==1.1.1                                                                                                                                                                                                                       0.0s
 => CACHED [11/13] RUN pip3 install pycryptodome==3.18.0                                                                                                                                                                                                                 0.0s
 => CACHED [12/13] RUN pip3 install PyJWT==2.6.0                                                                                                                                                                                                                         0.0s
 => [13/13] COPY . .                                                                                                                                                                                                                                                     0.1s
 => exporting to image                                                                                                                                                                                                                                                   0.0s
 => => exporting layers                                                                                                                                                                                                                                                  0.0s
 => => writing image sha256:4359cd9fb9d695371ddafbd2f84a97612f33f5cf1c4c1cd94b34add7e8fde9b1                                                                                                                                                                             0.0s
 => => naming to docker.io/library/topoftop:devapi                                                                                                                                                                                                                       0.0s
[ec2-user@ip-172-31-27-225 devapi]$ docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED       STATUS       PORTS                                       NAMES
032e399ea3f7   31ea28ea23af   "gunicorn '-w 1' '-k…"   2 weeks ago   Up 2 weeks   0.0.0.0:9991->9001/tcp, :::9991->9001/tcp   keen_gould
[ec2-user@ip-172-31-27-225 devapi]$ docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
topoftop     devapi    4359cd9fb9d6   23 seconds ago   228MB
<none>       <none>    31ea28ea23af   2 weeks ago      228MB
[ec2-user@ip-172-31-27-225 devapi]$ docker stop 032e399ea3f7
032e399ea3f7
[ec2-user@ip-172-31-27-225 devapi]$ docker run -it -d -p 9991:9001 topoftop:devapi
68b7d79cd84603b1814991731e4528b6a3e110f527c879d3a55799989eeaef0e
[ec2-user@ip-172-31-27-225 devapi]$ docker ps -a
CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS                      PORTS                                       NAMES
68b7d79cd846   topoftop:devapi   "gunicorn '-w 1' '-k…"   14 seconds ago   Up 13 seconds               0.0.0.0:9991->9001/tcp, :::9991->9001/tcp   inspiring_agnesi
032e399ea3f7   31ea28ea23af      "gunicorn '-w 1' '-k…"   2 weeks ago      Exited (0) 17 seconds ago                                               keen_gould
[ec2-user@ip-172-31-27-225 devapi]$ docker rm 032e399ea3f7
032e399ea3f7
[ec2-user@ip-172-31-27-225 devapi]$ docker rmi 31ea28ea23af
Deleted: sha256:31ea28ea23afa206ba6cc03485c9be6c82e5a1bbdd111792a203066c5851dece
[ec2-user@ip-172-31-27-225 devapi]$ docker ps -a
CONTAINER ID   IMAGE             COMMAND                  CREATED              STATUS              PORTS                                       NAMES
68b7d79cd846   topoftop:devapi   "gunicorn '-w 1' '-k…"   About a minute ago   Up About a minute   0.0.0.0:9991->9001/tcp, :::9991->9001/tcp   inspiring_agnesi

 

posted on 2024-12-02 15:24  悉野  阅读(5)  评论(0编辑  收藏  举报