Table of contents
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.
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!"
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 run
Can 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"
We can docker ps
view this by listing all current mirrors.
docker ps
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/容器Names
to view the standard output of the container.
docker logs 92927a25d292
docker logs admiring_borg
ps: Container ID and container Names can be found from
docker ps
inside
2.2.4 Stop the container running in the background
Use commanddocker stop 容器ID/容器Names
docker stop admiring_borg
2.3 Container usage
2.3.1 Client command view
- View all commands used
docker
- Use the command
docker command --help
to view the specific content, for example:
docker stats --help
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.
2.3.2.2 Using containers
docker run -it ubuntu /bin/bash
Here -it
is -i -t
the 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.
Exit container command:exit
2.3.2.3 Stopped container and restarted it
View stopped containers
docker ps -a
Start a stopped container
docker start 容器ID
Running containers in the background
When we need the container to run in the background instead of running directly, just add parameters. -d
This
way we will not enter directly, but we can use docker exec
commands 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 exec
Command usage
docker exec -it 4ea881615c3b /bin/bash
After exiting, the container still exists
docker attach
Command usage
2.3.2.4 Import and export containers
export container
docker export
docker export 容器ID > 导出文件名.导出文件类型
例如:
docker export 1e560fca3906 > ubuntu.tar
Import container
docker import
2.3.2.5 Delete container
docker rm -f 容器ID
It needs to be in stopped state when deleting.
docker rm -f 4ea881615c3b
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 ps
ordocker port 容器ID
127.0.0.1:端口号
After making sure it is enabled, you can access the web services provided by docker on the web .
-p
: Add this parameter to provide fixed port access.
docker run -d -p 5000:5000 training/webapp python app.py
2.3.3.2 View web service logs
docker logs [ID或者名字]
: View the standard output inside the container.
-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
Come on~ welcome a new day~ —swrici