1 overview

1.1 architecture

Docker provides a platform for developing, packaging, and running apps, separating them from the underlying infrastructure.

Docker Engine is a background process that provides the Server and CLI interfaces of the Rest API.

1.2 Underlying Technical Support

Namespaces: Separate user Spaces, network Spaces, processes (PID, NET, IPC, MNT, UTS)

Control groups: sets resource limits

Union File Systems: Layering containers and images

2 Image

2.1 Concept of Image

Image is a collection of files and meta data, and an Image is layered. Each layer can add, modify and delete files to form a new Image. Different images can share the same layer, and Image itself is read-only.

2.2 to get the Image

You can get official images from Docker Hub

docker pull + image name

For example, docker pull hello-world

[root@localhost ~]# docker pull hello-world Using default tag: latest latest: Pulling from library/hello-world b8dfde127a29: Pull complete Digest: sha256:308866a43596e83578c7dfa15e27a73011bdd402185a84c5cd7f32a88b501a24 Status: Downloaded newer image for hello-world:latest docker.io/library/hello-world:latest [root@localhost ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE Hello - World latest D1165f221234 3 weeks ago 13.3kBCopy the code

2.3 Customizing a Base Image

GCC -static hello. C -o hello

Create Dockerfile

FROM scratch
ADD hello /
CMD ["/hello"]
Copy the code

Run the docker build -t xd1998/hello-docker command.

(“.” indicates the current directory to find the docker file)

Perform the three steps specified in the Dockerfile

Sending build context to Docker Daemon 368.8MB Step 1/3: FROM scratch --> Step 2/3: ADD hello / ---> d9b08486b846 Step 3/3 : CMD ["/hello"] ---> Running in 74e47a7e6df1 Removing intermediate container 74e47a7e6df1 ---> 6f933eff0af6 Successfully built 6f933eff0af6 Successfully tagged xd1998/hello-docker:latestCopy the code

View image: Docker image ls

[root@localhost ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE xd1998/hello-docker latest 6f933eff0af6 2 Minutes ago 865kB Hello - World latest D1165F221234 3 weeks ago 13.3KBCopy the code

Docker History 6f933eff0af6

[root@localhost ~]# docker history 6f933eff0af6 IMAGE CREATED CREATED BY SIZE COMMENT 6f933eff0af6 5 minutes ago /bin/sh -c #(nop) CMD ["/hello"] 0B d9b08486b846 5 minutes ago /bin/sh -c #(nop) ADD file: c6c2AB724DAD6AE96... 865kBCopy the code

Docker run xd1998/hello-docker

The printed result indicates that the Base Image can be executed as a Container

[root@localhost ~]# docker run xd1998/hello-docker
hello docker!
Copy the code

3 Container

3.1 Container Concept

A Container is created with an Image, which is a readable Container layer on top of the Image layer. The relationship between Container and Image is similar to the relationship between an instance and a class in object orientation. Image stores and distributes applications, and Container runs applications.

3.2 Related Operations

View the running container: Docker container ls

View all containers: docker container ls -a

[root@localhost ~]# docker container ls -a
CONTAINER ID   IMAGE                 COMMAND    CREATED             STATUS                         PORTS     NAMES
6e2d332e348f   xd1998/hello-docke    "/hello"   17 minutes ago      Exited (14) 17 minutes ago               bold_albattani
80850a9eb600   hello-world           "/hello"   About an hour ago   Exited (0) About an hour ago             wonderful_pasteur
Copy the code

When you run a container, the command defined in CMD is executed by default. Because the custom CMD is not a memory resident command, the container state changes to Exited after the command is executed

Interactive run container: Docker run-it imageName

Delete a container: docker container rm containerID

Delete an image: docker image rm imageID

The ids of all containers are displayed: docker container ls -aq

Clean all containers: Docker rm $(Docker container ls -aq)

Clear exited containers: docker container ls -f “status=exited” -q

4 Dockerfile syntax and best practices

4.1 the FROM

Best practice: Use the official image as the base image

Create a base image FROM ubuntu:14.04Copy the code

4.2 the LABEL

Best practice: Metadata should be specified

LABEL Maintainer ="[email protected]" LABEL Version ="1.0" LABEL Description =" This is description"Copy the code

4.3 the RUN

Best practice: Each RUN generates a new layer, so it is best to merge multiple commands into one line. For legibility, use a backslash newline.

RUN yum update && yum install -y vim \
    python-dev
Copy the code

4.4 WORKDIR

Best practice: WORKDIR allows you to set the current working directory. Use WORKDIR instead of RUN CD and try to use absolute directories.

WORKDIR demo RUN PWD /test/demoCopy the code

4.5 the ADD/COPY

Best practice: COPY takes precedence over ADD, which in addition to adding files to a specified directory, can also be decompressed. Add a remote file/directory using curl or wget

WORKDIR /root ADD hello test/ # ADD hello to /root/test WORKDIR /root COPY hello Test / # Add hello to /root/testCopy the code

4.6 ENV

Best practice: It is best to use ENV to define constants

ENV MYSQL_VERSION 5.6 # Set constant RUN apt-get install -y mysql-server="${MYSQL_VERSION}" \ && rm -rf /var/lib/apt/lists/* # You can use constant 4 with ${MYSQL_VERSION},Copy the code

4.7 VOLUME/x.

VOLUME supports only the mount point directory of the specified container, not the mount point directory of the host. If there are files before and after the mount point directory path, the docker run command will copy and share all files after the volume mount is complete.

EXPOSE allows you to specify the ports that are exposed to the image, and to dynamically bind to random ports and all addresses of the host machine after startup.

4.8 CMD/ENTRYPOINT

RUN is to execute the command and create a new Image Layer, CMD is to set the default commands and parameters to be executed after the container is started, and ENTRYPOINT is to set the command to be RUN when the container is started.

ENV variables are recognized by Shell, but not by Exec. ENTRYPOINT [“/bin/bash”, “-c”, “echo hello $name”] is executed by Shell.

Vim CMD echo "hello docker" ENRTYPOINT echo "hello docker" # exec format RUN ["apt-get", "install", "-y", "vim"] CMD ["/bin/echo", "hello docker"] ENTRYPOINT ["/bin/echo", "hello docker"]Copy the code

CMD is the default command executed when the container is started. If the Docker run specifies other commands, CMD is ignored. If multiple CMD are defined, only the last one will be executed.

ENTRYPOINT allows the container to run as an application or service that is not ignored and must be executed

Best practice: Write a shell script as ENTRYPOINT

COPY docker-entrypoint.sh /usr/local/bin/
ENTRYPOINT ["docker-entrypoint.sh"]
EXPOSE 27017
CMD ["mongod"]
Copy the code

4.9 Publishing your Own Image

Login: docker Login

Push: Docker push tag:latest

5 Dockfile practice

Create/hello – docker/app. Py:

from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
    return "hello docker"
if __name__ == '__main__':
    app.run()
Copy the code

Create/hello – docker/Dockerfile:

FROM Python :2.7 LABEL "Maintainer =XD1998<[email protected]>" RUN PIP install flask COPY app.py /app WORKDIR /app EXPOSE 5000 CMD ["python", "app.py"]Copy the code

Run the build command: docker build -t xd1998/hello-docker.

The resources

  1. The Dockerfile corresponding to the Image provided by the official
  2. Dockerfile syntax official document