Docker容器

一:什么是容器

如果容器理解为独立运行的一个或一组应用,以及它们的运行态环境。 而虚拟机则为可理解为跑在上面的应用。

二:创建容器

创建文件名为Dockerfile的空目录,将以下三个文件复制到其中。

1.添加文件

Dockerfile文件

# Use an official Python runtime as a parent image
FROM python:2.7-slim

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
ADD . /app

# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]

# Set proxy server, replace host:port with values for your servers
ENV http_proxy host:port
ENV https_proxy host:port

requirements.txt

Flask
Redis

app.py

from flask import Flask
from redis import Redis, RedisError
import os
import socket

# Connect to Redis
redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)

app = Flask(__name__)

@app.route("/")
def hello():
    try:
        visits = redis.incr("counter")
    except RedisError:
        visits = "<i>cannot connect to Redis, counter disabled</i>"

    html = "<h3>Hello {name}!</h3>" \
           "<b>Hostname:</b> {hostname}<br/>" \
           "<b>Visits:</b> {visits}"
    return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits)

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=80)
 

2.创建镜像

查看文件

#ls
Dockerfile        app.py            requirements.txt

在“Dockerfile”文件夹下运行下面命令,创建了一个以“friendlyhello”命名的Docker镜像。

#docker build -t friendlyhello .

3.查看新建镜像

新建的镜像在Docker 镜像registry中

#docker images

4.运行应用程序

运行该APP(应用程序),并将4000映射到80端口

#docker run -p 4000:80 friendlyhello

并在浏览器中输入http://localhost:4000,注意如果是win7则将localhost修改为IP

5.后台运行应用程序

#docker run -d -p 4000:80 friendlyhello

6.查看容器

# docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED
1fa4ab2cf395        friendlyhello       "python app.py"     28 seconds ago

7.停止容器

#docker container stop 1fa4ab2cf395

三:分享镜像

1.登录Docker

登录到本地计算机上公共的Docker注册表。如果没有账号,可以登录到https://cloud.docker.com/中注册

#docker login

2.标记镜像

形如docker tag image username/repository:tag,意思是用户john将镜像“ friendlyhello”上传至“get-started”存储库,标记为“part2”。
#docker tag friendlyhello john/get-started:part2

3. 查看镜像

# docker images

4.发布镜像

形如“docker push username/repository:tag”,镜像一旦上传将会被公开

#docker push john/get-started:part2

5.取出镜像

输入形如“docker run -p 4000:80 username/repository:tag”如下命令

#docker run -p 4000:80 john/get-started:part2

四:附录:

如果想知道更多容器相关命令可试着运行下列命令

docker build -t friendlyname .              # Create image using this directory's Dockerfile
docker run -p 4000:80 friendlyname          # Run "friendlyname" mapping port 4000 to 80
docker run -d -p 4000:80 friendlyname       # 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

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券