Dockerfile format, Dockerfile example (install nginx)

Dockerfile format

A dockerfile is a script composed of a series of commands and parameters. These commands are applied to the base image and finally create a new image. They simplify the process from start to finish and greatly simplify deployment. Dockerfile starts with the FROM command, followed by various methods, commands and parameters. The output is a new image that can be used to create a container.

Similar to Makfile, users can use docker build to compile the image. Use this command to set the number of CPUs, memory size, file path, etc. used when compiling the image.

Dockerfile is composed of multiple instructions. Each instruction executes the corresponding program to complete certain functions when compiling the image. It consists of instructions + parameters, separated by commas, and # is the comment start character. Although the instructions are not case sensitive, they are general instructions. Use larger and lower case for parameters.

FROM: Specify which base image
format is based on : FROM or FROM:
such as FROM centos; FROM centos: latest

MAINTAINER: Specify author information

Format: MAINTAINER
such as MAINTAINER lzx [email protected]

RUN: Mirror operation instruction
format: RUN or RUN ["executable",'param1","param2"]
such as RUN yum install httpd or RUN ["/bin/bash","-c","echo hello"]

CMD: Specify the command to be executed when the container is started. There can only be one
format: CMD ["executable","param1","param2] or
CMD command param1 param2 or
CMD ["param1","param2"]
such as CMD ["/ bin/bash","/usr/local/nginx/sbin/nginx","-c","/usr/local/nginx/conf/nginx.conf"]

EXPOSE: Specify the port to be mapped.
Format: EXPOSE…
(-P (uppercase) specifies the container port, the host port is randomly assigned; -p (lowercase) specifies the host port and the container port)
such as EXPOSE 22 80 443 or EXPOSE -P 80 or EXPOSE -p 8088:80

ENV: Provide an environment variable
format for subsequent RUN instructions : ENV
such as ENV PATH /usr/local/mysql/bin:$PATH

ADD: Copy a local file or directory to a directory of the container.
Format: ADD
(src is the relative path of the directory where the dockfile is located, or it can be a url)
such as ADD <conf/vhosts> </usr/local/nginx /conf>

COPY: Copy a local file or directory to a directory of the container
Format: COPY
(basically the same usage as ADD, but does not support url)
such as COPY <conf/vhosts> </usr/local/nginx/conf>

ENTRYPOINT: Specifies the command to be executed when the container is started. There can be only one command, and only the last one takes effect when writing multiple entries.
Format: ENTRYPOINT ["executable","param1","param2] or
ENTRYPOINT command param1 param2 or
ENTRYPOINT ["param1", "Param2"]
(Usage is basically the same as CMD, but CMD can be overwritten by docker run command, while ENTRYPOINT cannot be overwritten, and will be executed earlier than the command specified by CMD or docker run)
such as ENTRYPOINT ["/bin/bash"," /usr/local/nginx/sbin/nginx","-c","/usr/local/nginx/conf/nginx.conf"]

VOLUME: Create a mount point
format that can be mounted from this machine or other containers : VOLUME ["/data"]

USER: Specify the user running the container.
Format: USER daemon

WORKDIR: Specify the working directory
format for subsequent RUN, CMD or ENTERPOINT : WORKDIR /path/to/workdir

Dockerfile example

The grammatical format of the dockerfile was introduced above, and now we are installing the nginx instance operation.

Edit the dockerfile:

vim Dockerfile

## Set the base image to CentOS

FROM centos

# File Auther / Maintainer

Maintainer jin [email protected]

# Install necessary tools

RUN yum install -y pcre-devel wget net-tools gcc zlib zlib-devel make openssl-devel

# Install Nginx

ADD http://nginx.org/download/nginx-1.19.5.tar.gz .

RUN tar zxvf nginx-1.19.5.tar.gz

RUN mkdir -p /usr/local/nginx

RUN cd nginx-1.19.5 && ./configure --prefix=/usr/local/nginx && make && make install

RUN rm -fv /usr/local/nginx/conf/nginx.conf

ADD http://www.apelearn.com/study_v2/.nginx_conf /usr/local/nginx/conf/nginx.conf

# Expose ports

EXPOSE 80

# Set the default command to execute when creating a new container

ENTRYPOINT /usr/local/nginx/sbin/nginx && tail -f /etc/passwd

Add tail -f to prevent the container from automatically stopping after nginx is started

Create a mirror:

docker build -t centos_nginx .

(.): Means to find the dockerfile in the current path, if it is in another path, you can write the path;

(-t) Specify the new image name

………………………………………. Omit ten thousand words

Successfully built 91a1362358a1

Successfully tagged centos_nginx:latest

If this appears, it means the operation is successful

[root@jinkai02 src]# docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

centos_nginx latest 91a1362358a1 2 minutes ago 384MB

Run the mirror:

[root@jinkai02 src]# docker run -itd -p 81:80 centos_nginx bash

92a9cd33fdd7350af4a3287edde8bd468108ad01f83c5e88014307bdebcae963

[root@jinkai02 src]# docker exec -it 92a9cd bash

[root @ 92a9cd33fdd7 /] # ps aux | grep nginx

root 8 0.0 0.0 18532 624 ? Ss 15:52 0:00 nginx: master process /usr/localnginx/sbin/nginx

nobody 9 0.0 0.3 41796 3708 ? S 15:52 0:00 nginx: worker process

nobody 10 0.0 0.3 41796 3708 ? S 15:52 0:00 nginx: worker process

root 26 0.0 0.0 9172 720 pts/1 S+ 15:53 0:00 grep --color=auto nginx

[root@92a9cd33fdd7 /]# ifconfig

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500

​ inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255

​ ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)

​ RX packets 8 bytes 656 (656.0 B)

​ RX errors 0 dropped 0 overruns 0 frame 0

​ TX packets 0 bytes 0 (0.0 B)

​ TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536

​ inet 127.0.0.1 netmask 255.0.0.0

​ loop txqueuelen 1000 (Local Loopback)

​ RX packets 0 bytes 0 (0.0 B)

​ RX errors 0 dropped 0 overruns 0 frame 0

​ TX packets 0 bytes 0 (0.0 B)

​ TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

[root@92a9cd33fdd7 /]# exit

Exit

[root@92a9cd33fdd7 /]# exit

Exit

Access port 81 of the host

[root@jinkai02 src]# curl 127.0.0.1:81

<!DOCTYPE html>

<html>

<head>

<title>Welcome to nginx!</title>

<style>

body {

​ width: 35em;

​ margin: 0 auto;

​ font-family: Tahoma, Verdana, Arial, sans-serif;

}

</style>

</head>

<body>

<h1>Welcome to nginx!</h1>

<p>If you see this page, the nginx web server is successfully installed and

working. Further configuration is required.</p>

<p>For online documentation and support please refer to

<a href="http://nginx.org/">nginx.org</a>.<br/>;

Commercial support is available at

<a href="http://nginx.com/">nginx.com</a>.</p>;

<p><em>Thank you for using nginx.</em></p>

</body>

</html>

Successful access, indicating that the above image was successfully created using Dockerfile

Guess you like

Origin blog.51cto.com/11451960/2640819