Basic knowledge and usage of docker, basic knowledge of docker

Source: Internet
Author: User
Tags docker ps

Basic knowledge and usage of docker, basic knowledge of docker
What is docker

DockerIs a container engine that provides a complete set of container solutions. It is the hottest open-source project in 2014, based on the Apache2.0 open source protocol, developed by Go language.

What is container

Containers have a long history. Linux LXC and Solaris Zones are different implementations of containers.

A container is essentially a process running on the host machine. However, some processing was performed before the process was started, and the process entered a completely new virtual environment, which is separated from the host environment. Therefore, this process is considered to be in an independent operating world.

A simple example is that the files created in the container cannot be seen on the host machine.

Container advantages

Container creation and deletion are fast. Containers consume less time during running. On a host machine, the number of containers can be large.

Advantages of docker

Convenient Container Management

Install docker Prerequisites

Docker can run on multiple operating systems or even windows. Here I use centos7.3 for installation. Docker has the following requirements on the operating system:

-Kernel version later than 3.10: You can view it through uname-r.

-64-bit system: You can view it through uname-I.

Install yum

After configuring the yum source, you can directly use the yum command for installation:

yum install docker

Start docker after installation:

service docker start

Run the following command to check the docker version:

# docker versionClient: Version:         1.13.1 API version:     1.26 Package version: 
 
   Go version:      go1.8.3 Git commit:      774336d/1.13.1 Built:           Wed Mar  7 17:06:16 2018 OS/Arch:         linux/amd64Server: Version:         1.13.1 API version:     1.26 (minimum version 1.12) Package version: 
  
    Go version:      go1.8.3 Git commit:      774336d/1.13.1 Built:           Wed Mar  7 17:06:16 2018 OS/Arch:         linux/amd64 Experimental:    false
  
 
Docker Start and Stop

Like other services, docker can use service commands to control start and stop:

'''Start ''' service docker start' stop '''service docker stop''' view status '''service docker status' 'Restart ''' service docker restart
Docker Architecture

Docker is a typical C/S architecture:

Docker server

This is a daemon process that runs on the background all the time. There is a web server embedded in it.

Docker client

It is a command line tool that interacts with docker server through http protocol

Docker server and docker cliebt share an executable file, which can be found by running the which docker command.

Docker Image What is a docker image?

Docker can be understood as a directory: When the docker server starts the container, it first copies a directory based on the image directory, and then when the container process starts, let the process chroot to this directory, so that this directory becomes the root file system of the container.

View docker Images

Run the following command to view the local docker images.

docker imagesREPOSITORY                  TAG                 IMAGE ID            CREATED             SIZEdocker.io/httpd             latest              2e202f453940        6 weeks ago         179 MBdocker.io/centos            latest              ff426288ea90        2 months ago        207 MBdocker.io/hello-world       latest              f2a91732366c        3 months ago        1.85 kBdocker.io/training/webapp   latest              6fae60ef3446        2 years ago         349 MB

Image id: image id. The length is 64 bits. Generally, the first 12 bits are used to indicate a TAG. Each IMAGE can be tagged with multiple tags. REPOSITORY: repository for local image storage (docker is empty after installation)

REPOSITORY and TAG can uniquely identify an image

Docker container Run containers

You can run the docker run Command to run containers. Common options include:

-I-t: These two options are often used together, abbreviated as-it, to create an interactive container-d: run the container in the background, used to create a daemon container -- name: specify a name for the container
Run interactive containers

My local image is as follows:

# docker imagesREPOSITORY                  TAG                 IMAGE ID            CREATED             SIZEdocker.io/httpd             latest              2e202f453940        6 weeks ago         179 MBdocker.io/centos            latest              ff426288ea90        2 months ago        207 MBdocker.io/hello-world       latest              f2a91732366c        3 months ago        1.85 kBdocker.io/training/webapp   latest              6fae60ef3446        2 years ago         349 MB

Now I passDocker. io/centosCreate an interactive container using this image. The command is as follows:

docker run -it centos:latest /bin/bash

-It: Specify to create an interactive container.

Centos: latest:Repository: TAGMethod to specify the image name

/Bin/bash: The process corresponding to the container. A new shell is started.

The preceding command starts a new shell that runs in the container environment. At this point, we leave the original shell, and the shell commands we execute will be in the virtual environment. When exit is executed, the Virtual shell is ended and the entire container is ended.

[root@193cbc5e0dbe /]# lsanaconda-post.log  bin  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

We can see that a new shell is generated after docker run is executed, and the directory before the command prompt is changed193cbc5e0dbe. If the image does not exist when you run this command, docker will automatically pull the image from the official image repository and generate the container.

Run a daemon container

Interactive containers are suitable for some temporary tasks. In most cases, you want to create a daemon container as follows:

docker run -d redis

Create a redis container here, but I am using a locally stuffy redis image, so I will pull the image from the official website and create a container.

Here, the default TAG"LatestIf no command is specified, the default command provided by the image is used. The default command for redis image is/entrypoint. sh redis-server.

The daemon container runs in the background, while the shell is the host shell, so that other work can be performed without the impact of the container.

View basic container Information

Run the following command to view the basic information of the container:

docker psCONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMESf9cb9fe5a189        redis               "docker-entrypoint..."   4 minutes ago       Up 4 minutes        6379/tcp            brave_mccarthy

You can see the container ID, used image, status, and other information.

The previous interactive container information is not visible here, because it exits, but you can add-Options:

docker ps -aCONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMESf9cb9fe5a189        redis               "docker-entrypoint..."   4 minutes ago       Up 4 minutes               6379/tcp            brave_mccarthy193cbc5e0dbe        centos:latest       "/bin/bash"              17 minutes ago      Exited (0) 8 minutes ago                       clever_roentgen

The interactive container is in the exited state.

View container details

The docker ps command can only view some basic content. To learn more details, such as the ip address and port, you need the following command:

docker inspect f9cb9fe5a189

WhereF9cb9fe5a189Is the container ID. This command returns data in json format, which is very detailed. This information is sometimes required for secondary development.

If you only want to obtain the IP address, you can add the following parameters:

docker inspect -f '{{.NetworkSettings.IPAddress}}' f9cb9fe5a189

{. NetworkSettings. IPAddress }}It can be understoodNetworkSettingsUnderIPAddressField

Stop a daemon container

To stop a daemon container, run the following command:

docker stop f9cb

F9cbIs the abbreviation of the container ID. This is also possible. You can't see this container when using docker ps.

Delete container

Stopping a container does not delete the container. You can delete the container as follows:

docker rm f9cb9

F9cb9Is the abbreviation of the container ID, so that the container will not be visible in docker ps-a and it will be deleted.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.