Dockerfile build image and test run (docker study notes 2)

DockerfileBuild image and test run

1Create Dockerfile

Create an empty directory ,Create a file named Dockerfile, Copy and paste the following into the file and save. Make a note of the comments that explain each statement in the new Dockerfile.

# Use official Python as the runtime parent image

FROM python:2.7-slim

# Set the working directory /app

WORKDIR /app

# Copy the contents of the current directory to the application container

ADD . /app

# Install all the requirements.txt The package

RUN pip install --trusted-host pypi.python.org -r requirements.txt

# exposes the port number of 80 to the outside of the container

EXPOSE 80

# Define the environment variable

ENV NAME World

# When the container starts, run app.py

CMD ["python", "app.py"]

Dockerfile means that we have not yet Several files were created, namely app.py and requirements.txt. Let's create the next one.

Create two more files, requirements.txt and app.py, and place them in the same folder as and Dockerfile. This completes our application and you can see it is very simple. When the above Dockerfile is built into the image, app.py and requirements.txt is because there is DockerfileADDapp.pyEXPOSE @@@command.

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)

Now we see pip install -r requirements.txt installing the Flask and Redis libraries for Python, the application print environment variable NAME, and the output of the call socket.gethostname(). Finally, because Redis is not running (because we only have the Python library installed, not Redis itself), we should expect the attempt to use it here to fail with an error message.

注意: When accessing the container ID inside the container, access the host name, which is similar to the process ID of the running executable.

You don't need anything in Python or requirements.txt systems, nor do you need to build or run this image to install them on your system. It seems that you have not really built a Python and Flask environment, but you are already using

and we are ready to build the application. Make sure you are still at the top of the new directory. This is what ls should display:

$ ls 

Dockerfile   app.py   requirements.txt

Run the build command now. This will create a Docker image that we will mark using -t.

docker build -t friendlyhello .#Create Mirror

Where is your architectural image? It is located in your machine's local Docker image registry:

$ docker image ls

REPOSITORY            TAG                 IMAGE ID

friendlyhello         latest              326387cea398

Run the application

Run the application, use the following method to map the computer's port 4000 to the container's published port 80-p

docker run -p 4000:80 friendlyhello

you should See the message http://0.0.0.0:80 that Python is providing services for your application. But the message comes from inside the container, it doesn't know that you map port 80 of the container to 4000, so that the correct URLhttp://localhost:4000

is generated in the web browser to view the display provided on the web page.

You can also use curl in the shell to view the same content.

$ curl http://localhost:4000

<h3>Hello World!</h3><b>Hostname:</b> 8fc990912a14<br/><b>Visits:</b> <i>cannot connect to Redis, counter disabled</i>

This port remaps the difference between the 4000:80 demos EXPOSEDockerfilepublishdocker run -phttp://localhost

CTRL+C

@ In the next steps, map port 4000 on the host to port 80 in the container and use click your terminal to exit.

docker run -d -p 4000:80 friendlyhello

Now let us run the docker container ls application in the (COMMAND background in separate mode: )

$ docker container ls

CONTAINER ID        IMAGE               COMMAND             CREATED

1fa4ab2cf395        friendlyhello       "python app.py"     28 seconds ago

You get the long container ID of the app and then kick back to the terminal. Your container is running in the background. You can also see the abbreviated container IDCONTAINER ID (and can be interchanged when running the command): http://localhost:4000

on behalf of the runtime start command docker container stopPlease note that CONTAINER ID is what match @ @@@这

docker container stop 1fa4ab2cf395