Docker base (2) Practice articles

Docker base (2) Practice articles

  • Docker's instruction
  • Global directive
  • Docker Warehouse Management
  • Docker image management
    • Dockerfile
  • Docker container management
    • Docker Compose
  • Nested commands

Docker's instruction

Object Docker operating instructions for four main areas:

  • Get Set and global information systems for the daemon resources. For example: docker info, docker deamon and so on.
  • Docker queries against the warehouse, the download operation. For example: docker search, docker pull and so on.
  • Docker image for queries, create, delete operation. For example: docker images, docker build and so on.
  • Docker containers for the query, create, open, stop the operation. For example: docker ps, docker run, docker start and so on.

Specific information may be viewed by the docker input terminal, using the docker COMMAND --help can view a further instruction use.
Next, learn some basic common commands.

Acquisition system and global information resource settings daemon

View Docker version information:

$ docker version
Client:
 Version:           18.09.7
 API version:       1.39
 Go version:        go1.10.4
 Git commit:        2d0083d
 Built:             Fri Aug 16 14:19:38 2019
 OS/Arch:           linux/amd64
 Experimental:      false

Server:
 Engine:
  Version:          18.09.7
  API version:      1.39 (minimum version 1.12)
  Go version:       go1.10.4
  Git commit:       2d0083d
  Built:            Thu Aug 15 15:12:41 2019
  OS/Arch:          linux/amd64
  Experimental:     false

View device information, such as the server version, storage drivers, kernel version, operating system, total memory, and so on:

$ docker info
Containers: 0
 Running: 0
 Paused: 0
 Stopped: 0
Images: 14
Server Version: 18.09.7
Storage Driver: overlay2
 Backing Filesystem: extfs
 Supports d_type: true
 Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
...

Docker Warehouse Management

Docker Docker hub is a public warehouse, it normally takes a mirror can be downloaded from here. Users can also upload self-made image.

Find a mirror, the following command will search for the image associated with the centos:

$ docker search centos

Download image, colon can specify a version number, if omitted, the default download the latest version, the equivalent of: latest

$ docker pull imageA:1.0

Upload images, you've got to register Docker Hub account, and use docker login Login

$ docker pull imageA:1.1

Docker image management

View all local mirror

$ docker images 

or

$ docker image ls

Each has a unique ImageID mirror, full-length 128-bit, default display only 12 abbreviations, see also the -a build command generated intermediate image.

Docker mirror having a layered structure, the use of docker history can query a number of layers of image points, and changes made to each layer

$ docker history dockerapitestimage:latest

IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
0112b74edea7        2 weeks ago         /bin/sh -c #(nop)  ENTRYPOINT ["dotnet" "Web…   0B
5185fbd03612        2 weeks ago         /bin/sh -c #(nop) COPY dir:9a2de08b90587d600…   294kB
d2f4b4e061f1        2 weeks ago         /bin/sh -c #(nop)  EXPOSE 5000                  0B
ca882651894c        2 weeks ago         /bin/sh -c #(nop) WORKDIR /app                  0B
e28362768eed        3 weeks ago         /bin/sh -c aspnetcore_version=3.1.1     && c…   17.8MB
<missing>           3 weeks ago         /bin/sh -c dotnet_version=3.1.1     && curl …   76.7MB
<missing>           3 weeks ago         /bin/sh -c apt-get update     && apt-get ins…   2.28MB
<missing>           3 weeks ago         /bin/sh -c #(nop)  ENV ASPNETCORE_URLS=http:…   0B
<missing>           3 weeks ago         /bin/sh -c apt-get update     && apt-get ins…   41.3MB
<missing>           3 weeks ago         /bin/sh -c #(nop)  CMD ["bash"]                 0B
<missing>           3 weeks ago         /bin/sh -c #(nop) ADD file:ba0c39345ccc4a882…   69.2MB

Remove Mirror command

$ docker image rm [Image Name]

Dockerfile

You can specify build step by Dockerfile mirror, every step of the operation performed with a single write, then you can easily direct run.
Here is a simple Dockerfile:

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 5000
COPY . .
ENTRYPOINT ["dotnet", "WebApplication1.dll"]

Dockerfile of grammar rules and common keywords are:

  • Each line with a keyword for the beginning of the line, if the line is too long, you can use the backslash '' newline
  • FROM keyword specifies the base image build used
  • MAINTAINER Keywords: Specifies that the image creator
  • ENV Keywords: Set Environment Variables
  • RUN Keywords: run shell commands, if multiple commands can use "&&" connection
  • COPY Keywords: copy local files to compile machine image file system
  • EXPOSE keyword: Specifies the listening port
  • WORKDIR Keywords: After you create a designated container terminal default landing coming in the working directory
  • ENTRYPOINT Keywords: the difference between keywords and keywords that front, before the keywords are executed when you create a mirror, and ENTRYPOINT specified content will be performed at the start of the container

Dockerfile execution

After writing a good Dockerfile, the directory in which it can be performed by docker build, you can also specify the path where the Dockerfile by -f, -t can be specified using a name that you want to build the image.

$ docker build -t test:1.0

docker build based on the content of Dockerfile, line by line execution based on keywords, every step will generate a temporary intermediate image. The intermediate image may be used docker image ls -a view.

Docker container management

View existing container this machine, the default display only running, increase -a display all containers, including stop

$ docker ps -a

Based image creation and launch container, -d can run in the background so that the vessel, - name can specify the name of the vessel, passing environment variables --env,

$ docker run -d --name api1 [Image Name]

After the container is created, Docker it is assigned a Container ID, also 128, default 12 before the show, or by Container ID specified when creating the vessel name, you can control the start vessel stopped.

$ docker stop <container id/name>
$ docker start <container id/name>

View run log container can be used:

$ docker logs <container id/name>

The following command all the basic information may query the container, including the operation, the storage location, configuration parameters, network settings

$ docker inspect <container id/name>

docker inspect display all the information in JSON format, also can extract the information specified by the "-f" Use template Golang

$ docker inspect -f {{.NetworkSettings.Bridge}} <container id/name>

Real-time view of system resources occupied by the container, such as CPU usage, memory, network and disk overhead:

$ docker stats <container id/name>

Execute commands inside the container way

$ docker exec <container id/name> <cmd>

Docker Compose

Slightly more complex applications will use multiple containers, a container if you want a place to start would be very troublesome, coupled with the dependencies between the cluster and the container will make it very difficult to maintain the application. Docker compose provides a basic multi-container-managed way.
For example, to create a WordPress application requires the following two commands:

$ docker run --name db --env MYSQL_ROOT_PASSWORD=example -d mariadb
$ docker run --name MyWordPress --link db:mysql -p 8080:80 -d wordpress

Because of its dependence db MyWordPress container vessel, so the first start db container.
Stop when the first stop MuWordPress container, then stop db.

The use of Docker Compose it can easily manage the presence of a multi container dependent.
Docker-compose first need to install separately.
And the preparation of docker-compose.yml file, WordPress transformation of the contents of the above command after docker-compose.yml as follows, using yml syntax:

wordpress: 
  image:wordpress
  links:
    - db:mysql
  ports:
    - 8080:80
db:
  image:mariadb
  environment:
    MYSQL ROOT PASSWORD:example

And then perform the directory where the file docker-compose.yml

$ docker-compose up

You can start once all depend on the container WordPress.

After the application is very easy to start and stop:

$ docker-compose stop
$ docker-compose start

docker-compose at start and stop, will automatically identify order dependent between the containers, the container according to the start-stop-by-order dependent.

Nested commands

Docker also supports nested execute commands, such as
Find The container ID:

$ docker ps -a |grep 0453e874jh93

Show only images and associated container name container:

$ docker ps |awk '{pring $2,$NF}'

Batch delete has stopped running container:

$ docker rm $(docker ps -a -q)

Shenkaoziliao
Lee Jin-bang Liu Ye Yin Chen Chun-day Sri Lanka a "step by step learn Docker"

Guess you like

Origin www.cnblogs.com/zhixin9001/p/12392318.html