Upgrade Docker and build your app

Source: Internet
Author: User
Tags redis versions docker ps docker run

Now that Docker has been upgraded to many versions, my current version of Docker is still 1.12.5 and older, so now we need to upgrade our Docker version, first of all, how to view the Docker version of our system.

Run the command Docker--version to view it. (i), new version of Docker Introduction and installation

Currently, Docker is divided into two available versions, the Docker Enterprise Edition and the Docker Community Edition, so the name Incredibles, Docker EE, or Docker Enterprise Edition is designed for enterprise development and IT teams to deploy commercial applications, and Docker CE, That is, the Docker community was designed for developers and small teams that have just started using Docker. Here we use the Docker CE. Uninstall old version

The old version of Docker is called Docker or Docker engine, and if installed, uninstall them first.

 sudo apt-get remove Docker docker-engine

If the uninstallation succeeds, Apt-get will report that no installation package is installed.

The mirrors, containers, volumes, and networks that are located in the directory/var/lib/docker/are reserved. The Docker CE package is now known as the Docker-ce,docker EE package and is now known as Docker-ee. additional packages recommended for trusty 14.04

If there are no other reasons, it is recommended that you install linux-image-extra-* these packages, which allow Docker to use AUFS storage drivers.

$ sudo apt-get update

$ sudo apt-get install \
    linux-image-extra-$ (uname-r) \
    linux-image-extra-virtual
Installing Docker

There are two kinds of installation methods, one is through the warehouse installation, one is through the package installation, here we choose to use the package installation.

We need to download the. deb file to install Docker. One of the trouble here is that every time we want to update Docker, we need to download a new file, it doesn't matter, we choose to install the package.

The installation steps for Docker CE and Docker EE are not the same, we only focus on the Docker CE. Go to download Address

, select our Ubuntu version, browse pool/stable, select AMD64 or ARMHF, and download the. deb file that you want to install.

After the download is complete, use the following command to install the installation package that you downloaded.

Dpkg-i/path/to/package.deb

After the installation is complete, we verify that the Docker CE installation is complete by running the Hello-world image. Uninstalling Docker

We can use the following command to uninstall the Docker package.

sudo apt-get purge docker-ce

However, the image, container, volume or custom profile will not be deleted automatically, and we can remove it by following the command:

sudo rm-rf/var/lib/docker
(ii) build new apps your new development environment

In the past, if you wanted to start writing a Python app, the first step would be to install a Python runtime environment on your machine, but that would lead to the question of where you want your app to run as expected in your machine environment, and where the app running your app is.

With Docker, you just need to crawl a lightweight Python runtime as a mirror, without having to install it. Then your build needs to include a basic Python image and your app code to make sure that your app, his dependencies and runtime are all in one piece.

These lightweight images are defined as dockerfile. define a container using Dockerfile

Dockerfile will define what will run in your container environment. Access to resources such as network interfaces and disk drives are virtualized in the environment, and this is isolated from other things in your system, so you have to map the ports to the outside world, and you have to specify those files you want to copy into that environment. Then, after doing that, you might want to define the app's build in this dockerfile to run anywhere. Dockerfile

Create an empty directory and then put the file with the name Dockerfile in it. Note the comments for each line.

# Use an official Python runtime as a mirror

from Python:2.7-slim # to set the working directory to/app
Workdir/app # Copy the
current directory contents into the/app container ADD. /app

# Installs all packages specified in the Requirements.txt file
RUN pip install-r requirements.txt

# so that 80 ports can be accessed outside the container
EXPOSE 80

# define Environment variables
ENV NAME World

# Run app.py
CMD ["Python", "app.py"] when the container starts

This dockerfile references some of the files we did not create, app.py and Requirements.txt. Below we obtain these files. The app itself

Get these two files and put them in the same directory as dockerfile. This completes our app and looks simple. When the above Dockerfile are built into the mirror, app.py and Requirements.txt will also exist, because the Dockerfile add command, the app.py output will be accessed via HTTP because of the expose command. 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)

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 can see that pip install Requirements.txt installs the flask and Redis libraries, and the app prints out the environment variable name, which is the same as the output after the Socker.gethoustname () call. Finally, because Redis is not running (because we have only installed the Python library, not the Redis itself), the result is that we try to use it but fail and produce an error message. Build Apps

That's it, in your system, you don't need anything from Python or requirements.txt files, and you don't need to build or run the scene to install into your system. It looks like you're setting up a python or flask environment variable, not really. The following LS should be displayed:

$ ls
Dockerfile      app.py          requirements.txt

Now run the build command. This will create a Docker image, which we use the-t option to mark him so that he has a friendly name.

Docker build-t Friendlyhello.

Where is the image you are building? He is in your machine's local Docker image entry.

$ docker Images

REPOSITORY            TAG                 IMAGE ID
friendlyhello         latest              326387cea398
Run the app

To run the app, use the-P option to map the port 4000 of your machine to port 80 of the container's expose.

Docker Run-p 4000:80 Friendlyhello

You should see a reminder that Python is http://0.0.0.0:80 serving your app. But that information comes from inside the container, and he doesn't know that you mapped ports 80 through 4000, making the current URL http://localhost:4000. Here you will see the Hello World text, the container ID, and the REDIS error message.

Note: This port mapping ' 4,000:80 ' is to prove that your ' EXPOSE ' in ' Dockerfile ' and you use ' Docker run-p ' to release the different. In a later step, we only map port 80 to port 80, so you can use ' http://localhost '.

Use CTRL + C in your terminal to exit.

Now we run the app in the background, using the detached mode:

Docker run-d-P 4000:80 Friendlyhello

You get a long container ID and are then kicked back into your terminal. Your container is running in the background. You can use Docker PS to see a simple container ID.

$ docker PS
CONTAINER ID        IMAGE               COMMAND             CREATED
1fa4ab2cf395        friendlyhello       "python app.py "     seconds ago

You will see that the container ID and http://localhost:4000 are matched.

Now use Docker stop to end the process, using the container ID, as follows:

Docker Stop 1fa4ab2cf395
Share your image

We learn how to share our images in the back.

 Docker login Docker tag Friendlyhello username/repository:tag Docker push Username/repository : Tag Docker run-p 4000:80 Username/repository:tag 

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.