How to package your app as a Docker image

Source: Internet
Author: User
Tags docker ps

A container is an instance of mirroring, a prelude mirror, and then a container, so the container can have more than one.

Although DockerHub provides a large number of mirrors, not every application can find a corresponding image to use in DockerHub due to the diversity of the enterprise environment. That requires the enterprise operators to master the skills of making Docker images. Before you start packing apps, you need to understand these two things first: the first thing to do is to choose the way that suits you to create the image:

1. The need to build the image is achieved by Dockerfile the generated image from the compilation .

2. through the container operation, and commit to implement the package to create the image .

The first approach is often used for less user interaction, such as when software is deployed without the need to enter any command applications. The second way of thinking is to use more user interaction, the installation process more configuration of the application. The second thing you need to be aware of is how to split the existing application to implement packaging. Docker containerized Applications should be the least functional. the split here does not mean splitting each software into a single container, but emphasizing the minimization of the implementation function. Use the most common LAMP architecture to discuss. The minimized structure should be Apache and PHP within a container, MySQL within a container. Instead of splitting Apache, PHP, and MySQL into a single container. The organic combination of Apache and PHP enables the application of the structure to fully implement PHP parsing and Web page functionality, while MySQL is solely responsible for the data storage function. Splitting a single container can result in a very complex architecture and is susceptible to container expansion. In general, you can split an app into 2-3 containers and 2 containers in the case of a data store and an application's own container. For some applications, there may also be a cache system, which is also placed in a single container. Under such a structure, when you need to expand, it is easier, depending on your needs, you can expand the cache system or application at any time.

Understand the above two things, and then go to the application packaging link.

Dockerfile Packaging

First, let's look at the Dockerfile of the official Docker-packaged Tomcat image.

As we can see, a lot of LINUX commands are executed to install and deploy the software, and the commands that need confirmation are added to-f to enforce. Ensure that there is no interaction, because in the process of compiling the image, can not be interactive operation, if you encounter interaction, the card will cause the failure of the image compilation, so DockerFile does not support the need to enter the command of the installation script, etc.

Of course, we also have some best practices for Dockerfile:

    • Images built with Dockerfile should be as streamlined as possible.
    • Try not to install non-essential packages.
    • A container only runs a separate instance, and the application with the coupling degree is installed separately into different containers.
    • Careful introduction of new data layers
    • The packages that are ready for installation are arranged alphabetically, which avoids repeated installation of the package and helps with software updates. Increase the readability of your code by adding the "\" Split command
    • Try to select an official version of the image as the base image to reduce the volume of the image.
    • Connect multiple RUN commands with "/" to make them easier to understand and easier to maintain.
    • Define a more generic port for the image, such as mirroring the HTTP Web service, preferably exposing port 80.
    • Dockerfile the first few lines of instructions should be fixed, do not change every time, so that the cache can be used
    • Building mirroring with –t tags helps users manage each image that is created.
    • Do not map public ports in Dockerfile.
    • When using cmd and entrypoint, be sure to use the array syntax, and the combination of CMD and entrypoint is better
    • Do not start initialization
    • Before Push, run the local build now to make sure that the locally built mirrors can work anywhere.
    • Do not upgrade the version in the build, and if an update attempts to modify the Init or change the contents of the container, the update may fail and may result in inconsistent mirroring.
    • The FROM command should contain the full repository name and label of the underlying image
    • Using a combination of instructions, such as Apt-get update, should be combined with the Apt-get install.
Base image Commit Build image

In addition to packaging a build image through Dockerfile, you can also generate mirroring through Docker Commit. The number of images that are packaged by Commit is due to the fact that the application itself has a large amount of interactive content at deployment time and cannot be specified by commands. When we package the image by Commit, we need to do the following:

1. Start a base image container and enter the console docker run -i – t centos:6.7 The above command creates a CentOS 6.7-based container and enters into the container

2. After the command of the configuration environment is logged in the container, you can execute commands under various Linux.

After configuring the environment, open a new console execution command docker ps , you can see the running container, such as my container ID is 35f1c2ae1f7e

3. Package the container as a mirror execution command docker commit 35f1c2ae1f7e mynewimage and package the container 35f1c2ae1f7e as a new mirror mynewimage .

Can perform docker images view mirroring

How to package your app as a Docker image

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.