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 understand docker basic image and dockerfile

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

How to understand docker base mirror and dockerfile, I believe many inexperienced people are helpless about this, this article summarizes the causes and solutions of the problem, through this article I hope you can solve this problem.

Make a base image

Note: You need to operate under CentOS6.

preparations

yum -y install febootstrap

Download ISO image file to server

mkdir /mnt/centos6/

mount -o loopCentOS-6.8-x86_64-bin-DVD1.iso /mnt/centos6/

Install the base package

pick bag

febootstrap -i bash -i yum -i iputils -i iproute -i openssh-server -i gcc -i libgcc centos6 centos68file:///mnt/centos6/

(-i Installation package, centos6 operating system version, centos68 installation directory, finally the source address, must be http://, ftp://, file://, https://,"")

febootstrap -i bash -i wget -i yum -i openssh-server -i iputils -i iproute -i gzip -i openssh-clients -i vim -i passwd -i vi -i curl -i sudo -i tar -i zip -i unzip -i dos2unix -i unix2dos -i net-tools -i initscripts -i telnet -i openssl -i gcc -i libtinfo -i libc -i libssh3 -i libsepol -i cmake -i ncurses-devel -i make -i libxml2-devel -i libxslt-devel -i pcre-devel -i libxml2 centos7 centos7 file:///mnt/centos7/

Install the basic package:

febootstrap centos6 centos68 file:///mnt/centos6/

configured

cd centos68/

vim etc/resolv.conf

; generated by /sbin/dhclient-scriptnameserver 114.114.114.114nameserver 8.8.8.8

cp -rf /root/.bash_profile /root/.bashrc root/

vim root/.bashrc

# .bashrc# User specific aliases and functionsalias rm='rm -i'alias cp='cp -i'alias mv='mv -i'alias ll='ls -l'# Source global definitionsif [ -f /etc/bashrc ]; then ./ etc/bashrcfi

chroot ./

yum upgrade -y

If you do not need to use the sshd service, you do not need to configure the following

CentOS6 generates sshd key: service sshd restart

For CentOS7, copy/etc/ssh/ssh_host_* from another CentOS7 machine. Start by executing/sbin/sshd -D.

Configure sshd

etc./ssh/sshd_conf modification

UseDNS no

cat etc/pam.d/sshd

#%PAM-1.0auth required pam_sepermit.soauth include password-authaccount required pam_nologin.soaccount include password-authpassword include password-auth# pam_selinux.so close should be the firstsession rule#session required pam_selinux.so close#session required pam_loginuid.so# pam_selinux.so open should only befollowed by sessions to be executed in the user context#session required pam_selinux.so openenv_paramssession optional pam_keyinit.so forcerevokesession include password-auth

yum clean all

exit

Change time zone to China cp /usr/share/zoneinfo/Asia/Shanghai etc/localtime

echo > root/.viminfo

echo > root/.bash_history

compressed and packaged in tar.gz format

tar --numeric-owner -zcvf /opt/centos6.tar.gz *

Note that the only supported file packaging types are (.tar,.tar.gz, .tgz, .bzip, .tar.xz, or .txz).

Import to docker

Note: Executed on docker machine

dockerimport centos6.tar.gz centos6

Writing Dockerfile

Note: Executed on docker machine

cd dockerfile/nginx

Note: The env and cgroup below are limited to dockerfile.

from centos6:20170620maintainer lykopsenv PATH/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/bin:/sbin/env PWD /root/add nginx.repo /etc/yum.repos.d/run yum install nginx -yrun echo /usr/sbin/nginx >>/etc/rc.localrun echo 'sleep 50000000000000000' >>/etc/rc.localcmd sh /etc/rc.local

Command should not exit immediately, otherwise the container will close

Creating an image

docker build -t centos6:nginx .

docker create centos6:nginx

Dockerfile basics concept

Dockerfile to solve automated deployment problems

Dockfile is a script that is interpreted by Docker programs and consists of instructions, one for each Linux command. Docker translates these Dockerfile instructions into real Linux commands. Dockerfile has its own writing format and supported commands, and Docker programs resolve dependencies between these commands, similar to Makefile. Docker program will read Dockerfile and generate customized image according to instructions.

Dockerfile instructions ignore case, it is recommended to use capitalization, use #as a comment, each line only supports one instruction, each instruction can carry multiple parameters.

Dockerfile instructions can be divided into two types according to their functions, construction instructions and setting instructions. The build directive builds an image, specifying an operation that will not be executed on the container running image; the set directive sets attributes of an image, specifying an operation that will be executed on the container running image.

basic commands

1、FROM

Construct an instruction that specifies a base image, and subsequent instructions depend on the image specified by the instruction.

FROM 【:】

2、MAINTAINER

a construction instruction for writing information related to the creator of the image into the image.

MAINTAINER

3、RUN

RUN can run any command supported by the base image.

RUN

RUN ["executable", "param1", "param2" ... ]

4、CMD

Set commands for operations specified when the container starts. This can be a custom script or a system command. This instruction can exist only once in the file, and if there are more than one, only the last one is executed.

CMD ["executable","param1","param2"]

CMD command param1 param2

When Dockerfile specifies ENTRYPOINT, the following format is used:

CMD ["param1","param2"]

ENTRYPOINT specifies the path to an executable script or program that will be executed with param1 and param2 as arguments. So if the CMD command uses the above form, then there must be a matching ENTRYPOINT in the Dockerfile.

5、ENTRYPOINT

Set command, which specifies the command to execute when the container starts, can be set multiple times, but only the last one is valid.

ENTRYPOINT["executable", "param1", "param2"]

ENTRYPOINTcommand param1 param2

The command can be used in two ways, either alone or in conjunction with CMD.

When used alone, CMD and ENTRYPOINT override each other, and only the last CMD or ENTRYPOINT is valid.

CMD instruction is used to specify default parameters of ENTRYPOINT, in which case CMD instruction is not a complete executable command, but only the parameter part;ENTRYPOINT instruction can only specify the execution command in JSON mode, but cannot specify parameters.

FROM ubuntu

CMD ["-l"]

ENTRYPOINT ["/usr/bin/ls"]

6、USER

Set the command to set the user who starts the container. The default is root.

USER daemon

7、EXPOSE

Sets the directive that maps a port in the container to a port in the host machine. When you need to access a container, you can use the IP address and mapped port of the host machine instead of the container's IP address. To complete the whole operation requires two steps, first in Dockerfile using EXPOSE to set the container port to map, and then specify the-p option when running the container plus the port set by EXPOSE, so that the port number set by EXPOSE maps to a port number on the host machine. Port mapping is an important feature of docker because the IP address of the container is randomly generated each time the container is run.

8、ENV

Build instructions to set an environment variable in image.

ENV

After setting, subsequent RUN commands can be used. After the container is started, you can view this environment variable through docker inspect, or you can set or modify the environment variable by setting docker run --env key=value.

If you install JAVA programs and need to set JAVA_HOME, you can write this in Dockerfile:

ENV JAVA_HOME /path/to/java/dirent

9、ADD

Build command, all files and folders copied to the container permissions are 0755, uid and gid are 0; if it is a directory, all files in the directory will be added to the container, excluding directories; if the file is a recognized compressed format, docker will automatically extract; if it is a file and does not end with a slash, it will be regarded as a file, and the contents will be written; if it is a file and ends with a slash, it will copy the file to the directory.

10、VOLUME

Set command.

VOLUME [""]

11. WORKDIR

Set command, can be switched many times (equivalent to cd command), RUN, CMD, ENTRYPOINT effective.

WORKDIR /path/to/workdir

After reading the above, do you know how to understand docker base mirror and dockerfile? If you still want to learn more skills or want to know more related content, welcome to pay attention to the industry information channel, thank you for reading!

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: 251

*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