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

What are the three concepts of Docker?

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly explains "what are the three concepts of Docker". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the three concepts of Docker"?

Docker

What is Docker?

Docker is a virtual environment container in which you can package your development environment, code, configuration files, etc., and publish and apply them to any platform.

Three Concepts of Docker

Mirror (Image)

Similar to a mirror in a virtual machine, it is a read-only template for the Docker engine that contains the file system. Any application needs an environment to run, and mirroring is used to provide such a running environment. For example, a Ubuntu image is a template that contains the Ubuntu operating system environment. Similarly, if you install Apache software on the image, it can be called an Apache image.

Container (Container)

Similar to a lightweight sandboxie, you can think of it as a minimalist Linux system environment (including root permissions, process space, user space, cyberspace, etc.) and applications running in it. The Docker engine uses containers to run and isolate applications. Containers are application instances created by images, which can be created, started, stopped, and deleted. Containers are isolated from each other and do not affect each other. Note: the image itself is read-only. When the container starts from the image, Docker creates a writable layer on the upper layer of the image, and the image itself remains unchanged.

Warehouse (Repository)

Similar to the code repository, this is the image repository, where Docker centrally stores image files. Note the difference between the registration server and the registration server (Registry): the registration server is the place where repositories are stored, and there are usually multiple repositories. Generally, each warehouse stores a class of images, each of which is distinguished by tag. For example, Ubuntu warehouse stores multiple Ubuntu images (12.04,14.04, etc.).

Installation of Docker

Docker can be installed on various platforms such as Windows, Linux, Mac, etc. You can view the document Install Docker for details.

After the installation is complete, you can view the version information of Docker:

$docker versionClient: Docker Engine-Community Version: 19.03.13 API version: 1.40 Go version: go1.13.15 Git commit: 4484c46d9d Built: Wed Sep 16 17:03:45 2020 OS/Arch: linux/amd64 Experimental: falseServer: Docker Engine-Community Engine: Version: 19.03.13 API version: 1.40 (minimum version 1.12) Go version: go1.13.15 Git commit: 4484c46d9d Built: Wed Sep 16 17:02:21 2020 OS/Arch: linux/amd64 Experimental: false containerd: Version: 1.3.7 GitCommit: 8fba4e9a7d01810a393d5d25a3621dc101981175 runc: Version: 1.0.0-rc10 GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd docker-init: Version: 0.18.0 GitCommit: fec3683

Check out Docker's help information: # docker-- help.

The basic operation of mirroring in Docker

Pull the CentOS image from the repository of the official registration server (https://hub.docker.com). As mentioned earlier, each repository will have multiple images, marked with tag. If tag is not added, latest image is used by default:

# docker search centos # check whether the centos image exists # docker pull centos # obtain the image using the pull command # docker images # View the images information in the current system

Create your own image

# docker run-it centos:latest / bin/bash # launch a container # # here the command line format has changed, indicating that you have entered a new environment # git-- version # there is no git# yum install git# in the container using yum to install git# git-- version # is already installed in the container.

View the running container

At this point, use exit to exit the container, and then check the program (container) running in docker: # docker ps-aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES72f1a8a0e394 centos:latest "/ bin/bash" 9 minutes ago Exited (0) 3 minutes ago

Here, the container is converted into an image, that is, the commit operation is performed. After completion, you can use docker images to view:

# docker commit-m "centos with git"-a "qixianhu" 72f1a8a0e394 xianhu/centos:git# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZExianhu/centos git 52166e4475ed 5 seconds ago 358.1 MBcentos latest 0584b3d2cf6d 9 days ago 196.5 MB

Where-m specifies the description information;-a specifies the user information; and 72f1a8a0e394, on behalf of the id;xianhu/centos:git of the container, specifies the user name, warehouse name and tag information of the target image. Notice the user name xianhu here, which will be used later.

At this point, our newly created image xianhu/centos:git is available in the Docker engine. The difference between this image and the original CentOS image is that there is a Git tool. At this point, the container created by the new image has its own git.

# docker run-it xianhu/centos:git / bin/bash# git-- versiongit version 1.8.3.1

Exit the container using exit. Notice that there are two containers in the Docker engine that can be viewed using docker ps-a.

Using Dockerfile to create Mirror

Dockerfile can be understood as a configuration file that tells docker build what a command should do. A simple Dockerfile file is shown below. Official statement: Dockerfile reference:

# describe the basic information of the FROM centos:latest# builder based on which image the image is based. RUN yum updateRUN yum install-y git# copy the local file to the image COPY. / * / usr/share/gitdir/ performed by MAINTAINER xianhu# during the build image

With Dockerfile, you can use the build command to build the image:

# docker build-t = "xianhu/centos:gitdir".

Where-t is used to specify the user information, tag, and so on of the new image. The last dot indicates looking for Dockerfile in the current directory.

After the build is complete, you can also use the docker images command to see:

# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZExianhu/centos gitdir 0749ecbca587 34 minutes ago 359.7 MBxianhu/centos git 52166e4475ed About an hour ago 358.1 MBcentos latest 0584b3d2cf6d 9 days ago 196.5 MB

These are two ways to build your own image. Some operations of the container are also involved. If you want to delete a container or image, you can use the rm command. Note: the container based on the image must be deleted before deleting the image.

# docker rm container_name/container_id# docker rmi image_name/image_id

Mirror other operation instructions:

# docker save-o centos.tar xianhu/centos:git # saves the image,-o can also be-- output# docker load-I centos.tar # load image,-I can also be-- input

The basic operation of container in Docker

In the previous section on mirroring, we have seen how to start a container, the docker run operation, based on the image.

[root@xxx ~] # docker run-it centos:latest / bin/bash

Here-it is two parameters:-I and-t. The former means to open and keep the stdout, while the latter means to allocate a terminal (pseudo-tty). At this point, if you exit using exit, the state of the container is in Exit instead of running in the background. If you want the container to keep running instead of stopping, you can use the shortcut key ctrl+p ctrl+q to exit, where the container's state is Up.

In addition to these two parameters, the run command has many other parameters. One of the more useful is-d background operation:

[root@xxx ~] # docker run centos:latest / bin/bash-c "while true; do echo hello; sleep 1; done" [root@xxx ~] # docker run-d centos:latest / bin/bash-c "while true; do echo hello; sleep 1; done"

The second command here uses the-d parameter to keep the container running in the background and does not produce any output to the current terminal. All stdout are output to log, which can be viewed using docker logs container_name/container_id.

Start, stop, restart container commands:

[root@xxx ~] # docker start container_name/container_ id [root @ xxx ~] # docker stop container_name/container_ id [root @ xxx ~] # docker restart container_name/container_id

After launching a container in the background, if you want to enter the container, you can use the attach command:

[root@xxx ~] # docker attach container_name/container_id

The command to delete the container has been mentioned earlier:

[root@xxx ~] # docker rm container_name/container_id

Basic Operation of Warehouse in Docker

Docker officially maintains a public repository of DockerHub, which contains a lot of commonly used images. In addition to downloading images from above, we can also push our own custom images to DockerHub.

In the section on mirror operations, we created a new xianhu/centos:git image.

(1) if you do not have an account to access https://hub.docker.com/, you need to register one first.

(2) Log in to DockerHub with the command docker login, and enter the user name and password to log in successfully:

[root@xxx] # docker loginLogin with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.Username: xianhuPassword:Login Succeeded

(3) push the local image to the DockerHub, where the xianhu should be the same as the username when logging in:

[root@xxx ~] # docker push xianhu/centos:git # successfully pushed [root@xxx ~] # docker push xxx/centos:git # failed The push refers to a repository [docker.io/xxx/centos] unauthorized: authentication required

(4) in the future, others can download the appropriate image from your warehouse.

[root@xxx ~] # docker pull xianhu/centos:git

Corresponding to the two ways to create an image, there are also two ways to update the image:

Make changes after creating the container, then commit generates the image, and then push it to the repository.

Update Dockerfile. This way is generally recommended at work, which is more concise and clear.

Once again, let's review three important concepts: images, containers, and repositories:

Download (pull) an image from the repository (usually DockerHub), and Docker executes the run method to get a container in which the user performs various operations. Docker executes the commit method to convert a container into a mirror. Docker uses commands such as login and push to push the local image to the repository. This image can be used on other machines or servers to generate containers and then run the corresponding applications.

Docker command

Command usage

Docker pull gets image

Docker build creates image

Docker images lists image

Docker run runs container

Docker ps lists container

Docker rm Delete container

Docker rmi Delete image

Docker cp copies files between host and container docker commit saves changes to new image

Dockerfile command

Instruction description

FROM sets the basic image used by the image

The author of the MAINTAINER setting image

Scripts that RUN runs when compiling mirrors

CMD sets the startup command of the container

LABEL sets the label of the image

EXPOSE sets the port exposed by the image

ENV sets the environment variable of the container

When ADD compiles the image, copy the file to the image.

When COPY compiles the image, copy the file to the image.

ENTRYPOINT sets the entry program for the container

VOLUME sets the mount volume of the container

USER sets the user name to run RUN CMD ENTRYPOINT

WORKDIR sets the working directory of the RUN CMD ENTRYPOINT COPY ADD directive ARG sets the parameters added when compiling the image

ONBUILD sets the ONBUILD instruction for the image

STOPSIGNAL sets the exit semaphore of the container

Volume operation

To put it bluntly, Volume is just the-v parameter. There are three ways to mount container and host directories:

Run the Nginx container,-d: run in the background,-- name: specify the name nginx,-v / usr/share/nginx/html: the address used inside the running container to access the web page, and the last nginx is the image name.

Docker run-d-name nginx-v / usr/share/nginx/html nginx

Use the-v parameter to mount the local directory to the container directory when starting the Nginx container

Docker run-p 8080 PWD:/usr/share/nginx/html 80-- name mynginx-v $PWD:/usr/share/nginx/html-d nginx

Use docker create to create a new container without starting it:

Docker create-v $PWD/data:/var/mydata-- name data_container ubuntu

Registry introduction

Registry is an image repository. You can either pull some images from the image repository and send them locally, or you can submit them to the repository.

Some terms:

ENGLISH Chinese

Host host image image

Container container

Registry warehouse

Daemon daemon client client

Compose multi-container application

Compose is a Docker application that is user-defined and runs multiple containers. In Compose you can use YAML files to configure your application services. Then, with a simple command, you can create and start all the services you configure.

Docker-compose.yml common commands

Command usage

Build creates an image locally

Command overrides the default command

Depends_on connection Container

Ports exposed port

Volumes Volum

Image pull Mirror

Up Startup Service

Stop out of service

Rm deletes each container in the service logs looks at the log of each container ps lists the containers related to the service

Thank you for your reading, the above is the content of "what are the three concepts of Docker". After the study of this article, I believe you have a deeper understanding of what the three concepts of Docker are, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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