Dockerfile instance

1. Organize ubuntu's software package management commands and service management commands

Ubuntu package management

The name of the Debian software package is deb, which is similar to the rpm package; there are two ways to manage the deb package: dpkg and apt. For software management, there are two types of dpkg apt:

  • dpkg : package manager for Debin, which can realize installation and deletion, but cannot resolve dependencies;
  • apt: advanced Packaging Tool, a powerful software management tool, similar to dnf/yum;

dpkg command – manage software installation packages

Syntax format: dpkg [parameter] package

Common parameters:

-i install package
-r remove package
-l Show list of installed packages
-L Shows the files associated with the package
-c Display the list of files in the package

apt-get command – Manage service software

Syntax format: apt-get [parameter] software name

Common parameters:

update Retrieve package list
upgrade update software
install install software
remove uninstall software
autoremove Automatically uninstall unused software
source download source code
build-dep compile dependencies
dist-upgrade update system
purge delete configuration file
clean Clean up junk files
check Check for damage

ubuntu startup, shutdown, restart service service command

start service

#service 服务名 start
service nginx start

close service

#service 服务名 stop
service nginx stop

restart service

#service 服务名 restart
service nginx restart

View service status

#service 服务名 status
service nginx status
2. Organize alpine package management commands and service management commands

Alpine Linux Package Management

apline installation source management

Linux defaults to foreign servers, and our access is relatively slow, so there is a mirror server in China

$ sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
$ sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories
# 还原官方使用
sed -i 's/mirrors.aliyun.com/dl-cdn.alpinelinux.org/g' /etc/apk/repositories

After changing the /etc/apk/repositories file, please run apk updateupdate index to take effect

apk upgrade --no-cache

Alpine common package management commands

Alpine uses apk for package management. Use the apk –help command to view the complete package management commands. Common commands are listed below:

1. update: update the local mirror source index from the remote mirror source

The update command will download APKINDEX.tar.gz from each mirror source list and store it in the local cache, usually in /var/cache/apk/ (Alpine is in this directory), /var/lib/apk/, /etc/apk/ cache/down.

$ apk update

2. add: install PACKAGES and automatically resolve dependencies

The add command installs packages from the warehouse and automatically manages dependencies.

$ apk add openssh openntp vim
$ apk add --no-cache mysql-client
$ apk add docker --update-cache --repository http://mirrors.ustc.edu.cn/alpine/v3.4/main/ --allow-untrusted

Install the specified version of the package

$ apk add asterisk=1.6.0.21-r0
$ apk add 'asterisk
$ apk add 'asterisk>1.6.1'

3. del: uninstall and delete PACKAGES

$ apk del openssh openntp vim

4. upgrade: upgrade the currently installed software packages. The upgrade command upgrades all software packages (generally including the kernel) installed in the system. Of course, you can also specify to upgrade only part of the software packages (specified by -u or –upgrade).

$ apk update #更新最新本地镜像源
$ apk upgrade #升级软件
$ apk add --upgrade busybox #指定升级部分软件包

5. search: search for software packages

The search command searches for available software packages, the -v parameter outputs the description content, and wildcards are used, and the -d or --description parameter specifies the query through the software package description.

$ apk search #查找所以可用软件包
$ apk search -v #查找所以可用软件包及其描述内容
$ apk search -v 'acf*' #通过软件包名称查找软件包
$ apk search -v -d 'docker' #通过描述文件查找特定的软件包

6. The info command is used to display information about software packages.

$ apk info #列出所有已安装的软件包
$ apk info -a zlib #显示完整的软件包信息
$ apk info --who-owns /sbin/lbu #显示指定文件属于的包

Alpine Linux service management

Alpine does not use systemctl for service management, but uses rca series of commands

The simplified version of alpine does not have rc series commands, which can be apk add --no-cache openrcinstalled

  • rc-update is mainly used to add or delete services at different run levels
  • rc-status is mainly used for run-level status management
  • rc-service is mainly used to manage the state of the service
  • openrc is mainly used to manage different run levels
3. Use dockerfile and alpine as the base image to deploy nginx.

1. Install nginx from apk
2. Compile and install nginx from source code

# apk 安装nginx
[root@zhao ~]#mkdir nginx
[root@zhao ~]#cd nginx
[root@zhao ~/nginx]#vim Dockerfile
FROM alpine
RUN echo "https://mirrors.aliyun.com/alpine/v3.11/main/" > /etc/apk/repositories && \
echo "https://mirrors.aliyun.com/alpine/v3.11/community/" >> /etc/apk/repositories && \
apk add nginx && mkdir /run/nginx/
EXPOSE 80 
ENTRYPOINT ["nginx","-g","daemon off;"]

[root@zhao ~/nginx]#docker build -t nginx-a:v1 .
[root@zhao ~/nginx]#docker images 
REPOSITORY   TAG       IMAGE ID       CREATED              SIZE
nginx-a      v1        680497aefd6d   About a minute ago   8.54MB
alpine       latest    9c6f07244728   3 weeks ago          5.54MB
centos       latest    5d0da3dc9764   11 months ago        231MB
[root@zhao ~/nginx]#docker run -d -P nginx-a:v1 
7ef50af037649cecaff747c018a6e63e0b0941eee8df94546044c8b7510b46d1
[root@zhao ~/nginx]#docker ps
CONTAINER ID   IMAGE        COMMAND                  CREATED         STATUS         PORTS                                     NAMES
7ef50af03764   nginx-a:v1   "nginx -g 'daemon of…"   4 seconds ago   Up 3 seconds   0.0.0.0:49153->80/tcp, :::49153->80/tcp   practical_brown
[root@zhao ~/nginx]#docker exec -it 7ef50af03764 /bin/sh
/ # vi /etc/nginx/conf.d/default.conf 
# This is a default site configuration which will simply return 404, preventing
# chance access to any other virtualhost.

server {
    
    
	listen 80 default_server;
	listen [::]:80 default_server;

	# Everything is a 404
	location / {
    
    
		index index.html;
	}

	# You may need this to prevent return 404 recursion.
	location = /404.html {
    
    
		internal;
	}
}
/ # nginx -s reload

insert image description here

# 源代码编译安装nginx
[root@zhao ~/nginx-alpine]#tree
.
├── Dockerfile
└── software
    └── nginx-1.19.7.tar.gz

1 directory, 2 files
[root@zhao ~/nginx-alpine]#vim Dockerfile 
FROM alpine:latest
ENV version 1.19.7
ENV PATH /usr/local/nginx/sbin:$PATH
ADD software/nginx-${version}.tar.gz /usr/src
EXPOSE 80 

RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && \
    adduser -S -H -s /sbin/nologin nginx && \
    apk add --no-cache -U pcre-dev openssl openssl-dev gd-dev g++ zlib-dev make && \
    mkdir -p /var/log/nginx && \
    chown -R nginx /var/log/nginx && \
    cd /usr/src/nginx-${version} && \
    ./configure \
    --prefix=/usr/local/nginx \
    --user=nginx \
    --group=nginx \
    --with-debug \
    --with-http_ssl_module \
    --with-http_realip_module \
    --with-http_image_filter_module \
    --with-http_gunzip_module \
    --with-http_gzip_static_module \
    --with-http_stub_status_module \
    --http-log-path=/var/log/nginx/access.log \
    --error-log-path=/var/log/nginx/error.log && \
    make && make install && \
    sed -i '/nobody/s/#//g' /usr/local/nginx/conf/nginx.conf && \
    rm -rf /var/cache/* /usr/src/*

WORKDIR /usr/local/nginx

CMD ["-g","daemon off;"]

ENTRYPOINT ["/usr/local/nginx/sbin/nginx"]

[root@zhao ~/nginx-alpine]#docker build -t nginx-s:v1 .
Successfully built 942391d0e251
Successfully tagged nginx-s:v1
[root@zhao ~/nginx-alpine]#docker run -d --name nginxv1 -p 80:80  nginx-s:v1 
7db71468b11cdf3bcd6c116453f40284ba5b3817a21fcbfd8f821b335a743745
[root@zhao ~/nginx-alpine]#docker ps
CONTAINER ID   IMAGE        COMMAND                  CREATED          STATUS          PORTS                                        NAMES
7db71468b11c   nginx-s:v1   "/usr/local/nginx/sb…"   4 seconds ago    Up 3 seconds    0.0.0.0:80->80/tcp, :::80->80/tcp, 443/tcp   nginxv1

insert image description here

Guess you like

Origin blog.csdn.net/weixin_53388991/article/details/126630160