docker学习笔记--Container

本文详细介绍了如何使用Docker tag命令为镜像添加标签,以便于版本管理和回滚。同时,阐述了如何将带有特定标签的镜像推送到远程仓库,并从仓库中拉取和运行镜像。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

制作镜像

https://www.cnblogs.com/linjiqin/p/8669360.html

docker tag

https://www.cnblogs.com/pzk7788/p/10180919.html

docker tag 用于给镜像打标签,语法如下:

docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

① 比如我现在有一个 centos 镜像:

[root@localhost ~]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              1e1148e4cc2c        2 weeks ago         202MB

② 我对 centos 进行开发,开发了第一个版本,我就可以对这个版本打标签,打完标签后会生成新的镜像:

[root@localhost ~]$ docker tag centos centos:v1
[root@localhost ~]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              1e1148e4cc2c        2 weeks ago         202MB
centos              v1                  1e1148e4cc2c        2 weeks ago         202MB

③ 我继续对 centos 进行开发,开发了第二个版本,继续打标签:

[root@localhost ~]$ docker tag centos centos:v2
[root@localhost ~]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              1e1148e4cc2c        2 weeks ago         202MB
centos              v1                  1e1148e4cc2c        2 weeks ago         202MB
centos              v2                  1e1148e4cc2c        2 weeks ago         202MB

④ 以此类推,每开发一个版本打一个标签,如果以后我想回滚版本,就可以使用指定标签的镜像来创建容器:

[root@localhost ~]$ docker run -itd centos:v1

 

Publish the image

上传前记得login

https://stackoverflow.com/questions/41984399/denied-requested-access-to-the-resource-is-denied-docker

Upload your tagged image to the repository:

docker push username/repository:tag

Once complete, the results of this upload are publicly available. If you log in to Docker Hub, you see the new image there, with its pull command.

Pull and run the image from the remote repository

From now on, you can use docker run and run your app on any machine with this command:

docker run -p 4000:80 username/repository:tag

Stop image and Run image 

PS E:\MY_CODE\所有test文件\docker_test> docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                  NAMES
dfb0e87415e4        friendlyhello       "python app.py"     25 minutes ago      Up 25 minutes       0.0.0.0:4000->80/tcp   jolly_jepsen
PS E:\MY_CODE\所有test文件\docker_test> docker stop dfb0e87415e4
dfb0e87415e4

PS E:\MY_CODE\所有test文件\docker_test> docker run -p 4000:80 friendlyhello
 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://0.0.0.0:80/ (Press CTRL+C to quit)
PS E:\MY_CODE\所有test文件\docker_test> curl http://localhost:4000


StatusCode        : 200
StatusDescription : OK
Content           : <h3>Hello World!</h3><b>Hostname:</b> e2cd39223f69<br/><b>Visits:</b> <i>cannot connect to Redis, counter disabled</i>
RawContent        : HTTP/1.0 200 OK
                    Content-Length: 118
                    Content-Type: text/html; charset=utf-8
                    Date: Wed, 24 Apr 2019 03:33:56 GMT
                    Server: Werkzeug/0.15.2 Python/2.7.16

                    <h3>Hello World!</h3><b>Hostname:</b> e2cd39...
Forms             : {}
Headers           : {[Content-Length, 118], [Content-Type, text/html; charset=utf-8], [Date, Wed, 24 Apr 2019 03:33:56 GMT], [Server, Werk
                    zeug/0.15.2 Python/2.7.16]}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : mshtml.HTMLDocumentClass
RawContentLength  : 118

Summary

https://docs.docker.com/get-started/part2/

Here is a list of the basic Docker commands from this page, and some related ones if you’d like to explore a bit before moving on.

docker build -t friendlyhello .  # Create image using this directory's Dockerfile
docker run -p 4000:80 friendlyhello  # Run "friendlyhello" mapping port 4000 to 80
docker run -d -p 4000:80 friendlyhello         # Same thing, but in detached mode
docker container ls                                # List all running containers
docker container ls -a             # List all containers, even those not running
docker container stop <hash>           # Gracefully stop the specified container
docker container kill <hash>         # Force shutdown of the specified container
docker container rm <hash>        # Remove specified container from this machine
docker container rm $(docker container ls -a -q)         # Remove all containers
docker image ls -a                             # List all images on this machine
docker image rm <image id>            # Remove specified image from this machine
docker image rm $(docker image ls -a -q)   # Remove all images from this machine
docker login             # Log in this CLI session using your Docker credentials
docker tag <image> username/repository:tag  # Tag <image> for upload to registry
docker push username/repository:tag            # Upload tagged image to registry
docker run username/repository:tag                   # Run image from a registry

 No matter where docker run executes, it pulls your image, along with Python and all the dependencies from requirements.txt, and runs your code. It all travels together in a neat little package, and you don’t need to install anything on the host machine for Docker to run it.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值