Creation and uploading of Docker images

Source: Internet
Author: User
Tags docker ps fully qualified domain name docker hub docker run

Docker image creation and uploading Docker image introduction

Docker images are the standard format for application publishing in addition to the core technology of Docker. A full Docker image can support the operation of a Docker container, and in the entire use of Docker, after entering an already trained container, it is possible to operate in the container, the most common operation is to install the application in the container, if you want to migrate the installed services, You need to create new images of your environment and the services you build.

There are three ways to create a mirror, based on an existing image, based on a local template, and based on Dockerfile.

Create based on an existing image

Create a primary Use Docker commit command based on an existing image. Essentially, the program that runs a container and the running environment of the program are packaged to create a new image.

The following initiates an existing image, after the container has been modified, submits the modified container as a new image.

[[email protected] ~]# docker imagesREPOSITORY          TAG                 IMAGE ID            CREATED             SIZEdocker.io/centos    latest              5182e96772bf        6 days ago          200 MB

Start a container

[[email protected] ~]# docker run -itd 5182e96772bf /bin/bashWARNING: IPv4 forwarding is disabled. Networking will not work.4faf98f1c6619212c3205b0a7830617ff69d457c0882ed0ca4e71430fc549406

This indicates that the IPv4 routing function is not turned on, so we need to turn on the IPv4 routing function.

[[email protected] ~]# sysctl -pnet.ipv4.ip_forward = 1


Stop and delete the container that you have created, and restart a container.

[[email protected] ~]# docker ps -aCONTAINER ID        IMAGE               COMMAND             CREATED             STATUS         4faf98f1c661        5182e96772bf        "/bin/bash"         32 seconds ago      Up 30 seconds  [[email protected] ~]# docker stop 4faf98f1c6614faf98f1c661[[email protected] ~]# docker rm 4faf98f1c6614faf98f1c661

Re-create and start a container

[[email protected] ~]# docker run -itd 5182e96772bf /bin/bashcbeb511d2b9a7a5a25ad532a1bb8183eb4008248fad3d545eaeb7f4f57f4660a

Enter into the container to see if the HTTPD service is installed.

[[email protected] ~]# docker exec -it cbeb511d2b9a /bin/bash[[email protected] /]# rpm -q httpdpackage httpd is not installed

You can see that there is no installation, and we use Yum to install the HTTPD service and repackage the programs and environments running inside the container to generate a new image.

[[email protected] /]# yum install httpd -y[[email protected] /]# rpm -q httpdhttpd-2.4.6-80.el7.centos.1.x86_64#显示已经安装好httpd

We use the Docker commit command to create a new image with several common options:

  • -M: Description information
  • -A: Author information
  • -P: Stop the container from running during the build
    [[email protected] ~]# docker commit -m "nwe" -a "test" cbeb511d2b9a apache:testsha256:96aaf92c7b04b6d91fcaea0d15ad7715d575dac82971458320d54d449eedc174[[email protected] ~]# docker imagesREPOSITORY          TAG                 IMAGE ID            CREATED             SIZEapache              test                96aaf92c7b04        12 seconds ago      319 MBdocker.io/centos    latest              5182e96772bf        6 days ago          200 MB

    You can see that there are now two images locally, one of which we re-created after the modification. We tested the Apache service that created and started the container with this image to access the container.

      [[email protected] ~]# Docker ps-acontainer ID IMAGE COMMAND CREATED STATUS PORTS names5ea867891f2b 96aaf92c7b04 "/bin/bash" 8 Secon         DS ago up 4 seconds 0.0.0.0:80->80/tcp upbeat_spencecbeb511d2b9a 5182e96772bf "/bin/bash" Minutes ago up minutes Inspiring_davinci  
[[email protected] ~]# docker exec -it 5ea867891f2b /bin/bash[[email protected] /]# rpm -q httpdhttpd-2.4.6-80.el7.centos.1.x86_64[[email protected] /]# /usr/sbin/apachectl -D FOREGROUNDPassing arguments to httpd using apachectl is no longer supported.You can only start/stop/restart httpd using this script.If you want to pass extra arguments to httpd, edit the/etc/sysconfig/httpd config file.AH00558: httpd: Could not reliably determine the server‘s fully qualified domain name, using 172.17.0.3. Set the ‘ServerName‘ directive globally to suppress this message

Create based on local templates

By importing the operating system files you can generate the image, the template can be downloaded from the OpenVZ Open source project, and the following is an example of importing a Debian template compressed package from a download to a local mirror using the Docker Import command.

  [[email protected] ~]# wget http://download.openvz.org/template/precreated/ DEBIAN-7.0-X86-MINIMAL.TAR.GZ--2018-08-13 19:59:32--http://download.openvz.org/template/precreated/ Debian-7.0-x86-minimal.tar.gz parsing host download.openvz.org (download.openvz.org) ... 185.231.241.69 connecting download.openvz.org (download.openvz.org) |185.231.241.69|:80 ... is connected. An HTTP request has been made and is waiting for a response ... $ OK Length: 88436521 (84M) [application/x-gzip][[email protected] ~]# cat debian-7.0-x86-minimal.tar.gz | Docker import-daoke:testsha256:2850c25d855bb1c545ab41de6f194754fbfcd5664edb78773aa7da7acf86ea7c[[email                Protected] ~]# Docker imagesrepository TAG IMAGE ID CREATED Sizedaoke Test 2850c25d855b seconds ago 215 Mbapache Test 96AAF9 2c7b04 5 hours ago 319 Mbdocker.io/centos Latest 5182E96772BF 6 days ago 20 0 MB  

When the import operation is complete, the ID information of the mirror is returned, and the newly created image information can be seen in the local mirror list.

Create based on Dockerfile

In addition to manually generating Docker images, you can also use Dockerfile to automatically generate mirrors. Dockerfile is a file consisting of a set of instructions, each of which corresponds to a command in Linux, and the Docker program will read the instructions in the dockerfile to generate the specified image.

    • The ADD command has two parameters, source and target. Its basic function is to copy files from the file system of the source system to the file system of the target container. If the source is a URL, the contents of the URL will be downloaded and copied into the container.
    • CMD is similar to the Run command, and CMD can be used to execute specific commands. Unlike run, these commands are not executed during the mirroring build, but are called after the container is built with the image.
    • The expose is used to specify ports so that applications within the container can interact with the outside world via ports.
    • Maintainer This command is placed in the beginning of the dockerfile, although theoretically it can be placed anywhere in the dockerfile. This command is used to declare the author and should be placed behind the from.
    • The Run command is the core part of the Dockerfile execution command. It takes the command as a parameter and is used to create the mirror. Unlike the cmd command, the Run command is used to create a mirror (forming a new layer above the previous commit layer).
    • The FROM command is probably the most important dockerfile command. The change command defines which base image to use to start the build process. The underlying image can be any mirror. If the underlying image is not found, Docker will attempt to find the image from Docker image index. The from command must be the first command of Dockerfile.
    • The env command is used to set environment variables. These variables exist in the form of "Key=value" and can be called by scripts or programs within the container. This mechanism provides great convenience for running applications in containers.
  [[email protected] ~]# vim Dockerfile from centos# based on the underlying image of CentOS, if not locally, go to the warehouse to download maintainer test# To maintain the user information of the image, fill out the run Yum install httpd-y# mirroring operation to install the Apache package expose 80# Open 80 port Add index.html/var/www/html/index.html# Copy page Home file add run.sh/run.sh# Add a startup script to the image run chmod 777/run.sh# Modify script Execute permissions cmd ["/run.sh"] #启动容器时执行脚本 [[email protected] ~]# vim run.sh#!/bin/bashrm-rf/run/httpd/*exec/usr/sbin/apachectl-d foreground[[email protected] ~]# vim Index.html This is test[[email protected] ~]# Docker build-t Httpd:centos. #在Dockerfile文件目录下, run the command, pay attention to the back point, and be sure to have [[ Email protected] ~]# Docker imagesrepository TAG IMAGE ID CREATED SIZ                EHTTPD centos ef950765e1bb 2 minutes ago 319 Mbdaoke Test          2850c25d855b minutes ago 215 Mbdocker.io/centos latest 5182E96772BF 6 days ago mb# you can see that the first one is the image we just created  

To load a newly created image into a container to run

[[email protected] ~]# docker run -d -p 1200:80 httpd:centos519bf067d1ad0e06aeb030530d3bcb517be1424ed41c0635d6aca28784c80143[[email protected] ~]# docker ps -aCONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                  NAMES519bf067d1ad        httpd:centos        "/run.sh"           24 seconds ago      Up 23 seconds       0.0.0.0:1200->80/tcp   pedantic_elion

You can see that the mirror is already running in the container, accessing the virtual machine IP and port as http://192.168.58.159:1200, and successfully accessing the home page.

Upload the image to the public repository

As you create more images, you need a place to save the image, which is the repository. There are currently two types of warehouses: public warehouses and private warehouses. The most convenient is to use the public warehouse and download images, download the public warehouse image does not need to register, but upload the image to the public warehouse need to register, after filling out username and Passwaord can upload their own image.

[[email protected] ~]# docker login Login with your Docker ID to push and pull images from Docker Hub. If you don‘t have a Docker ID, head over to https://hub.docker.com to create one.Username: lightblueyxPassword: Login Succeeded[[email protected] ~]# docker tag httpd:centos lightblueyx/httpd:centos[[email protected] ~]# docker push lightblueyx/httpd:centos The push refers to a repository [docker.io/lightblueyx/httpd]cdf567d92322: Pushed 619278fd492d: Pushed 7e054e58afc8: Pushed abf29fcf5046: Pushed 1d31b5806ba4: Pushed centos: digest: sha256:6ce997f142e7757853296a314ea4de4309785867988ddc06dae57d5bacc428e6 size: 1362

Uploading a mirror to a private warehouse

You can use registry to build a local private warehouse, where you first need to build a private warehouse to download the registry image.

[[email protected] ~]# docker pull registryUsing default tag: latestTrying to pull repository docker.io/library/registry ... latest: Pulling from docker.io/library/registry4064ffdc82fe: Pull complete c12c92d1c5a2: Pull complete 4fbc9b6835cc: Pull complete 765973b0f65f: Pull complete 3968771a7c3a: Pull complete Digest: sha256:51bb55f23ef7e25ac9b8313b139a8dd45baa832943c8ad8f7da2ddad6355b3c8Status: Downloaded newer image for docker.io/registry:latest

After that, you need to create a JSON file under the/etc/docker directory, or you will get an error when uploading the image to a custom private repository, and then restart the Docker service.

Then use the downloaded registry image to start a container, by default the warehouse is stored in the container of the/tmp/registry directory, using the-v option to mount the local directory to the/tmp/registry in the container to use, This will not be feared when the container is deleted and the image disappears. Starting a private warehouse service locally, the listener port is consistent with the JSON file port.

[[email protected] ~]# docker run -d -p 5000:5000 -v /data/registry:/tmp/registry registryb592e059d495c1498f364d4c4c676b18ee3b8e8468fbc054fc598835b7cb2a5a

Use the Docker tag command to mark the image you want to upload as 192.168.58.159:5000/daoke.

[[email protected] ~]# docker tag daoke:test 192.168.58.159:5000/daoke[[email protected] ~]# docker imagesREPOSITORY                  TAG                 IMAGE ID            CREATED             SIZEhttpd                       centos              ef950765e1bb        46 minutes ago      319 MBlightblueyx/httpd           centos              ef950765e1bb        46 minutes ago      319 MB192.168.58.159:5000/daoke   latest              2850c25d855b        About an hour ago   215 MBdaoke                       test                2850c25d855b        About an hour ago   215 MBdocker.io/centos            latest              5182e96772bf        6 days ago          200 MBdocker.io/registry          latest              b2b03e9146e1        5 weeks ago         33.3 MB

Use Docker push to upload the image of the tag.

[[email protected] ~]# docker push 192.168.58.159:5000/daokeThe push refers to a repository [192.168.58.159:5000/daoke]3a1d67a7fe13: Pushed latest: digest: sha256:7eaeca4042df243bcec277baf4867b04ad6e0b1f112b8324e05655f216dc919f size:

You can view the uploaded image locally.

[[email protected] ~]# cd /data/[[email protected] data]# lsregistry

Creation and uploading of Docker images

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.