docker食用食谱(一) —— 概论与命令
2019-07-09 本文已影响0人
谁有羊毛
docker食用主干
众所周知docker是海洋蓝色生物,比较难食用,故在此。。。
一、如何使用
- 到一个空目录下写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
COPY . /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"]`
创建app.py文件,其中 app.py 内容为:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run(host='0.0.0.0')
二、docker常用命令
- 构建镜像
# Create image using this directory's Dockerfile
docker build -t [tag_name] .
docker build -t friendlyhello .
- 运行一个镜像为容器
docker run -d -p 4000:80 friendlyhello
# 此时可以访问本地的 127.0.0.1:4000 端口,看看一个在docker 中运行的web服务器
- 停止容器
docker container ls
docker container stop <hash>
- 其他命令
# Run "friendlyhello" mapping port 4000 to 80 前台运行
docker run -p 4000:80 friendlyhello
# Same thing, but in detached mode 后台运行
docker run -d -p 4000:80 friendlyhello
# 进入一个正在运行到容器
docker exec -it friendlyhello bash
# 查看所有运行的容器 List all running containers
docker container ls
# 查看所有容器 List all containers, even those not running
docker container ls -a
# 停止一个容器 Gracefully stop the specified container
docker container stop <hash>
# 强制停止一个容器 Force shutdown of the specified container
docker container kill <hash>
# 删除本地容器 Remove specified container from this machine
docker container rm <hash>
# 删除所有容器 Remove all containers
docker container rm $(docker container ls -a -q)
# 删除未运行的容器
docker container prune
# 列出所有的镜像 List all images on this machine
docker image ls -a
# 删除镜像 Remove specified image from this machine
docker image rm <image id>
# 删除所有的镜像 Remove all images from this machine
docker image rm $(docker image ls -a -q)
# 挂载数据卷 映射本机目录
docker run -v /local/data:/data -t image_tag_name
# 登录dockerhub Log in this CLI session using your Docker credentials
docker login
# 给镜像打标签 Tag <image> for upload to registry
docker tag <image> username/repository:tag
# 上传镜像到远程仓库 Upload tagged image to registry
docker push username/repository:tag
# 从远程拉镜像运行 Run image from a registry
docker run username/repository:tag