1.定义一个Dockerfile

$ 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"]
$ cat requirements.txt 
Flask
Redis
$ cat 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.构建app

$ ls
app.py  Dockerfile  requirements.txt

$ docker build --tag=friendlyhello .
$ docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
friendlyhello       latest              d74d9cebbfbe        47 seconds ago      131MB
python              2.7-slim            48e3247f2a19        3 weeks ago         120MB
hello-world         latest              fce289e99eb9        3 months ago        1.84kB

$ docker run -p 4000:80 friendlyhello

1595000247542.jpg

2.png

3.总结和备忘

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

参考:

https://docs.docker.com/get-s...


pa0v0
1 声望0 粉丝