Dockerfile 是一个文本文件,利用给定的指令,基于父镜像创建新的镜像。
1.创建新文件夹及文件
mkdir py2.7Docker //创建文件夹
ubuntu@VM-0-13-ubuntu:~$ cd py2.7Docker
ubuntu@VM-0-13-ubuntu:~/py2.7Docker$ ls
ubuntu@VM-0-13-ubuntu:~/py2.7Docker$ touch Dockerfile //创建文件
ubuntu@VM-0-13-ubuntu:~/py2.7Docker$ ls
Dockerfile
ubuntu@VM-0-13-ubuntu:~/py2.7Docker$ vim Dockerfile
# 编辑Dockerfile
cat 查看编辑内容:
ubuntu@VM-0-13-ubuntu:~/py2.7Docker$ cat 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"]
同样的方法,创建 requirements.txt and app.py
requirements.txt
Flask
Redis
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. build 建立docker
$ ls
Dockerfile app.py requirements.txt
出现错误 ““docker build” requires exactly 1 argument…”
ubuntu@VM-0-13-ubuntu:~/py2.7Docker$ docker build --tag=friendlyhello
"docker build" requires exactly 1 argument.
See 'docker build --help'.
Usage: docker build [OPTIONS] PATH | URL | -
Build an image from a Dockerfile
使用pwd获取当前路径,之后再加入路径build
pwd
ubuntu@VM-0-13-ubuntu:~/py2.7Docker$ docker build --tag=friendlyhello /home/ubuntu/py2.7Docker
。。。
。。。
Successfully built f6afc80826ac
Successfully tagged friendlyhello:latest //成功
查看
ubuntu@VM-0-13-ubuntu:~/py2.7Docker$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
friendlyhello latest f6afc80826ac 43 seconds ago 131MB
ubuntu 18.04 94e814e2efa8 2 days ago 88.9MB
python 2.7-slim 8559620b5b0d 9 days ago 120MB
运行
docker run -p 4000:80 friendlyhello
停止
docker container stop 1fa4ab2cf395
3. 上传到DockerHub
登陆DockerHub账号:
ubuntu@VM-0-13-ubuntu:~/py2.7Docker$ docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: lizengquan
Password:***
修改标签,并push
ubuntu@VM-0-13-ubuntu:~/py2.7Docker$ docker tag friendlyhello lizengquan/py2.7:001
ubuntu@VM-0-13-ubuntu:~/py2.7Docker$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
friendlyhello latest f6afc80826ac 26 minutes ago 131MB
lizengquan/py2.7 001 f6afc80826ac 26 minutes ago 131MB
ubuntu@VM-0-13-ubuntu:~/py2.7Docker$ docker push lizengquan/py2.7:001
查看
此时网页上也会显示
ubuntu@VM-0-13-ubuntu:~/py2.7Docker$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
friendlyhello latest f6afc80826ac 31 minutes ago 131MB
lizengquan/py2.7 001 f6afc80826ac 31 minutes ago 131MB
ubuntu 18.04 94e814e2efa8 2 days ago 88.9MB
从远程仓库运行
docker run -p 4000:80 username/repository:tag
4. 运行docker
ubuntu@VM-0-13-ubuntu:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
friendlyhello latest f6afc80826ac About an hour ago 131MB
lizengquan/py2.7 001 f6afc80826ac About an hour ago 131MB
ubuntu 18.04 94e814e2efa8 2 days ago 88.9MB
python 2.7-slim 8559620b5b0d 9 days ago 120MB
运行python
ubuntu@VM-0-13-ubuntu:~$ docker run -it friendlyhello:latest python2.7 //远程运行类似
Python 2.7.16 (default, Mar 5 2019, 00:20:32)
[GCC 6.3.0 20170516] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print "afd"
afd