Docker---2.Docker container usage articles

1. Concept

Docker has three basic concepts, namely Image , Container and Repository .

  • Image : Docker image is equivalent to a root file system. For example, the official image ubuntu:16.04 contains a complete set of the root file system of the Ubuntu16.04 minimal system.
  • Container : The relationship between image and container is like classes and instances in object-oriented programming. The image is a static definition, and the container is the entity when the image is run. Containers can beCreate, start, stop, delete, pausewait.
  • Repository : The repository can be regarded as a code control center, used to save images.

PS:

  • Docker uses a client-server (C/S) architecture model and uses remote APIs to manage and create Docker containers.

  • Docker containers are created from Docker images.

  • The relationship between containers and images is similar to objects and classes in object-oriented programming.

Insert image description here

concept illustrate
Docker images (Images) Docker images are templates used to create Docker containers, such as Ubuntu systems.
Docker container(Container) A container is an application or a group of applications that runs independently and is the entity of the image runtime.
Docker client (Client) The Docker client uses the Docker SDK (https://docs.docker.com/develop/sdk/) to communicate with the Docker daemon through the command line or other tools.
Docker host (Host) A physical or virtual machine used to execute the Docker daemon and containers.
Docker Registry Docker warehouse is used to save images and can be understood as a code warehouse in code control. Docker Hub (https://hub.docker.com) provides a huge collection of images for use. A Docker Registry can contain multiple repositories; each repository can contain multiple tags; each tag corresponds to an image. Usually, a warehouse will contain images of different versions of the same software, and tags are often used to correspond to each version of the software. We can specify which version of this software is the image through the format of <warehouse name>:<tag>. If no label is given, latest will be used as the default label.
Docker Machine Docker Machine is a command line tool that simplifies the installation of Docker. You can install Docker on the corresponding platform through a simple command line, such as VirtualBox, Digital Ocean, and Microsoft Azure.

2. Related commands

2.1 Start docker

service docker start 

If you do not start the docker command, an error will appear.

Cannot connect to the Docker daemon at unix:///var/run/docker.sock. 
Is the docker daemon running?

2.2 docker run

Command: docker run,Use Docker to run an application inside a container.

2.2.1 Docker’s Hello World!

docker run ubuntu:15.10 /bin/echo "Hello,World!"

Insert image description here
Command analysis:

  • docker: Docker binary executable file.

  • run: Combined with the previous docker to run a container.

  • ubuntu:15.10 specifies the image to be run. Docker first checks whether the image exists on the local host. If it does not exist, Docker will download the public image from the image warehouse Docker Hub.

  • /bin/echo “Hello world”: Command executed in the started container.

2.2.2 Some related parameters

Some relevant parameters of run:

  • -t : Specify a pseudo terminal or terminal within the new container.

  • -i : Allows you to interact with the standard input (STDIN) within the container.

For example: open an ubuntu system image

root@4HTFKO9PKNWGWU0:~# docker run -i -t ubuntu:15.10 /bin/bash
root@6a7bd21754c5:/# echo "Hello World!"
Hello World!

Exit the system using exit:

root@6a7bd21754c5:/# exit
exit
root@4HTFKO9PKNWGWU0:~#

2.2.3 Container mode running in the background

dokcer runCan be run in the background using the command

docker run -d ubuntu:15.10 /bin/sh -c "while true; do echo hello world; sleep 1; done"

Insert image description here
We can docker psview this by listing all current mirrors.

docker ps

Insert image description here
Output details:

  • CONTAINER ID: Container ID.

  • IMAGE: The image used.

  • COMMAND: The command to run when starting the container.

  • CREATED: The creation time of the container.

  • STATUS: Container status.

    • created
    • restarting
    • running or Up (running)
    • removing (migrating)
    • paused
    • exited(stopped)
    • dead
  • PORTS: The port information of the container and the connection type used (tcp\udp).

  • NAMES: Automatically assigned container names.

When we look at the background running, we can probably understand that it outputs one Hello World, which can be used docker logs + 容器ID/容器Namesto view the standard output of the container.

docker logs 92927a25d292
docker logs admiring_borg

Insert image description here
Insert image description here

ps: Container ID and container Names can be found from docker psinside

2.2.4 Stop the container running in the background

Use commanddocker stop 容器ID/容器Names

 docker stop admiring_borg

Insert image description here

2.3 Container usage

2.3.1 Client command view

  • View all commands useddocker
    Insert image description here
  • Use the command docker command --helpto view the specific content, for example:
docker stats --help

Insert image description here

2.3.2 Container usage

2.3.2.1 Pull the image

Use the command docker pull 镜像, which is often used when there is no local mirror. For example: pull the ubuntu image.
Insert image description here

2.3.2.2 Using containers

docker run -it ubuntu /bin/bash

Here -itis -i -tthe abbreviation of . The meaning of the command is to use the ubuntu image to generate an interactive terminal container for use.

Parameter Description:

  • -i: interactive operation.
  • -t: terminal.
  • ubuntu: ubuntu screenshot。
  • /bin/bash: The command is placed after the image name. Here we hope to have an interactive shell, so /bin/bash is used.

Insert image description here

Exit container command:exit

Insert image description here

2.3.2.3 Stopped container and restarted it

View stopped containers

docker ps -a

Insert image description here


Start a stopped container

docker start 容器ID

Insert image description here


Running containers in the background

When we need the container to run in the background instead of running directly, just add parameters. -dThis
Insert image description here
way we will not enter directly, but we can use docker execcommands to enter the background to run the container.


Enter the background container
  • docker exec: Recommended because this command will exit the container terminal but will not cause the container to stop.
  • docker attach: After using this command to enter, if you exit from the container, it will cause the container to stop.

docker execCommand usage

docker exec -it 4ea881615c3b /bin/bash

Insert image description here

After exiting, the container still exists
Insert image description here


docker attachCommand usage
Insert image description here


2.3.2.4 Import and export containers

export container

docker export

docker export 容器ID > 导出文件名.导出文件类型
例如:
docker export 1e560fca3906 > ubuntu.tar

Insert image description here


Import container

docker import

Insert image description here


2.3.2.5 Delete container

docker rm -f 容器IDIt needs to be in stopped state when deleting.

docker rm -f  4ea881615c3b

Insert image description here

2.3.3 web application

2.3.3.1 Running a web application

Run a Python Flask application in a docker container to run a web application

docker pull training/webapp
docker run -d -P training/webapp python app.py

Parameter Description:

  • -d: Let the container run in the background.

  • -P: Randomly map the network ports used inside the container to the host we use.

After startup, you can obtain the listening port through docker psordocker port 容器ID
Insert image description here

Insert image description here
127.0.0.1:端口号After making sure it is enabled, you can access the web services provided by docker on the web .

Insert image description here


-p: Add this parameter to provide fixed port access.

 docker run -d -p 5000:5000 training/webapp python app.py

Insert image description here


2.3.3.2 View web service logs

docker logs [ID或者名字] : View the standard output inside the container.
Insert image description here

  • -f: Let docker logs output the standard output inside the container just like using tail -f.

2.3.3.3 View the processes in the web service container

docker top 	容器ID/容器Name

2.3.3.4 Check the configuration information of the service in docker

docker inspect 容器ID : View the underlying information of Docker. It will return a JSON file recording the configuration and status information of the Docker container.

3 Learning Links

Link

Come on~ welcome a new day~ —swrici

Guess you like

Origin blog.csdn.net/Srwici/article/details/125603130