Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build a docker basic image

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to build a basic image of docker. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Container VS virtual machine

Containers and virtual machines have similar resource isolation and allocation advantages, but the functions are different, because containers virtualize the operating system, not hardware, so containers are easier to migrate and more efficient.

After a simple understanding of docker, you need to actually experience installing and building containers, which in this case uses centos6.5:

1. Install docker related softwar

[root@bogon ubuntu-16.04] # rpm- ivh http://dl.Fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpmRetrieving http://dl.Fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpmwarning: / var/tmp/rpm-tmp.KYucBm: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEYPreparing... # [100%] 1:epel-release # [root@bogon ubuntu-16.04] # yum-y install docker-ioLoaded plugins: fastestmirror, refresh-packagekit SecurityLoading mirror speeds from cached hostfile...Complete! [root@bogon ubuntu-16.04] # service docker startStarting cgconfig service: [OK] Starting docker: [OK] [root@bogon ubuntu-16.04] # chkconfig docker on [root@bogon ubuntu-16.04] #

When using service docker status to check the status of docker services, it is found that docker dead but pid file exists is not started, and Cannot connect to the Docker daemon appears when executing docker-related commands (such as docker ps). Is' docker-d' running on this host?, needs to solve this problem, as follows:

[root@bogon ubuntu-16.04] # service docker statusdocker dead but pid file exists [root@bogon ubuntu-16.04] # yum-config-manager-- enable public_ol6_latestLoaded plugins: fastestmirror, refresh-packagekit [root@bogon ubuntu-16.04] # yum install-y device-mapper-event-libsLoaded plugins: fastestmirror, refresh-packagekit, security...

two。 Build a basic image

After using docker, you can get it from the image library by commanding docker pull, but sometimes network problems or other reasons may cause it to fail to pull. It is introduced on docker China's official website to use Docker official image acceleration to solve the problem:

You can pull directly from the accelerated address of the image using the following command:

$docker pull registry.docker-cn.com/myname/myrepo:mytag

For example:

$docker pull registry.docker-cn.com/library/ubuntu:16.04

The original text is as follows: click here

In this article, I use Dockerfile to build the basic image ubuntu 16.04 (xenial). The Github address of the corresponding Dockerfile is: click here to search ubuntu in hub.docker.com to see the corresponding image information. The Dockerfile content is as follows:

FROM scratchADD ubuntu-xenial-core-cloudimg-amd64-root.tar.gz / # a few minor docker-specific tweaks# see https://github.com/docker/docker/blob/9a9fc01af8fb5d98b8eec0740716226fadb3735c/contrib/mkimage/debootstrapRUN set-xe\\ # https://github.com/docker/docker/blob/9a9fc01af8fb5d98b8eec0740716226fadb3735c/contrib/mkimage/debootstrap#L40-L48 & & echo'#! / bin/sh' > / usr/sbin/policy-rc.d\ & & echo 'exit 101'> / usr/sbin/policy-rc.d\ & & chmod + x / usr/sbin/policy-rc.d\\ # https://github.com/docker/docker/blob/9a9fc01af8fb5d98b8eec0740716226fadb3735c/contrib/mkimage/debootstrap#L54-L56 & & dpkg-divert-- local-- rename-- add / sbin/initctl\ & & cp-a / usr/sbin/policy-rc.d / sbin/initctl\ & & sed-I 's / ^ exit. * / exit exit'/ sbin/initctl\\ # https://github.com/docker/docker/blob/9a9fc01af8fb5d98b8eec0740716226fadb3735c/contrib/mkimage/debootstrap#L71-L78 & & echo 'force-unsafe-io' > / etc/dpkg/dpkg.cfg.d/docker-apt-speedup\\ # https://github.com/docker/docker/blob/9a9fc01af8fb5d98b8eec0740716226fadb3735c/contrib/mkimage/debootstrap#L85-L105 & & echo' DPkg :: Post-Invoke {"rm-f / var/cache/apt/archives/*.deb / var/cache/apt/archives/partial/*.deb / var/cache/apt/*.bin | | true" };'> / etc/apt/apt.conf.d/docker-clean\ & & echo 'APT::Update::Post-Invoke {"rm-f / var/cache/apt/archives/*.deb / var/cache/apt/archives/partial/*.deb / var/cache/apt/*.bin | | true";};' > / etc/apt/apt.conf.d/docker-clean\ & & echo 'Dir::Cache::pkgcache "; Dir::Cache::srcpkgcache" '> / etc/apt/apt.conf.d/docker-clean\ # https://github.com/docker/docker/blob/9a9fc01af8fb5d98b8eec0740716226fadb3735c/contrib/mkimage/debootstrap#L109-L115 & & echo' Acquire::Languages "none";'> / etc/apt/apt.conf.d/docker-no-languages\\ # https://github.com/docker/docker/blob/9a9fc01af8fb5d98b8eec0740716226fadb3735c/contrib/mkimage/debootstrap#L118-L130 & & echo 'Acquire::GzipIndexes "true" Acquire::CompressionTypes::Order:: "gz";'> / etc/apt/apt.conf.d/docker-gzip-indexes\\ # https://github.com/docker/docker/blob/9a9fc01af8fb5d98b8eec0740716226fadb3735c/contrib/mkimage/debootstrap#L134-L151 & & echo 'Apt::AutoRemove::SuggestsImportant "false" '> / etc/apt/apt.conf.d/docker-autoremove-suggests# delete all the apt list files since they're big and get stale quicklyRUN rm-rf / var/lib/apt/lists/*# this forces "apt-get update" in dependent images Which is also good# enable the universeRUN sed-I's / ^ #\ s *\ (deb.*universe\) $/\ 1docker' / etc/apt/sources.list# make systemd-detect-virt return "docker" # See: https://github.com/systemd/systemd/blob/aa0c34279ee40bce2f9681b496922dedbadfca19/src/basic/virt.c#L434RUN mkdir-p / run/systemd & & echo 'docker' > / run/systemd/container# overwrite this with' CMD []'in a dependent DockerfileCMD ["/ bin/bash"]

Now explain the relevant commands in Dockerfile:

FROM refers to dependent base images, such as scratch, which means blank, zero-based images. Dependent images can be local or remote library

ADD means to add a local file to the image. If you encounter a linux unzipped file, it will be decompressed automatically, which is why there is no explicit decompression of tar.gz in the whole file.

RUN runs commands, such as commands related to installing software

CMD sets the command that is executed by default when starting Container, which can be overridden when the container is started

At present, there are only a few commands involved in this Dockerfile, and the others will be explained later. After the explanation, start building:

[root@bogon ubuntu-16.04] # docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@bogon ubuntu-16.04] # docker imagesREPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE [root@bogon ubuntu-16.04] # pwd/home/ml/ubuntu-16.04 [root@bogon ubuntu-16.04] # ll-htotal 40MMurray 1 ml ml 2.8K Dec 19 12:37 Dockerfile-rw-rw-r--. 1 ml ml 40M Dec 19 12:39 ubuntu-xenial-core-cloudimg-amd64-root.tar.gz [root@bogon ubuntu-16.04] # [root@bogon ubuntu-16.04] # docker build-t ubuntu:16.04 .Sending build context to Docker daemon 41.94 MBSending build context to Docker daemon Step 0: FROM scratch-> Step 1: ADD ubuntu-xenial-core-cloudimg-amd64-root.tar.gz /-- > 537c2f6dd023Removing intermediate container dee7679a7ee2Step 2: RUN set-xe & & echo' #! / bin/sh' > / usr/sbin/policy-rc.d & &\ echo 'exit 101' > > / usr/sbin/policy-rc.d & & chmod + x / usr/sbin/policy-rc.d & &\ dpkg-divert-- local-- rename-- add / sbin/initctl & & cp-a / usr/sbin/policy-rc.d / sbin/initctl & & sed-I's / ^ exit. * / exit 0max'/ sbin/initctl & &\.. .-> Running in 41d719b68981 + echo #! / bin/sh+ echo exit 101 + chmod + x / usr/sbin/policy-rc.d+ dpkg-divert-- local-- rename-- add / sbin/initctlAdding 'local diversion of / sbin/initctl to / sbin/initctl.distrib'+ cp-a / usr/sbin/policy-rc.d / sbin/initctl+ sed-is / ^ exit. * / exit 0 / / sbin/initctl+ echo force-unsafe-io+ echo DPkg::Post-Invoke {"rm-f / var/cache" / apt/archives/*.deb / var/cache/apt/archives/partial/*.deb / var/cache/apt/*.bin | | true " }; + echo APT::Update::Post-Invoke {"rm-f / var/cache/apt/archives/*.deb / var/cache/apt/archives/partial/*.deb / var/cache/apt/*.bin | | true"; + echo Dir::Cache::pkgcache ""; Dir::Cache::srcpkgcache ""; + echo Acquire::Languages "none"; + echo Acquire::GzipIndexes "true"; Acquire::CompressionTypes::Order:: "gz"; + echo Apt::AutoRemove::SuggestsImportant "false" -> c49bdbf61888Removing intermediate container 41d719b68981Step 3: RUN rm-rf / var/lib/apt/lists/*-- > Running in 6389964016a2-- > 4508181f7442Removing intermediate container 6389964016a2Step 4: RUN sed-I's / ^ #\ s *\ (deb.*universe\) $/\ 1Running in cbed2b28c988-- > 8eed06df8f19Removing intermediate container cbed2b28c988Step 5: RUN mkdir-p / run/systemd & & echo 'docker' > / run/systemd/container-- > Running in aff40dbc6e05-> 19c96e7912a4Removing intermediate container aff40dbc6e05Step 6: CMD / bin/bash-> Running in 2469ee9d7251-> 77e565a65647Removing intermediate container 2469ee9d7251Successfully built 77e565a65647 [root@bogon ubuntu-16.04] # docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZEubuntu 16.04 77e565a65647 33 seconds ago 110.5 MB [root@bogon ubuntu-16.04] #

As you can see from the build log, each command is a step, and after execution, an id is generated, similar to 6389964016a2. In fact, this is the layering of the image, stacked layer by layer.

At this point, a ubuntu16.04 version of the docker image has been built, so the next step is to run

3. Run Mirror

Use the docker run command to run:

[root@bogon ubuntu-16.04] # docker run-it ubuntu:16.04root@5ea0b95e8641:/# cat / etc/issueUbuntu 16.04.3 LTS\ n\ lroot@5ea0b95e8641:/# ps-efUID PID PPID C STIME TTY TIME CMDroot 1 00 22:47? 00:00:00 / bin/bashroot 11 1 0 22:47? 00:00:00 ps-efroot@5ea0b95e8641:/#

Where 5ea0b95e8641 is the ID of the current container. Enter the container to view all processes. If pid is 1, shouldn't bash,linux be init? In fact, this is the difference between a container and a virtual machine. The init process of the container is the docker service process on the host, and each container is just a process. The parameter-it means that the front end opens and assigns a terminal, and-d runs in the background. Let's try whether-d can be used in the current one:

[root@bogon ~] # docker run-d ubuntu:16.0443ae7ded8e6920b55b8e744b52ffce37b89b25182fcacdc10a5414e6621abff3 [root@bogon ~] # docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@bogon ~] # docker run-d ubuntu:16.04 / bin/bash77f3ec2ebfb3f154772683eeea8ca7e2ba3b7756b1488f5f09818af424e0298e [root@bogon ~] # docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

It can be opened obviously. After using-d, docker ps cannot find any running containers. If you use-it, use docker ps to view it under other shell:

[root@bogon ml] # docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES8341a332c788 ubuntu:16.04 "/ bin/bash" 18 seconds ago Up 18 seconds drunk_cori

As you can see, there is a container running because we did not exit. From this, you can see that the container actually runs as a process, and after the execution of / bin/bash, the process dies, so the container does not exist. If there is a tomcat service in the container, it is a different case.

The basic image is basically built.

Thank you for reading! This is the end of this article on "how to build a basic image of docker". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report