Docker basic operation usage

Chen Bucheng I 2022-06-24 07:17:46 阅读数:136


One .Docker install

Docker It's an open source commercial product , There are two versions : Community Edition (Community Edition, Abbreviation for CE) And enterprise (Enterprise Edition, Abbreviation for EE). The enterprise edition includes some charging services , Personal developers generally don't use . The following descriptions are for the Community Edition .

here centos Use yum Can be installed yum -y install docker

After installation , Run the following command , Verify that the installation was successful docker version

Docker Is the server —- Client Architecture . Command line run docker When ordered , This machine is required to have Docker service . If this service is not started , You can use the following command to start service docker start

Two .image file

Docker Put the application and its dependencies , Pack in image In the document . Only through this document , Can be created Docker Containers .image Files can be seen as templates for containers .Docker according to image An instance of a file generation container . The same image file , You can generate multiple container instances running at the same time .

image It's binary . In development , One image Documents are often inherited by another image file , Add some personalization to create . for instance , You can Ubuntu Of image On the basis of , Add... To it Apache The server , Form your image.

List all of the image file . docker images

Delete image file docker image rm [imageName]

image Documents are universal , Of a machine image Files copied to another machine can still be used . Generally speaking, to save time , We should try to use what others have made image file , Instead of making it yourself . Even if you want to customize , It should also be based on other people's image Document processing , Not from scratch .

For the convenience of sharing ,image When the document is finished , It can be uploaded to the warehouse on the Internet .Docker The official warehouse of Docker Hub Is the most important 、 Most commonly used image Warehouse . Besides , Sell what you make image Documents are also available .

3、 ... and . example hello world

1. below , We use the simplest image file ”hello world”, Feel the Docker.

It should be noted that , Domestic connection Docker The official warehouse is very slow , It's also broken , You need to change the default warehouse to a domestic mirror website . echo 'DOCKER_OPTS="--registry-mirror="' >> /etc/default/dockerservice docker restart

2. First , Run the following command , take image File fetching from warehouse to local . docker image pull library/hello-world

In the above code ,docker image pull Grab. image The order of the document .library/hello-world yes image Location of documents in the warehouse , among library yes image Group of files ,hello-world yes image Name of file .

3. because Docker Official image file , All put in library In the group , So its default group can be omitted . So the above command can be written as follows . docker image pull hello-world

4. After the capture is successful , You can see this on this computer image The file . docker image ls

5. Now? , Run the image file . docker container run hello-world

docker container run Command from image file , Build a running container instance .

Be careful docker container run Command has auto grab image File functionality . If it is found that there is no specified image file , It will be automatically grabbed from the warehouse . So the previous docker image pull Command is not a required step .

If the operation is successful , You will read the following output on the screen .

Hello from Docker!
This message shows that your installation appears to be working correctly.
... ...

6. After outputting this prompt ,hello world It will stop running , Container auto terminate .

Some containers do not terminate automatically , Because it's a service . Such as installation and operation Ubuntu Of image, You can experience it on the command line Ubuntu System . docker container run -it ubuntu bash

For containers that do not automatically terminate , You have to use docker container kill Command manual termination . docker container kill [containID]

Four . Container file

image Container instance generated by file , It's also a document , Called a container file . That is, once the container is generated , There will be two files at the same time : image Files and container files . Also, closing the container does not delete the container file , It's just that the container stops running .

List the containers this machine is running docker container ls

List all containers of this machine , Include containers that terminate operations docker container ls --all

In the output of the above command , Including the container ID. Many places need to provide this ID, For example, in the previous section, the container was terminated docker container kill command .

Terminate the running container file , It will still occupy the hard disk space , have access to docker container rm Command deletion . docker container rm [containerID]

After running the above command , Reuse docker container ls –all command , You will find that the deleted container file has disappeared .

copyright:author[Chen Bucheng I],Please bring the original link to reprint, thank you.