“This is the 11th day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021.”

Generally, the construction of an application image requires a series of operations such as basic image selection, directory creation, software installation, service deployment, volume mounting, port exposure, and environment variable definition. To solve this problem, a Dockerfile appears, which is a concrete description of an image, or a concrete build step template for the image.

The main components of a Dockerfile

FROMAlpine :latest -- RUN apt-get install golang-go -- CMD ["/bin/sh"] -- Specifies the container startup commandCopy the code

Common directives for Dockerfile

FROM -- Specifies the base mirror
MAINTAINER -- Specifies MAINTAINER information. This parameter is optional
RUN -- Executes shell commands in the container
ADD --COPY file, will automatically decompress
COPY - COPY files
WORKDIR -- Sets the current working directory
VOLUME -- Sets the VOLUME and mounts the host directory
EXPOSE -- Specifies the port to EXPOSE
CMD - specifies the command to execute after the container is started
ENV -- Defines environment variables
ENTRYPOINT -- Command executed after the container is started
Copy the code

The difference between CMD and ENTRYPOINT in Dockerfile

If there is only CMD, run the CMD command. If a command is added to start the container, it replaces the CMD command.

When only ENTRYPOINT is used, you can run the ENTRYPOINT command. If a parameter is added (in the command position) when the container is started, the parameter is used as the ENTRYPOINT parameter. If both ENTRYPOINT and CMD exist, the content of CMD will be used as an ENTRYPOINT parameter. If X is added when the container is started, X will replace the content of CMD as an ENTRYPOINT parameter.

For example

Install SSH software for Alpine image and implement SSH remote connection.

The file in the current path location: /home/phyger/dockerfile

Dockerfile content

FROM alpine:latest
MAINTAINER phyger
# copy the time zone file
COPY Shanghai /etc/localtime
Install openSSH, generate key, change password of root user
RUN Echo "http://mirrors.ustc.edu.cn/alpine/v3.5/main" > / etc/apk/repositories && \ echo "Http://mirrors.ustc.edu.cn/alpine/v3.5/community" > > / etc/apk/repositories && \ apk - update the add - no - cache && \ apk add --no-cache openssh && \ sed -i "s/#PermitRootLogin.*/PermitRootLogin yes/g" /etc/ssh/sshd_config && \ ssh-keygen -t rsa -P "" -f /etc/ssh/ssh_host_rsa_key && \ ssh-keygen -t ecdsa -P "" -f /etc/ssh/ssh_host_ecdsa_key && \ ssh-keygen -t dsa -P "" -f /etc/ssh/ssh_host_dsa_key && \ ssh-keygen -t ed25519 -P "" -f /etc/ssh/ssh_host_ed25519_key && \ echo "root:admin" | chpasswd
Expose port 22
EXPOSE 22
# command parameters
CMD ["-D"]
Specifies the command to start the container
ENTRYPOINT ["/usr/sbin/sshd"]
Copy the code

Build the mirror

Image using

Start container:

Check out the IP:

SSH connection test:

Check SSH processes in the container:

Here, success!