Docker Tutorial
Docker Installation
How to use Docker
Docker Instance
Docker Container Lifecycle Command
Docker Container Operation Command
Docker Container rootfs Command
Docker Mirror Repository Command
Docker Local Image Management Command
Docker info|version Command
Docker allows you to run applications inside containers. Use the docker run command to run an application inside a container.
output Hello world
iditect@iditect:~$ docker run ubuntu:15.10 /bin/echo "Hello world" Hello world
Analysis of each parameter:
docker: Docker binary executable.
run: Combined with the previous docker to run a container.
ubuntu:15.10 specifies the image to 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 repository Docker Hub.
/bin/echo "Hello world": command executed in the started container
The complete meaning of the above command can be explained as: Docker creates a new container with the ubuntu15.10 image, then executes bin/echo "Hello world" in the container, and then outputs the result.
We let the containers run by docker achieve the ability to "talk" through the two parameters of docker -i -t :
iditect@iditect:~$ docker run -i -t ubuntu:15.10 /bin/bash root@0123ce188bd8:/#
Analysis of each parameter:
-t: Specify a pseudo-terminal or terminal inside the new container.
-i: Allows you to interact with standard input (STDIN) inside the container.
Note the second line root@0123ce188bd8:/#
, at this point we have entered a container of the ubuntu15.10 system
We try to run the commands cat /proc/version and ls in the container to view the version information of the current system and the list of files in the current directory respectively
root@0123ce188bd8:/# cat /proc/version Linux version 4.4.0-151-generic (buildd@lgw01-amd64-043) (gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.10) ) #178-Ubuntu SMP Tue Jun 11 08:30: 22 UTC 2019 root@0123ce188bd8:/# ls bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@0123ce188bd8:/#
We can exit the container by running the exit command or by using CTRL+D.
root@0123ce188bd8:/# exit exit root@iditect:~#
Note that the third line root@iditect:~#
indicates that we have exited the current container and returned to the current host.
Create a container running as a process using the following command
iditect@iditect:~$ docker run -d ubuntu:15.10 /bin/sh -c "while true; do echo hello world; sleep 1; done" 2b1b7a428627c51ab8810d541d759f072b4fc75487eed05812646b8534a2fe63
In the output we don't see the expected "hello world", but a long string of characters
2b1b7a428627c51ab8810d541d759f072b4fc75487eed05812646b8534a2fe63
This long string is called the container ID, which is unique to each container. We can use the container ID to see what happened to the corresponding container.
First, we need to confirm that the container is running, which can be viewed through docker ps :
iditect@iditect:~$ docker ps CONTAINER ID IMAGE COMMAND... 5917eac21c36 ubuntu:15.10 "/bin/sh -c 'while t..." ...
The output details are introduced:
CONTAINER ID: container ID.
IMAGE: The image used.
COMMAND: The command to run when the container is started.
CREATED: The creation time of the container.
STATUS: Container status.
There are 7 states:
PORTS: The port information of the container and the connection type used (tcp\udp).
NAMES: Automatically assigned container names.
Use the docker logs
command view the standard output inside the container:
iditect@iditect:~$ docker logs 2b1b7a428627
iditect@iditect:~$ docker logs amazing_cori
We use the docker stop command to stop the container.
By docker ps
looking , the container has stopped working:
iditect@iditect:~$ docker ps
You can see that the container is no longer there.
It can also be stopped with the following command:
iditect@iditect:~$ docker stop amazing_cori