Docker基础操作

318 阅读3分钟
一.docker下载安装

Docker Hub下载地址(Mac)

二.创建docker镜像

本地创建一个空目录,并创建如下三个文件:

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
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"]

Dockerfile定义容器内环境中发生的事情

2.requirements.txt

Flask

requirements.txt定义了应用所用的依赖

3.app.py

from flask import Flask
import socket
import os

app = Flask(__name__)

@app.route('/')
def hello():
    html = "<h3>Hello {name}!</h3>" \
           "<b>Hostname:</b> {hostname}<br/>"           
    return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname())
    
if __name__ == "__main__":
    app.run(host='0.0.0.0', port=80)

app.py是所要运行的程序

生成镜像命令:

# -tag修改所生成镜像的名字为friendlyhello
docker build --tag=friendlyhello .

查看镜像:

docker image ls

可以看到镜像已经成功创建: 在这里插入图片描述

三.运行应用程序

运行程序命令:

# -p指定端口号映射,即将本机端口4000映射到容器的80端口
# 运行镜像时可以指定镜像名加版本,也可以直接指定imageId

docker run -p 4000:80 friendlyhello:latest
# docker run -p 4000:80 4999a7a365fb

浏览器访问url,验证服务是否成功启动

想要后台运行程序,加 -d 参数即可

docker run -d -p 4000:80 4999a7a365fb
四.Container操作

成功启动镜像后,程序实际上是在容器内运行的 查看容器:

# 当前正在运行的容器
docker container ls

# 查看所有容器
docker container ls --all

在这里插入图片描述

在容器内进行操作,并将操作结果保存到镜像

# 此处需指定containerId
docker exec -it 1fa4ab2cf395 /bin/sh
# 在容器内部新建了一个文件
root@1fa4ab2cf395:/app# touch test.txt
root@1fa4ab2cf395:/app# exit

# 将这个新建的文件提交到镜像中保存
docker commit 4ddf4638572d geektime/helloworld:v2

结束容器进程:

#此处需要指定 containerId
docker container stop 1fa4ab2cf395

删除容器:

#此处需要指定 containerId
docker container rm 1fa4ab2cf395

删除镜像:

#此处需要指定 imageId
docker container rm 4999a7a365fb
# 注意:删除镜像前需要停止其正在运行的容器并删除
5.上传镜像到Docker Hub

1.注册Docker Hub账户,并登陆(Docker Hub) 2.登陆本地docker公共注册表

docker login

2.可上传的镜像,其镜像名格式需为: DockerHub用户名/镜像名,方可上传镜像 故进行如下操作

# tag命令:Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
# 旧镜像名及版本号:friendlyhello:latest  
# 新镜像名及版本号:cactuslrg/friendlyhello:v1
docker tag friendlyhello:latest cactuslrg/friendlyhello:v1

在这里插入图片描述

3.上传镜像:

docker push cactuslrg/friendlyhello:v1

在这里插入图片描述

4.可在Docker Hub仓库上看到自己新上传的镜像 在这里插入图片描述

6.从远程仓库拉出并运行镜像

命令:

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

[外链图片转存失败(img-R4Z1GHwH-1567590212544)(evernotecid://9E570A26-2179-4237-B22E-716D5BDA6A79/appyinxiangcom/25888820/ENResource/p28)]

补充:

本文提到的docker命令:

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

docker命令汇总(忘记了直接--help即可)

**容器生命周期管理**
run
start/stop/restart
kill
rm
pause/unpause
create
exec
**容器操作**
ps
inspect
top
attach
events
logs
wait
export
port
**容器rootfs命令**
commit
cp
diff
**镜像仓库**
login
pull
push
search
**本地镜像管理**
images
rmi
tag
build
history
save
load
import
info|version
info
version