Docker Hello World
Docker allows you to run an application within a container, using the docker run command to run an application within a container.
Output Hello world
w3cschool@w3cschool:~$ docker run ubuntu:15.10 /bin/echo "Hello world" Hello world
Individual parameter parsing:
-
docker: Docker's binary execution file.
-
run: Combine with the previous docker to run a container.
-
ubuntu:15.10 specifies the mirror to run, Docker first looks for the existence of the mirror from the local host, and if it does not exist, Docker downloads the public image from the mirror repository Docker Hub.
-
/bin/echo "Hello world": Commands executed in the startup container
The full meaning of the above command can be interpreted as: Docker creates a new container in the ubuntu15.10 image, executes bin/echo "Hello world" in the container, and then outputs the results.
Run interactive containers
We use docker's two parameters - i -t, to make the container that docker runs implement the "dialogue" capability
w3cschool@w3cschool:~$ docker run -i -t ubuntu:15.10 /bin/bash root@dc0050c79503:/#
Individual parameter parsing:
-
-t: Specify a pseudo-terminal or terminal in the new container.
-
-i: Allows you to interact with standard input (STDIN) in the container.
At this point we have entered a container for the ubuntu15.10 system
Let's try to run the command cat/proc/version and ls in the container to see the version information of the current system and the list of files in the current directory, respectively
We can exit the container by running the exit command or by using CTRL-D.
Start container (background mode)
Use the following commands to create a container that runs as a process
w3cschool@w3cschool:~$ 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 desired "hello world" but a long string of characters
2b1b7a428627c51ab8810d541d759f072b4fc75487eed05812646b8534a2fe63
This long string is called a container ID and is unique to each container, and we can see what happened to the corresponding container through the container ID.
First, we need to make sure that the container is running and can be viewed through docker ps
w3cschool@w3cschool:~$ docker ps
CONTAINER ID: Container ID
NAMES: The name of the container that is automatically assigned
Use the docker logs command inside the container to view the standard output within the container
w3cschool@w3cschool:~$ docker logs 2b1b7a428627
w3cschool@w3cschool:~$ docker logs amazing_cori
Stop the container
We use the docker stop command to stop the container:
Viewed by docker ps, the container has stopped working:
w3cschool@w3cschool:~$ docker ps
You can also stop with the following command:
w3cschool@w3cschool:~$ docker stop amazing_cori