Docker mirroring production, upload, pull and deploy (using Aliyun) __ Aliyun

Source: Internet
Author: User
Tags redis wildfly docker ps docker run aliyun

Due to the discovery of the push mirror in the learning process has been timed out, so directly to the Aliyun Docker warehouse to apply for a (central management center –> to create a mirrored warehouse –> mine is the East China 2 binding GitHub account can), done. Later push on the use of this warehouse, pull use accelerator, pay attention to switch according to the use of the scene to switch, Dockerhub discard ... Recorded the operation process:
1. Create a namespace Hhu (in the current school, only lowercase, each account can only create 5), the creation of rookie Docker Mirror warehouse Docker1 (bound GitHub a warehouse, the individual can be arbitrary, the warehouse image is like an app, can constantly update its version), Then all the test mirrors can be pushed here, and later for a specific other mirror can be applied to another mirror warehouse (for example, when Tomcat, separate request for a mirrored warehouse Tomcat, for Redis when the application of a redis warehouse, and so on). Complete

2. Mirror production, this step under separate carry out detailed records;

3. Mirror push: After the production is finished, you need to push the mirror to the mirrored test warehouse in Docker1. The basic information is as follows-
1. Public network address: Registry.cn-shanghai.aliyuncs.com/hhu/docker1
2. Intranet address (ECS optional): Registry-internal.cn-shanghai.aliyuncs.com/hhu/docker1
3. Code warehouse (that is, the warehouse on the bound GitHub): Https://github.com/Jacksonary/Docker
, my first Docker mirror name is: Jacksonary/myfirstapp, according to network situation select public network Push, the main process is as follows:

# 1. Switch from accelerator to warehouse address login
Docker login--username=jacksonary@163.com registry.cn-shanghai.aliyuncs.com

# 2. Create a label for it based on the mirror name or ID, default to latest
docker tag Jacksonary/myfirstapp registry.cn-shanghai.aliyuncs.com/hhu/docker1[: Mirror version number]

# 3. Push Mirror
Docker push registry.cn-shanghai.aliyuncs.com/hhu/docker1[: Mirror version number]

You can then view the pushed mirrors in the Aliyun warehouse as follows:

When you get the above mirrored file, you need to specify the mirror version number, so it is recommended that you append the required mirror version number to make a distinction when you push the mirror, and if I need to pull the above mirror, I can do the following:

# because the default version defaults to latest, you can get it by default, or append: Latest (recommended)
Docker pull Registry.cn-shanghai.aliyuncs.com/hhu/docker1
first, the production of Docker image

Usually a project to put a folder, such as the official web site has a project called Flask-app, then all the files are in the project directory, we need to add a text file called "Dockerfile" under the project root directory, and remove its txt suffix, Then use the ordinary text editor for the Docker environment, such as the following dockerfile:

# 1. Specifies that basic mirrors are Linux (alipine Docker mirrors are lightweight Linux systems with only 5 m) from
alpine:3.5

# Install Python and Pip under Alipine, this app is written in Python , you will need to install the Python environment, usually copying files and installing the necessary

copy requirements.txt/usr for the python you need to install the app by relying on RUN apk add--update py2-pip #
/src/app/
RUN pip install--no-cache-dir-r/usr/src/app/requirements.txt

# Copy application required files to mirror
copy app.py/usr /src/app/
COPY templates/index.html/usr/src/app/templates/

# Set the port number that needs to be exposed
expose 5000

# Set application to start Python application cmd via cmd
["Python", "/usr/src/app/app.py"]

Then create Docker mirrors, PowerShell into the project root directory (that is, Dockerfile directory), execute

Docker build-t Jacksonary/myfirstapp.

It is important to note that when we use "Dockerfile" as the Docker configuration file name, we write it directly, but if you use a different profile name, you must specify it, such as "Jdk-9-alpine." Dockerfile "As a docker configuration file, it should be written with the-F specified configuration file:

Docker build-t jacksonary/myfirstapp-f Jdk-9-alpine. Dockerfile.

Where-T represents a label for the currently created mirror as "Jacksonary/myfirstapp",/the first half must be your Docker username ID (if you are using a dockerhub warehouse, because Dockerhub default is your username, If you use Aliyun, I can take it,/the latter part is the name of the application, together as the image of the tag, the following URL path can not be lost, the point is the current path, the implementation will be automatically published to the current HV virtual machine, using Docker Images can view more than one "Jacksonary/myfirstapp" mirror, complete.
"Summary" about Dockerfile file configuration required:
1. The dockerfile file must begin with from, followed by the underlying container and version, indicating the parent container for the current mirror, which is usually in the form of a username/Mirror name: Version number (Dockerhub)

The 2.RUN directive is used to create the current Docker mirror, and each time the instruction is invoked, Docker creates a new mirror layer so that it is easy to roll back to the previous mirror version, and its syntax is to follow the shell instruction (such as run mkdir/user/local/) behind the run. Foo), it will automatically execute the/bin/sh shell, of course you can also specify such as: Run/bin/bash-c ' Mkdir/user/local/foo '

3.COPY instructions can copy local files to a container

The command defined by the 4.CMD directive will be executed at the time the mirror starts, unlike the run instruction, which does not create a new mirror layer, but simply executes the instruction, which can have only one cmd instruction in each mirrored Dockerfile file. You can also have multiple directives to be executed (this is best done by scripting the CMD), and the CDM execution instructions require us to specify where to run these instructions, while run does not need to be specified, such as the following CMD command

cmd ["Python", "./app.py"]
cmd ["/bin/bash", "echo", "Hello World"]

The 5.EXPOSE directive is used to specify which port the mirror program will serve, which can be retrieved via the Docker inspect <container-id> instruction, but the expose instruction does not actually expose the port to the host. Rather, it is exposed in the form of a-p flag when Docker run is released, the above is lowercase p you need to specify the mapping between the host to the virtual host port, and the uppercase P is to expose the port in the mirror to the random port of the host, which port can be viewed through Docker PS, for example:

As you can see in the image above, you are exposing the mirrored 8080 port to the 32768 port of the host, which can be viewed through localhost:32768.

The 6.PUSH directive can publish mirrors to platforms such as Docker Cloud

The 7.ENV directive is used to configure environment variables, such as:

# Configure Java environment variables, which are standard Java environment variables in Linux,
env java_home=/opt/jdk-9
env path= $PATH: $JAVA _home/bin
second, the deployment runs the mirror

After creating the mirror, you can run a run, here first provide my own according to the tutorial Mirror: Docker pull registry.cn-shanghai.aliyuncs.com/hhu/docker1, can pull down to run in Docker:

Docker pull Registry.cn-shanghai.aliyuncs.com/hhu/docker1

Docker run-p 8888:5000--name Myfirstapp Registry.cn-shanghai.aliyuncs.com/hhu/docker1

Where-P (which is important) means that the exposed 5000 ports on the virtual machine are mapped to native 8888 ports, and the mirror is named Myfirstapp, at which point you can view the cat's git map by accessing the http://localhost:8888. Each refresh randomly retrieves a different cat map. third, mirror push

At the beginning of the article, the entire production of Docker image of the file address: Https://github.com/Jacksonary/Docker/tree/master/flask-app Four, simple Java application Deployment

Is simple Java engineering, where Maven is packaged, come on, go to our working directory, execute

MVN Archetype:generate-dgroupid=edu.hhu.java-dartifactid=helloworld-dinteractivemode=false

To create a simple Mavenjava project, I know that most people can do this successfully, but there are a few people who execute this command and can't create a project (I'm one of them) that prompts

There is no POM in this directory

A look of a bewildered to give him an empty pom, it also suggests that there is no data in the POM, well, in another way, we'll tell him we're going to create the project:

MVN archetype:generate

Then it will prompt us to do not have the built-in skeleton, chooses 7:maven-archetype-quickstart then, then according to the prompt input groupid and artifactid and so on information, finally will ask whether you pack, Package directly (and then the jar package will appear in the target directory), well, that's it. Let's see if this works:

JAVA-CP Target/helloworld-1.0-snapshot.jar edu.hhu.java.App

Where-CP indicates the package path of all the classes needed to execute this class file-the path to the System class loader, and the default skeleton gives "Hello World" to greet you, well, Java project creation is complete.

The second step is to write the Docker configuration file Dockerfile:

From Openjdk:latest

COPY target/helloworld-1.0-snapshot.jar/usr/src/helloworld-1.0-snapshot.jar

CMD java- Cp/usr/src/helloworld-1.0-snapshot.jar edu.hhu.java.App

Part III create mirroring and execute

Docker build-t Jacksonary/helloworld.
Docker Run Jacksonary/helloworld
application of complex multi-container in Docker (Docker-compose)

In the actual development, often need a variety of business, no longer is Ubuntu print a word, such as the Web and database interaction, such applications are typically composed of multiple containers, do not need to use Shell to launch these containers, all containers will be a "service group" in the way defined in a configuration file , similar to Dockerfile, written in the project root directory, and can then be leveraged

Docker-compose up-d

The Docker-compose script can be used to start, stop, restart the application and all the services in the application, Docker-compose's complete command is as follows:

directives content

Build

Build or Rebuild Services

Help

Get Help on a command

Kill

Kill containers

Logs

View Output from containers

Port

Print the public port for a port binding

Ps

List containers

Pull

Pulls Service Images

Restart

Restart Services

Rm

Remove stopped containers

Run

Run a one-off command

Scale

Set number of containers for a service

Start

Start Services

Stop

Stop Services

Up

Create and start containers

These defined Docker component service portals are docker-compse configuration files, typically in the form of yml files, such as the following DOCKER-COMPSE.YML (note that the colon must be spaces after each property configuration, except for port mappings):

Version: ' 3.3 '
services:
  DB:
    container_name:db
    image:mysql:8
    Environment:
      mysql_ Database:employees
      mysql_user:mysql
      mysql_password:mysql
      mysql_root_password:supersecret
    ports :
      -3307:3306
  Web:
    image:arungupta/docker-javaee:dockerconeu17
    ports:
      -8081:8080
      -9991:9990
    depends_on:
      -DB

In the combo file above:
1. Two services defined: DB and Web
2. The Image property specifies a mirrored file for each service word
3. Mysql:8 mirroring will start the MySQL service
4. The Environment property defines the MYSQL service environment variable for initialization: Mysql_database A database that specifies a name when mirroring is started, Mysql_user and Mysql_password create a new user and set a password. This user will be granted the Super privilege of the database created by Mysql_database, Mysql_root_password is mandatory to set the MYSQL superuser password
5. Ports implementation of the port forwarding, the front is the host, the following is a virtual machine
6. The depends_on attribute indicates a dependency between two services, in which Wildfly (an application server) relies on MySQL, so MySQL will start before wildfly

After having the combined configuration file mentioned above, PW enters the directory where the file is located, can use Docker-compose up-d to start the two services in isolation mode, Docker PS can see the mapping between ports, or it can be found that two containers are started. Docker-compose logs can view the log of the service, at this time we can access all personnel information through Http://localhost:8081/resources/employees, stop this group of services:

Docker-compose down

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.