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

Introduction to Dockerfile and actual combat cases

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

Share

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

1. Concept

Dockerfile is a text file, which contains lines of instructions and parameters, each line of instructions build a layer, so the content of each line of instructions is to describe how the layer should be built; they greatly simplify the deployment of the process from beginning to end; Dockerfile starts with the FROM command, followed by various methods, commands and parameters, and finally produces a new image that can be used to create containers.

2. Detailed explanation of the order

Instruction

Description

Example

FROM

The base image must be specified for the first command

FROM centos:latest

MAINTAINER

This command is used to declare the author and should be placed on the next line of FROM

MAINTAINER wzs

RUN

The shell command executed when building the image is the core part of the Dockerfile execution command.

RUN yun install httpd

RUN ["yum", "install", "httpd"]

CMD

Similar to the RUN command, the shell command is executed; unlike RUN, RUN is executed during the construction of the image, and CMD is called after the container is built with the image; if we specify the command or have entrypoint in docker run, then CMD will be overwritten; multiple CMD commands in Dockerfile only take effect last

Usage 1: without brackets

CMD echo "hello cmd!"

Usage 2: with parentheses, there is no shell by default

CMD ["/ bin/bash", "- c", "echo 'hello cmdbits'"]

Usage 3: pass parameters to ENTRYPOINT

CMD ["- l"]

ENTRYPOINT

Similar to CMD commands, both are shell commands executed when running the container; unlike CMD, CMD can specify command overrides when executing docker run, while ENTRYPOINT can only be overridden by using-- entrypoint; CMD and ENTRYPOINT can be used together, ENTRYPOINT is used as a fixed command, and CMD is used as a variable parameter carried by commands in ENTRYPOINT

Usage 1: do not use [] any run and CMD cannot be passed in, and CMD will be overwritten.

ENTRYPOINT / bin/bash-c 'start.sh'

Usage 2: use [] to pass in the-l parameter in CMD, namely ls-l

CMD ["- l"]

ENTRYPOINT ["ls"]

EXPOSE

Expose the port of the container; you can also specify whether the port is listening on TCP or UDP. The default is TCP.

EXPOSE 80443

EXPOSE 80/udp

ENV

Set the environment variable in the container. You can modify the value of the environment variable when you start the container-- env =

ENV MYSQL_ROOT_PASSWD 123456

ADD

Copy a file or directory to the image. If it is a URL or compressed package, it will be downloaded or decompressed automatically.

ADD https://xxx.com/html.tar.gz / var/www/html

ADD html.tar.gz / var/www/html

COPY

Copy a file or directory to a mirror

COPY. / test.txt / tmp/test.txt

USER

Specify the running user for RUN, CMD, ENTRYPOINT, COPY, ADD execution commands

USER wzs

WORKDIR

Set up working directories for RUN, CMD, ENTRYPOINT, COPY, ADD

WORKDIR / data

ARG

Specify some parameters when building the image

ARG user

USER $user

3. Explain the usage of CMD and ENTRYPOINT

Explanation: because the usage of CMD and ENTRYPOINT is complicated, here are some examples to interpret the difference and usage of CMD and ENTRYPOINT.

Case 1: CMD usage 1

Docker build-t cmd:v1-f / var/Dockerfile/cmd-1.txt / var/Dockerfile/

After building the cmd:v1 image using Dockerfile, you can see that the CMD echo "123" command in Dockfile is used by executing docke run cmd:v1.

Executing docke run cmd:v1 echo "abc" overrides the CMD command in Dockfile

Case 2: CMD usage 2

Docker build-t cmd:v2-f / var/Dockerfile/cmd-2.txt / var/Dockerfile/

Use parentheses in []; at this point, the command is no longer in any shell terminal environment, and if we want to execute shell, we must add shell to the parenthesis argument

Executing docker run cmd:v2 ls / tmp/ overrides the CMD command in Dockfile

Case 3: CMD usage 3 Entrey Point usage 1

The third use of CMD is to pass parameters without carrying any commands, so use it with ENTRYPOINT

The echo in ENTRYPOINT in the Dockefile file is used as a command, and the "hello cmd" of CMD is used as a parameter. After execution, it is as follows:

You can also override the contents of CMD with the parameter "hello entrypoint" when docker run

Case 4: CMD usage 3 EntRYPoint usage 2

When the ENTRYPOINT in Dockerfile does not use square brackets, the contents of CMD are overwritten; docker run only executes ENTRYPOINT

No parameter or command can be passed into entrypoint when docker run is used.

4. Actual combat cases

Case 1. Tomcat deployment

First, create a directory to create the tomcat image, and store the installation packages and Dockerfile files needed for the image.

Dockerfile file content

# use basic image

FROM centos:latest

# indicate the author

MAINTAINER wzs

# create a directory

RUN mkdir-p / usr/local

# add installation package to the image

ADD apache-tomcat-7.0.93.tar.gz / usr/local/

ADD jdk-8u221-linux-x64.tar.gz / usr/local/

# define environment variables

ENV JAVA_HOME / usr/local/jdk1.8.0_221

ENV JRE_HOME $JAVA_HOME/jre

ENV CATALINA_HOME / usr/local/apache-tomcat-7.0.93

ENV PATH $CATALINA_HOME/bin:$JAVA_HOME/bin:$JRE_HOME/bin:$PATH

# expose Port

EXPOSE 8080

# run the tomcat process at startup

CMD ["/ usr/local/apache-tomcat-7.0.93/bin/catalina.sh", "run"]

Use Dockerfile to build an image

Docker build-t tomcat:v1-f. / Dockfile.txt. /

Create and run a container using an image

Docker run-itd-- name tomcat-docker-p 8081 name tomcat-docker 8080 tomcat:v1

Case 2. Nginx deployment

Create a directory to create a nginx image and store the installation packages and Dockerfile files needed for the image

Dockerfile file content

# use basic image

FROM centos:latest

# declaration author

MAINTAINER wzs

# install dependent libraries

RUN yum-y install gcc gcc-c++ make pcre-devel zlib-devel openssh-devel zlib

# add installation package to the image

ADD nginx-1.8.0.tar.gz / usr/local

# compilation and deployment

RUN cd / usr/local/nginx-1.8.0 & &\

. / configure-prefix=/usr/local/nginx & &\

Make & &\

Make install

# Delete installation package files

RUN rm-rf / usr/local/nginx-1.8.0

# expose Port

EXPOSE 80

# define a working directory

WORKDIR / usr/local/nginx

# run nginx at startup

CMD [". / sbin/nginx", "- g", "daemon off;"]

Build an image using a Dockerfile file

Docker build-t nginx:v1-f. / Dockerfile.txt. /

Create a running container using an image

Docker run-itd-- name nginx-docker-p 81:80 nginx:v1

Case 3. Mysql deployment

Create a directory where the tomcat image is made, because mysql is installed using yum; so there are only Dockerfile files

Dockerfile file content

# use basic image

FROM centos:latest

# author

MAINTAINER wzs

# add mysql users

RUN useradd-d / opt/mysql mysql

# download the yum installation package file of mysql

ADD http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm / opt/

RUN rpm-ivh / opt/mysql-community-release-el7-5.noarch.rpm

# install mysql-server

RUN yum install-y mysql-server

# mysql initialization

RUN mysql_install_db-user=mysql-basedir=/usr/-ldata=/var/lib/mysql/

# expose Port

EXPOSE 3306

# the combination of CMD and ENTRYPOINT is used here. The fixed command started for the mysql service in ENTRYPOINT, and CMD specifies the user running mysql, which can be overridden and modified during the docker run.

CMD ["--user=mysql"]

ENTRYPOINT ["/ usr/sbin/mysqld"]

Build a mysql image using Dockerfile

Docker build-t mysql:v1-f. / Dockerfile. /

Create a running container using an image

Docker run-itd-- name mysql-docker mysql:v1

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