Official Docker document Translation 2

Source: Internet
Author: User
Tags curl redis socket tag name docker hub docker run in python

Reprint please indicate the source:
https://blog.csdn.net/forezp/article/details/80158062
This article is from Fang Zhibong's blog container prep installation docker, version 1.13 or 1.13 above. Read the content of the first article to give your Docker environment a quick test to make sure all the work is ready:

Docker run Hello-world Introduction

It's time to start building an application in Docker mode. We start at the bottom of this hierarchy, that is, it's a container, and that's what we're introducing on this interface. At this level (container. Above is a service that defines how the container behaves in a production environment, which we will explore in the next article. Finally, at the top level is the stack, which defines the interaction of all the services described in the 5th article. Stack (fifth article introduction) service (the third article Introduction) container (This article describes, you is here) your new development environment

In the past, if you wrote a Pyhton application, your first step was to install the Python development environment on your machine. But you need a running environment on your machine that fits perfectly with your application, and you also need to match your production environment.

With Docker, you can migrate a handy Python runtime as a mirror, without the need for installation. Then, by building a mirror that includes the Python runtime and your app's code, you can ensure that your code and running environment are perfectly combined and running.

These handy mirrors are defined by a file called Dockefile. defining a mirror through Dockerfile

Dockerfile defines what run environment is running inside the container. Resources such as access to network interfaces and disk drives are virtualized in this environment, isolated from the rest of the system, so you need to map the ports to the outside world and explicitly what files you want to "replicate" to the running environment. However, after you have done this, you can expect that the build of the application defined in this dockerfile will behave exactly the same at run time. Dockerfile

Create an empty folder. Enter the newly created folder via the CD command, create a file named Dockerfile, copy the following into the file, and save it.

# Use of the official Python runtime as a parent image from
Python:2.7-slim

# Set The working directory To/app
WO Rkdir/app

# Copy The current directory contents to the container At/app
ADD./app

# Install any needed PA Ckages specified in Requirements.txt
RUN pip install--trusted-host pypi.python.org-r requirements.txt

# Make PO RT available to the world outside this container
EXPOSE

# Define environment variable
ENV NAME world
  # Run app.py When the container launches
CMD ["Python", "app.py"]

This dockerfile is associated with 2 files that we have not yet created, namely app.py and Requirements.txt. Let's create it here. Application Section

Create 2 files, Requirements.txt and app.py, and put them into the same folder as Dockerfile. This completes our application and you can find it simple to create an application. When the above dockerfile is built into a mirror, app.py and Requirements.txt are added to the mirror via the Add command, and the expose command is able to expose the port, which can be accessed via HTTP. Requirements.txt

Flask
Redis
app.py
From flask import flask from
redis import Redis, rediserror
import OS
import sockets

# 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 =" 

Now we see PIP install-r requirements.txt Installing flask and redis libraries for Python, and the application prints the environment variable name and the output of calling Socket.gethostname (). Finally, because Redis is not running (because we have only installed the Python library, not the Redis itself), we should expect that trying to use it here will fail and produce an error message.

That's the point. You do not need Python or any requirements.txt files on your system, and you do not need to install or run this image on your system. It looks like you're not really building an environment with Python and flask, but you already have it. Building Apps

We are ready to build the application. Make sure you have the highest permissions on the newly created file. Here is the LS command, which should show the following:

$ ls
Dockerfile      app.py          requirements.txt

Now run the build command. Create a Docker image with the-t flag, so that the image has a friendly name.

Docker build-t Friendlyhello.

Where is the image you are building. Local Docker image registry on your machine "

$ docker Image ls

REPOSITORY            TAG                 image ID
friendlyhello         latest              326387cea398
Run the app

To run the application, use-p to map the machine's port 4000 to the container's published port 80:

Docker Run-p 4000:80 Friendlyhello

You should see a message in http://0.0.0.0:80 that Python is serving your application. But the message comes from inside the container, and it doesn't know that the container's port 80 is mapped to 4000,

http://localhost:4000 in the browser allows you to see what is displayed on the page.

You can also view the same content by using the Curl command:

$ Curl http://localhost:4000

This 4,000:80 port remapping is to demonstrate the difference between expose in Dockerfile and content published using Docker run-p. In a later step, we only need to map port 80 on the host to port 80 in the container and use http://localhost.

Press CTRL + C to end the process.

You can now let the application run in the back-end process, using detached mode.

Docker run-d-P 4000:80 Friendlyhello

You can get the container ID of the app and stop the application with the container ID. The container is running in the background. You can use the Docker container ls command to view the abbreviated container ID:

$ docker Container ls
container ID        IMAGE               COMMAND             CREATED
1fa4ab2cf395        friendlyhello       " Python app.py "     seconds ago

Now to end the process with the Docker containner Stop command, you need to use the container ID, as follows:

Docker container Stop 1fa4ab2cf395
Share your Image:

To demonstrate the portability of the container we just created, we uploaded the image we built and could run anywhere else. After all, when you want to deploy a container to a production environment, you need to know how to push a registered warehouse.

The registry repository is a collection of repositories, and the repository is a collection of images-a bit like the GitHub repository, but the code has been created. You can create many repositories by registering an account on the warehouse. The Docker CLI uses the public registry of Docker by default. Login Dokcer Id

If you do not have a Docker account yet, please register an account on the website cloud.docker.com. Make a note of your user name.

Log on to the Docker public registry on the local computer.

$ docker Login
Tag Mirror

The command to associate the local image with the repository in the registry is Username/repository:tag. The label is optional, but is recommended because it is the mechanism used by the registry to provide a version of the Docker image. Provide the repository for the context and mark a meaningful name, such as Get-started:part2. This puts the image into the startup repository and marks it as part2.

Now, put it together to mark the image. Run the dock tag image using your user name, repository, and tag name to upload the image to the destination you want. The syntax for this command is:

Docker Tag Image Username/repository:tag

Like what:

Docker tag Friendlyhello John/get-started:part2

Run the Docker image LS command to see the image of your new tag.

$ docker Image ls

REPOSITORY               TAG                 image ID            CREATED             SIZE
friendlyhello            latest              d9e555c53008        3 minutes ago       195MB
john/get-started         part2               d9e555c53008        3 minutes ago       195MB
python                   2.7-slim            1c7128a655f6        5 days ago          183MB
...
Push Image

Upload your tagged image to the warehouse

Docker Push Username/repository:tag

When you are finished, the results of this upload will be published publicly. If you are logged into the Docker Hub, you can see the new image there through its pull command. get and run a mirror from a remote repository

From now on, you can use Docker run and use this command to run your application on any machine:

Docker Run-p 4000:80 Username/repository:tag

If the image is not on the local machine, Docker pulls it out of the repository.

$ docker run-p 4000:80 John/get-started:part2
Unable to find image ' John/get-started:part2 ' locally
Part2:pulli ng from john/get-started
10a267c67f42:already exists
F68a39a6a5e4:already exists
9beaffc0cf19:already exists
3c1fe835fb6b:already exists
4c9f1fa8fcb8:already exists
Ee7d8f576a14:already exists
Fbccdcced46e:already exists
digest:sha256:0601c866aab2adcc6498200efd0f754037e909e5fd42069adeff72d1e2439068
status:downloaded Newer image for John/get-started:part2
 * Running on http://0.0.0.0:80/(press CTRL + C to quit )

Regardless of where Docker run executes, it extracts your image and all of the dependencies in Python and Requirements.txt, and runs your code. They're all in a neat little package, and you don't need to install any Docker on the host. Summary

This is the content of this page. In the next section, we'll learn how to extend our application by running this container in a service. Command Review

The basic Docker commands for this page are listed here, along with some related commands, if you want to explore before proceeding.

Ocker build-t Friendlyhello. # Create image using this directory ' s Dockerfile docker run-p 4000:80 Friendlyhello # Run "friendlyname" Mapping port 40                                XX to $ 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  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.