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

How to create a template for a Docker container

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "how to create the template of Docker container". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Docker is an open source container engine, which mainly solves the problems of inconsistent cross-environment and slow environment for installing applications in multi-environment. To cite a few examples, there are generally three kinds of environment development, testing, and formal problems in an actual project. When the test environment is normal, there will be problems in the formal environment, which can not be solved for several days all night, and it is difficult to give birth every time the upgrade is launched. In addition, it is found that a single service cannot resist concurrency after being launched, and it will take a lot of time to deploy the same environment in large numbers. The birth of Docker makes it possible to solve these problems completely and make production more efficient.

To learn Docker, you need to understand several concepts: mirroring, containers, repositories, data volumes, image building, container orchestration clusters, and so on.

Docker basic command

Docker version View docker version

Docker info View docker details

Docker-- help View docker command

1 Image creates a template for the container. The underlying layer uses the federated file system mechanism. When pulling the image from the repository, the Docker engine verifies that it will not download the module when it is needed, and then downloads and installs the dependent modules of other layers on this basis. This mechanism also enables the image to download quickly. Commonly used commands to pull, find, delete, submit.

1.1 docker images to list the images on the local host

Docker images-a lists all local images

Docker images-p shows only mirrored ID

Docker images-- digests displays summary information of the image

Docker images-- no-trunc displays complete image information

1.2 docker search TERM

Docker search tomcat looks for tomcat images from Docker Hub

1.3 docker pull [OPTIONS] NAME [: TAG] can be pulled from a person's public repository (including a private repository). If there is no network, or an image can be obtained from other private servers.

Docker pull tomcat downloads the tomcat image from Docker Hub. Equivalent to: docker pull tomcat:latest

1.4 docker commit-m "submitted description"-a name of the target image to be created by the "author" container ID: [signature] submit the container to make it a new image.

Such as: docker commit-m "new tomcat"-a "lizq" f9e29e8455a5 mytomcat:1.2

1.5 docker rmi

Docker rmi hello-world removes a hello-world image from Docker

Docker rmi-f hello-world forcibly deletes a hello-world image from Docker

Docker rmi-f hello-world nginx forcibly removes hello-world and nginx images from Docker

Docker rmi-f $(docker images-aq) deletes all mirrors through the image ID queried by docker images-aq

2 dynamic image of containers. Multiple containers can be generated by one image. Common commands to create, delete, find, stop.

2.1 docker run [OPTIONS] IMAGE creates and starts the container based on the image. IMAGE is the mirror ID or mirror name

OPTIONS description:

 -- name= "new container name": specify a name for the container

 -d: run the container in the background and return the container ID, that is, start the guardian container

 -I: runs the container in interactive mode, usually in conjunction with-t

 -t: reassigns a pseudo-input terminal to the container, usually used with-I

 -P: random port mapping

 -p: specifies the port mapping, available in the following four formats:

Ip:hostPort:containerPort

Ip::containerPort

HostPort:containerPort

ContainerPort

2.2 docker ps lists all currently running containers

Docker ps-a lists all containers

Docker ps-l lists the recently created containers

Docker ps-n 3 lists the 3 recently created containers

Docker ps-Q shows only container ID

Docker ps-- no-trunc displays the complete information of all currently running containers

Exit exits and stops the container

Ctrl+p+q only exits the container and does not stop the container.

2.3 docker start starts an existing container

2.4 docker restart container ID or container name restart container

2.5 docker stop graceful stop the running container docker stop $(docker ps-aq) batch stop

2.6 docker kill immediately stops the main processes in the container

2.7 docker rm

Docker rm container ID or container name delete container

Docker rm-f Container ID or Container name Force Delete Container

Docker rm $(docker ps-a-Q) deletes multiple containers

Docker logs-f-t-- since-- tail container ID or container name view container log

For example: docker logs-f-t-- since= "2018-09-10"-- tail=10 f9e29e8455a5

 -f: view real-time logs

 -t: view the date on which the log was generated

 -- since: this parameter specifies the start date of the output log, that is, only logs after the specified date are output

 -- tail=10: view the last 10 logs

2.8 docker top Container ID or Container name View processes running within the container

2.9 docker inspect is used to view the details of images and containers. All the information is listed by default. You can specify the template format of the output through the-- format parameter to output specific information.

2.10 docker attach can connect to a running container, observe the container's health, or interact with the container's main process

2.11 docker exec container ID goes into the container

2.12 docker cp container ID: the file path in the container the host path copies files from the container to the host.

(3) the warehouse is divided into public warehouse and private warehouse, and the warehouse stores the server of the image.

The data volume is the mapping of the data directory between the host and the container, through the-v host directory: virtual machine directory. The function is to persist the data. If the container is not set, the container will be deleted, and the important data will be deleted. Another function is data synchronization between containers.

4. Image construction creates a dockerFile through the Docker syntax keyword, and then generates an image by command. In fact, it is to generate an image through the command file.

4.1 create data Volum

When using the dockerrun command, use the-v parameter flag to create a data volume within the container, and use the-v flag multiple times to create multiple data volumes

Docker run-dp-- name testVolume-v / testVolume ubuntu:14.04

Use the-v flag to also specify that a local existing directory be mounted to the container as a data volume

Docker run-dp-- name testVolume-v / src/webapp:/opt/testVolume ubuntu:1404

The default permission for docker to mount data volumes is read and write, and ro specifies read-only:

Docker run-dp-- name web-v / src/webapp:/opt/webapp:ro ubuntu:14.04

4.2 data volume container

You need to share some continuously updated data between containers. The easiest way is to use a data volume container, which is actually a common container designed to provide data volumes for other containers to mount.

Create a data volume container dbdata and create a data volume in it to mount to / dbdata:

Docker run-ti-v / dbdata-- name dbdata ubuntu:14.04

Then we can use-volumes-form in other containers to mount the data volumes in the dbdata container, such as creating db1 and db2 containers, and mounting the data volumes from the dbdata container:

Docker run-ti-volumes-from dbdata-name db1 ubuntu:14.04

Docker run-ti-volumes-from dbdata-name db2 ubuntu:14.04

5 dockerFile syntax

Dockerfile consists of one line of command statements and supports comment lines that begin with #.

Generally speaking, Dockerfile is divided into four parts: basic image information, maintainer information, mirror operation instructions and instructions to execute when the container starts.

5.2 the FROM format is FROM or FROM:.

The first instruction must be a FROM instruction. Also, if you create multiple mirrors in the same Dockerfile, you can use multiple FROM instructions (one for each mirror).

5.3 MAINTAINER format specifies maintainer information for MAINTAINER.

5.4 the RUN format is RUN or RUN ["executable", "param1", "param2"].

The former will run the command / bin/sh-c in the shell terminal; the latter will be executed using exec. Specifying the use of other terminals can be done in a second way, such as RUN ["/ bin/bash", "- c", "echo hello"].

Each RUN instruction executes the specified command based on the current mirror and is submitted as a new mirror. You can use\ to wrap lines when the command is long.

5.5 CMD

Three formats are supported

CMD ["executable", "param1", "param2"] is executed using exec, recommended

CMD command param1 param2 is executed in / bin/sh and provided to applications that need to interact

Default parameters provided to ENTRYPOINT by CMD ["param1", "param2"]

Specifies the command to be executed when the container is started, and there can be only one CMD command per Dockerfile. If multiple commands are specified, only the last one will be executed.

If the user starts the container and specifies the command to run, the command specified by CMD will be overwritten.

5.6 the EXPOSE format is EXPOSE [...].

Tell the port number exposed by the Docker server container for use by the interconnected system. When starting the container, the host will automatically assign a port to be forwarded to the specified port through-P _ ~ ~ Docker.

5.7 the ENV format is ENV. Specify an environment variable that will be used by subsequent RUN instructions and maintained while the container is running.

For example

ENV PG_MAJOR 9.3

ENV PG_VERSION 9.3.4

RUN curl-SL http://example.com/postgres-$PG_VERSION.tar.xz | tar-xJC / usr/src/postgress & & …

ENV PATH / usr/local/postgres-$PG_MAJOR/bin:$PATH

5.8 ADD format is ADD.

This command copies the specified to the container. It can be a relative path to the directory where Dockerfile is located, a URL; or a tar file (which is automatically extracted to a directory).

5.9 COPY format is COPY.

Copy the local host's (the relative path of the directory where the Dockerfile is located) to the container.

COPY is recommended when using the local directory as the source directory.

5.10 ENTRYPOINT

There are two formats:

ENTRYPOINT ["executable", "param1", "param2"]

ENTRYPOINT command param1 param2 (executed in shell).

Configure the commands that are executed after the container starts and cannot be overridden by the parameters provided by docker run.

There can be only one ENTRYPOINT per Dockerfile, and when more than one is specified, only the last one takes effect.

5.11 the VOLUME format is VOLUME ["/ data"].

Create a mount point that can be mounted from a local host or other container, which is generally used to store databases, data that needs to be maintained, and so on.

5.12 USER format is USER daemon.

Specify the user name or UID when the container is run, and subsequent RUN will also use the specified user.

When the service does not require administrator privileges, you can specify the running user through this command. And you can create the desired user before, for example: RUN groupadd-r postgres & & useradd-r-g postgres postgres. To obtain administrator privileges temporarily, you can use gosu instead of recommended sudo.

5.13 the WORKDIR format is WORKDIR / path/to/workdir.

Configure the working directory for subsequent RUN, CMD, and ENTRYPOINT instructions.

Multiple WORKDIR instructions can be used, and subsequent commands, if the argument is a relative path, are based on the path specified by the previous command. For example

WORKDIR / a

WORKDIR b

WORKDIR c

RUN pwd

The final path is / a/b/c.

5.14 the ONBUILD format is ONBUILD [INSTRUCTION].

Configure the operation instructions that are performed when the created mirror is used as the base mirror for other newly created mirrors.

For example, Dockerfile creates a mirror image-A with the following content.

[...]

ONBUILD ADD. / app/src

ONBUILD RUN / usr/local/bin/python-build-- dir / app/src

[...]

If you create a new image based on image-A, when you specify the base image using FROM image-An in the new Dockerfile, the content of the ONBUILD instruction will be executed automatically, which is equivalent to adding two instructions later.

FROM image-A

# Automatically run the following

ADD. / app/src

RUN / usr/local/bin/python-build-- dir / app/src

Use the image of the ONBUILD instruction, which is recommended to be indicated in the tag, such as ruby:1.9-onbuild

6 Container choreography means that some production environments have a large number of containers to maintain and a lot of repetitive operations. Through docker-compose batch operation of containers, efficient and safe maintenance of containers.

This is the end of "how to create templates for Docker containers". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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