Dockerfile introduction and deployment Docker

00. Dockerfile

Dockerfile text file is used to construct a mirror

1. Example

# FROM:定制的镜像都是基于 FROM 的镜像
FROM ubuntu
# 镜像作者
MAINTAINER bigfaceCat [email protected]
# RUN:用于执行后面跟着的命令行命令。
RUN apt-get update
RUN apt-get install -y gcc
ENTRYPOINT ["gcc"]

test

:~/share/02dockfile$ vim Dockerfile
:~/share/02dockfile$ docker build -t my_gcc .
Sending build context to Docker daemon 2.048kB
Step 1/5 : FROM ubuntu
---> 775349758637
Step 2/5 : MAINTAINER bigfaceCat [email protected]
---> Running in 791b7b58b2f5
Removing intermediate container 791b7b58b2f5
---> 0fe7b8555fd2
Step 3/5 : RUN apt-get update
---> Running in 6c47769437ec

2. workdir

Specify the working directory. WORKDIR work with the specified directory will exist in each layer building mirrored.
Construction mirror docker build process, each new layer is RUN command. Only directory will be created by WORKDIR always existed.
format:

WORKDIR <工作目录路径>

dockerfile

from ubuntu:latest
maintainer bigfaceCat [email protected]
workdir /a
run touch a.txt
workdir b
run touch 2.txt

Creating a mirrored Construction

:~/share/03dockfile$ docker build -t mytest:v1.0 .
Sending build context to Docker daemon 2.048kB
Step 1/6 : from ubuntu:latest
---> 775349758637
Step 2/6 : maintainer bigfaceCat [email protected]
---> Running in de2c135a84d8
Removing intermediate container de2c135a84d8
---> d8dbd3fb7e4e
Step 3/6 : workdir /a
---> Running in ab6eab6abd72
Removing intermediate container ab6eab6abd72
---> 3775363f0fab
Step 4/6 : run touch a.txt
---> Running in ffdf87379b35
Removing intermediate container ffdf87379b35
---> cb43988121cb
Step 5/6 : workdir b
---> Running in b57fcb1a4bef
Removing intermediate container b57fcb1a4bef
---> 7a00fe70187d
Step 6/6 : run touch 2.txt
---> Running in 42bc3893d8b1
Removing intermediate container 42bc3893d8b1
---> 2b6705cef44d
Successfully built 2b6705cef44d
Successfully tagged mytest:v1.0

View mirror built

:~/share/03dockfile$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mytest v1.0 2b6705cef44d 28 seconds ago 64.2MB

:~/share/03dockfile$ docker run -it mytest:v1.0 /bin/bash
root@2d728bb1e516:/a/b#
root@2d728bb1e516:/a/b# pwd
/a/b
root@2d728bb1e516:/a/b#
root@2d728bb1e516:/# find /a
a
a/b
a/b/2.txt
a/a.txt
root@2d728bb1e516:/#

3. run

RUN : used to execute the command line commands followed behind. There are two formats:

# RUN命令的格式
shell格式
RUN <命令行命令>
# <命令行命令> 等同于,在终端操作的 shell 命令。
exec格式
RUN ["可执行文件", "参数1", "参数2"]
# 例如:
# RUN ["./test.php", "dev", "offline"] 等价于 RUN ./test.php dev offline

4. cpoy

Copy instruction, copy the file from the context directory or directories specified in the path to the container.
format:

COPY [--chown=<user>:<group>] <源路径1>... <目标路径>
COPY [--chown=<user>:<group>] ["<源路径1>",... "<目标路径>"]
#参数描述
[--chown=<user>:<group>]:可选参数,用户改变复制到容器内文件的拥有者和属组。
<源路径>:源文件或者源目录,这里可以是通配符表达式,其通配符规则要满足 Go 的
filepath.Match 规则。
<目标路径>:容器内的指定路径,该路径不用事先建好,路径不存在的话,会自动创建。
示例:
COPY hom* /mydir/
COPY hom?.txt /mydir/

5. add

ADD COPY command and consistent format to use (the same demand, the official recommended COPY). Function similarly, except as follows:

  • ADD advantages of: performing <source> is compressed tar file, compression format, copied and extract to <destination path> for the next, and where xz of gzip bzip2 automatically.
  • Shortcoming of ADD: without decompression, compressed tar files can not be copied. Mirrored cache invalidation will make the building, which may lead to image building becomes slow. Specifically whether to use, it can be determined according to whether the automatic decompression.

Dockerfile

from ubuntu:latest
maintainer bigfaceCat [email protected]
add test.tar.gz /data/

6. cmd

Similar RUN command for running programs, but both run different time points:

  • CMD run when docker run.
  • RUN is a docker build.

Role: designated as the default container starts to run the program, the program ends, the container will be over. CMD command designates docker run program may be specified in the command line parameters to run the program covered.
** Note: ** If Dockerfile If there is more than CMD command, only the last one will work.

CMD <shell 命令>
CMD ["<可执行文件或命令>","<param1>","<param2>",...]
CMD ["<param1>","<param2>",...] # 该写法是为 ENTRYPOINT 指令指定的程序提供默认参数

7. entrypoint

CMD command is similar, but it will not be docker run command-line parameter to specify the instructions covered, and these command-line parameters are specified as a parameter to the ENTRYPOINT instruction program.

However, using the --entrypoint option when you run docker run, this option can be used as parameters to run a program covering
ENTRYPOINT instructions specified program.

** Pros: ** You can specify parameters required ENTRYPOINT run in the implementation of docker run.

** Note: ** If Dockerfile If there are multiple ENTRYPOINT instructions, only the last one will work.

format:

ENTRYPOINT ["<executeable>","<param1>","<param2>",...]

With the CMD command can be used: generally only use is becoming a reference CMD, CMD herein ENTRYPOINT tantamount to pass parameters, the following examples will be mentioned.

FROM nginx
ENTRYPOINT ["nginx", "-c"] # 定参
CMD ["/etc/nginx/nginx.conf"] # 变参

It does not pass parameters to run

$ docker run nginx:test
#容器内会默认运行以下命令,启动主进程。
#nginx -c /etc/nginx/nginx.conf

Parameter passing operation

$ docker run nginx:test -c /etc/nginx/new.conf
#容器内会默认运行以下命令,启动主进程(/etc/nginx/new.conf:假设容器内已有此文件)
#nginx -c /etc/nginx/new.conf

8. expose

Statement port.
effect:

  • Port mirroring to help users understand the guardian of this image services to facilitate configuration mapping.
  • At runtime when using the random port mapping, that is, when docker run -P, will be automatically mapped EXPOSE random port.

format:

EXPOSE <端口1> [<端口2>...]

9. A program running in the container

Test case test.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main()
{
	int listen_sock = socket(AF_INET, SOCK_STREAM, 0);
	if (0 <= listen_sock)
	{
		struct sockaddr_in stServerAddr;
		stServerAddr.sin_family = AF_INET;
		stServerAddr.sin_addr.s_addr = htonl(INADDR_ANY);
		stServerAddr.sin_port = htons(55555);
		if ((0 == bind(listen_sock, (struct sockaddr *)&stServerAddr,
		sizeof(stServerAddr))) && (0 == listen(listen_sock, 10)))
		{
			struct sockaddr_in stClientAddr;
			socklen_t AddrLen = sizeof(stClientAddr);
			int data_sock = -1;
			while (0 <= (data_sock = accept(listen_sock, (struct sockaddr
				*)&stClientAddr, &AddrLen)))
			{
				char szBuff[256];
				int recv_len = 0;
				while (0 < (recv_len = recv(data_sock, szBuff,
					sizeof(szBuff), 0)))
				{
					send(data_sock, szBuff, recv_len, 0);
				}
				close(data_sock);
			}
		}
		else
		{
			perror("bind:");
		}
	}
	else
	{
	perror("create listen socket:");
	}
	return -1;
}

Compiler

:~/share/06echo$ gcc test.c -o myecho
:~/share/06echo$ ls
Dockerfile hello.txt myecho test.c test.tar.gz

The first way

Dockerfile

from ubuntu:latest
maintainer bigfaceCat [email protected]
copy myecho /
expose 55555
#启动容器的时候执行myecho 必须使用相对路径
cmd ./myecho

command:

:~/share/06echo$ docker build -t mytest:v1.4 .
Sending build context to Docker daemon 14.85kB
Step 1/5 : from ubuntu:latest
---> 775349758637
Step 2/5 : maintainer bigfaceCat [email protected]
---> Using cache
---> d8dbd3fb7e4e
Step 3/5 : copy myecho /
---> Using cache
---> 609ab81360e8
Step 4/5 : expose 55555
---> Using cache
---> ff02cb9c73b8
Step 5/5 : cmd myecho
---> Using cache
---> 87f44d74903b
Successfully built 87f44d74903b
Successfully tagged mytest:v1.4

:~/share/06echo$ docker run mytest:v1.4

:~/share$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e7cd0e815dc1 mytest:v1.4 "/bin/sh -c ./myecho" About a minute ago Up About a minute 55555/tcp crazy_chatelet

View and IP testing

:~/share$ docker inspect crazy_chatelet

:~/share$ nc 172.17.0.7 55555
hello world
hello world
alkfjsdlkjflasdkjflskdjf
alkfjsdlkjflasdkjflskdjf
#退出docker
:~/share$ docker kill crazy_chatelet
crazy_chatelet

The second way

Dockerfile

from ubuntu:latest
maintainer bigfaceCat [email protected]
copy myecho /
expose 55555
#启动容器的时候执行myecho
entrypoint ./myecho

command

:~/share/07entrypoint$ docker build -t mytest:v1.5 .
Sending build context to Docker daemon 14.85kB
Step 1/5 : from ubuntu:latest
---> 775349758637
Step 2/5 : maintainer bigfaceCat [email protected]
---> Using cache
---> d8dbd3fb7e4e
Step 3/5 : copy myecho /
---> Using cache
---> 609ab81360e8
Step 4/5 : expose 55555
---> Using cache
---> ff02cb9c73b8
Step 5/5 : entrypoint ./myecho
---> Running in fe47f45aca03
Removing intermediate container fe47f45aca03
---> d30cd898d9a7
Successfully built d30cd898d9a7
Successfully tagged mytest:v1.5

test

:~/share/07entrypoint$ docker run -P mytest:v1.5
pensive_khorana

:~/share$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6617ee48c3de mytest:v1.5 "/bin/sh -c ./myecho" 54 seconds ago Up 52 seconds 0.0.0.0:32772->55555/tcp determined_euclid

:~/share$ nc localhost 32772
lkajfdslkjf
lkajfdslkjf
lkafjsdlkfjlksdajfalkdsj
lkafjsdlkfjlksdajfalkdsj
lakfjsdlkfj
lakfjsdlkfj

Reference Documents

Docker official English resources:
Docker's official website: http://www.docker.com
Getting Windows Docker: https://docs.docker.com/windows/
Docker Linux Getting Started: https://docs.docker.com/linux/
Docker mac Getting Started: https://docs.docker.com/mac/
Docker user guide: https://docs.docker.com/engine/userguide/
Docker official blog: http://blog.docker.com/
Docker Hub: https://hub.docker.com/
Docker open Source: https://www.docker.com/open-source
Docker Chinese resources:
Docker Chinese website: http://www.docker.org.cn
Docker Getting Started tutorial: HTTP : //www.docker.org.cn/book/docker.html
Docker installation manual: http://www.docker.org.cn/book/install.html
one hour Docker tutorial:https://blog.csphere.cn/archives/22
Docker纸质书:http://www.docker.org.cn/dockershuji.html
DockerPPT:http://www.docker.org.cn/dockerppt.html

Published 11 original articles · won praise 0 · Views 404

Guess you like

Origin blog.csdn.net/bigfaceCatzzZ/article/details/104655251