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

Basic operating commands of Docker

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

Share

Shulou(Shulou.com)06/02 Report--

Docker introduction

Docker is an open source engine that automatically deploys development applications to containers. It was written by the Docker team and licensed under the Apache 2.0 open source agreement. It provides a simple, lightweight modeling method, makes the development life cycle more efficient and faster, and encourages service-oriented architecture design. The goal of the Docker project is to implement a lightweight operating system virtualization solution. Docker is based on technologies such as Linux container (LXC). Docker is further encapsulated on the basis of LXC, so that users do not need to care about the management of the container, making the operation easier. Users can manipulate Docker containers as easily as a fast and lightweight virtual machine.

Characteristics of Docker:

Faster delivery and deployment

More efficient virtualization

Easier migration and expansion

Easier management

Performance comparison between Container Technology and traditional Virtual Machine

Comparison between Docker and Virtual Machine Construction

The Docker container is essentially a process on the host. Docker realizes resource isolation through namespace, resource restriction through cgroups, and efficient file operation through write-time copy mechanism (copy-on-write).

Docker has five namespaces: process, network, mount, host and shared memory. In order to isolate problematic applications, Docker uses Namespace to isolate processes, create isolated running spaces for processes or process groups, and provide different views of namespaces for processes. Thus, each isolated process group is externally represented as a container (container). It is important to note that Docker makes users think that they occupy all the resources, but this is not a "virtual machine".

Three concepts in Docker: image, container, warehouse

Image: a Docker image is a read-only template that can be used to create a Docker container. Docker provides a simple mechanism to create or update an existing image, and users can even download an image from someone else to use it directly.

Mirroring is a file structure. Each command in Dockerfile creates a new hierarchy in the file system on which the file system is built, and the image is built on top of these federated file systems. The official Docker website has a dedicated page to store all available images at index.docker.io.

Container (Container): a container is a running instance created from an image. It can be started, started, stopped, and deleted. Each container is an isolated and secure platform. Think of the container as a simple version of the Linux environment, and Docker uses the container to run applications. The image is read-only, and the container creates a writable layer as the top layer at startup.

Warehouse: a warehouse is a place where images are stored centrally. Multiple repositories are often stored on the warehouse registration server (Registry), and each warehouse contains multiple images, each with a different tag. At present, the largest public repository is Docker Hub, which stores a large number of images for users to download.

The Docker repository is used to store our images, and after we have created our own image, we can use the push command to upload it to a public or private warehouse, so that the next time we want to use this image on another machine, we just need to pull it from the warehouse. The concept of a Docker repository is similar to that of Git, and the registration server can be understood as a service such as GitHub.

Basic operation of Docker

[root@localhost ~] # docker search mysql// finds an image

This search is equivalent to searching in https://hub.docker.com/. Try to use the official image.

[root@localhost ~] # docker pull busybox// pull image

[root@localhost ~] # docker save-o busybox.tar busybox:latest// exports the image locally-o: quite-- output export

[root@localhost ~] # docker images// to view local images

Repository (image name) Image label Image id creation time size

Although we see the image tag as latest (latest), it doesn't mean it's necessarily up-to-date. And if the image is not tagged, it is tagged with latest by default.

[root@localhost ~] # docker rmi busybox:latest// delete image

[root@localhost ~] # docker images// View the local image. There is no busybox.

[root@localhost ~] # docker load-I busybox.tar// imports the image based on the local image package

[root@localhost ~] # docker images// View the local image and there is busybox

[root@localhost ~] # docker ps// View Container-running [root@localhost ~] # docker ps-a docker rm c3bb3a6f73eb// / View all containers [root@localhost ~] # docker rm c3bb3a6f73eb// Delete container id or image name (cannot delete running container)

[root@localhost ~] # docker stop test// stops the container (remember to verify docker ps-a)

[root@localhost ~] # docker start test// launch container (remember to verify docker ps-a)

[root@localhost ~] # docker rm test-fram / forcibly delete containers (remember to verify docker ps-a)

[root@localhost ~] # docker ps-a-Q | xargs docker rm-f / all containers are forcibly deleted (forbidden in production environment)

[root@localhost ~] # docker ps-a-Q | xargs docker start-f it / forcibly open all containers (forbidden in production environment) [root@localhost ~] # docker ps-a-Q | xargs docker stop-f hand / forcibly close all containers (prohibited in production environment) [root@localhost ~] # docker run-it-- name test1 busybox:latest// opens a container-I: interactive-t: pseudo terminal-d: guard Daemon-- name: container naming-- restart=always: always keep running (run with docker enabled) [root@localhost ~] # docker run-itd-- name test2-- after restart=always busybox:latest//docker restart Always keep running (run with docker turned on)

Routing Forwardin

[root@localhost ~] # vim / etc/sysctl.conf / / add route forwarding [root@localhost ~] # sysctl-pnet.ipv4.ip_forward = 1net.bridge.bridge-nf-call-iptables = 1net.bridge.bridge-nf-call-ip6tables = 1

Enter the container method

[root@localhost ~] # docker exec-it test2/ bin/sh// enters a container (still running after exiting the container) [root@localhost ~] # docker attach test2// also enters a container (exit container is not running) difference: the exec entry method needs to add the-I-t option, and then you need to give the container a shell environment. But attach doesn't have to go to so much trouble. How exec enters: if you perform an exit exit, the container remains running. Attach: if you perform an exit exit, the container will be closed. If you want to keep the container unclosed, you can use the keyboard: Ctrl + p Ctrl + Q. Essentially distinguishing: the way exec enters will produce new processes. Attach does not produce new processes.

Force deletion of a mirror

[root@localhost ~] # docker rmi centos:7-f id / forcibly delete the image tag. To delete the image completely, delete the image id with the following command. Docker has a caching mechanism. Even if the image is deleted, it will be cached. Other containers can still use the basic operation logic of Docker.

Run a container based on the centos: 7 image, and deploy the Nginx service within that container.

1) download centos:7 image

[root@localhost ~] # docker pull centos:7

[root@localhost ~] # rz upload a nginx package

2) run the container

[root@localhost] # docker run-itd-- name webapp-- restart=always centos:7

3) enter the container and start deploying the nginx service

[root@localhost ~] # docker cp nginx-1.14.0.tar.gz webapp:/root// imports the nginx package into the container [root@localhost ~] # docker exec-it webapp / bin/bash// enters the container [root@8604fb370aab /] # ls root

Install nginx

[root@8604fb370aab /] # cd / root [root@8604fb370aab ~] # tar zxf nginx-1.14.0.tar.gz [root@8604fb370aab ~] # yum-y install gcc pcre pcre-devel openssl-devel zlib zlib-devel make// to install nginx depends on [root@8604fb370aab ~] # cd nginx-1.14.0 [root@8604fb370aab nginx-1.14.0] # useradd-M-s / sbin/nologin nginx// to create users [root@8604fb370aab nginx-1.14.0] #. / configure-- prefix=/usr/local/nginx-- user=nginx-- group=nginx & & make & & make install// compilation and installation [root@8604fb370aab nginx-1.14.0] # ln-s / usr/local/nginx/sbin/nginx / usr/local/sbin/// link command directory [root@8604fb370aab nginx-1.14.0] # nginx// launch nginx [root @ 8604fb370aab nginx-1.14.0] # cd / usr/local/nginx/html/ [ Root@8604fb370aab html] # echo This is a testweb in container > index.html// create a test page [root@8604fb370aab html] # curl 127.0.0.1 / visit the web page

[root@8604fb370aab /] # yum provides ip// to see which component supports this command [root@8604fb370aab /] # yum-y install net-tools// installs [root@8604fb370aab /] # ifconfig// that supports this command to view ip

Host to view web pages

[root@localhost ~] # curl 172.17.0.4

[root@localhost ~] # docker commit webapp myweb:xgp// makes the container into an image (a hash value is returned, representing the id number of the image) to increase portability

[root@localhost ~] # docker images// View Image

[root@localhost ~] # docker run-itd-- name webapp-2 myweb:xgp [root@localhost ~] # docker exec-it webapp-2 / bin/bash [root@e8d15e9aef29 /] # nginx [root@e8d15e9aef29 /] # curl 127.0.0.1This is a testweb in container [root@e8d15e9aef29 /] # ifconfig

View a web page

[root@localhost ~] # curl 172.17.0.5

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