Getting started with docker command

Posted by MatrixGL on Tue, 14 Apr 2020 19:33:02 +0200

date: 2018-2-5
reference: http://www.runoob.com/docker/

Container ID and container name can be mixed without distinction
At least I haven't found a place where I can't mix

1. Use the docker run command to run an application within the container

docker run ubuntu:15.10 /bin/echo "Hello world"
## If you do not specify a version label for the image, for example, if you only use ubuntu,docker will use ubuntu:latest image by default

2. Run interactive containers

docker run -i -t ubuntu:15.10 /bin/bash
## -t: Specify a pseudo terminal or terminal in the new container
## -i: Allows you to interact with stdin the container

3. Start container in background mode

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

4. You can view the running containers through docker ps

docker ps

5. Use the docker logs command in the container to view the standard output in the container

docker logs b5c8dd8f29b9

6. Stop the container

docker stop b5c8dd8f29b9

7. Get a new image

docker pull training/webapp

8. Running a web application

docker run -d -P training/webapp python app.py
## -d: Let the container run in the background
## -P: Map the network port used inside the container to the host we use

9. View WEB application container

docker ps
## Where PORT information: 0.0.0.0:32768 - > 5000 / TCP
## Indicates that Docker has opened 5000 port (default Python Flask port) and mapped to host port 32768

## We can also specify the - p identity to bind the specified port
docker run -d -p 5000:5000 training/webapp python app.py

10. Shortcut to view network port

docker port 7a38a1ad55c6

11. View WEB application log

docker logs -f 7a38a1ad55c6
## -f: Let dokcer logs output the standard output inside the container as if using tail -f

12. View the process of WEB application container

docker top 7a38a1ad55c6

13. Check WEB application

docker inspect determined_swanson
## Use Docker inspection to view the underlying information of Docker. It will return a JSON file to record the configuration and status information of Docker container

14. Stop WEB application container

docker stop determined_swanson 
## Same as 6. Stop container

15. Start and restart WEB application container

docker start determined_swanson
## For containers that have been stopped, we can use the command docker start to start them
docker restart determined_swanson
## We can use the docker restart command to restart the running container

16. Remove the WEB application container

docker rm determined_swanson
## When deleting a container, the container must be in the stopped state, otherwise an error will be reported

17. List images

docker images

18. Find image

docker search httpd

19. Update image

## Before updating the image, we need to use the image to create a container
docker run -t -i ubuntu:15.10 /bin/bash
## Use apt get update command to update in the running container
## After completing the operation, enter the exit command to exit the container
## At this time, the container with the ID of e218edb10161 is changed according to our requirements. We can submit the container copy by commanding docker commit
docker commit -m="has update" -a="ganlu" e218edb10161 runoob/ubuntu:v2
## -m: Description information submitted
## -a: Specify mirror author
## runoob/ubuntu:v2: Specifies the target image name to create

20. Build an image

## We use the command docker build to create a new image from scratch. To do this, we need to create a Dockerfile file, which contains a set of instructions to tell Docker how to build our image

## Dockerfile

## FROM    centos:6.7
## MAINTAINER      Fisher "fisher@sudops.com"

## RUN     /bin/echo 'root:123456' |chpasswd
## RUN     useradd runoob
## RUN     /bin/echo 'runoob:123456' |chpasswd
## RUN     /bin/echo -e "LANG=\"en_US.UTF-8\"" >/etc/default/local
## EXPOSE  22
## EXPOSE  80
## CMD     /usr/sbin/sshd -D

## Each instruction creates a new layer on the image, and the prefix of each instruction must be uppercase
## The first FROM specifies which image source to use
## The RUN instruction tells docker to execute the command and install something in the image
## Then, we use the Dockerfile file to build an image through the docker build command

docker build -t runoob/centos:6.7 .
## -t: Specify the name of the target image to create
## .: the directory of the Dockerfile. You can specify the absolute path of the Dockerfile

## Here is a brief introduction to the use of Dockerfile, not in-depth

21. Set image label

## We can use the docker tag command to add a new label for the image
docker tag 860c279d2fec runoob/centos:dev
## Using the docker images command, you can see that the image with ID 860c279d2fec has one more label
## REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
## runoob/centos       6.7                 860c279d2fec        5 hours ago         190.6 MB
## runoob/centos       dev                 860c279d2fec        5 hours ago         190.6 MB

22. Network port mapping

docker run -d -P training/webapp python app.py
docker run -d -p 5000:5000 training/webapp python app.py
## Specific instructions are mentioned in 8.9

docker run -d -p 127.0.0.1:5001:5000 training/webapp python app.py
## We can specify the network address of the container binding, such as binding 127.0.0.1

docker run -d -p 127.0.0.1:5000:5000/udp training/webapp python app.py
## If you want to bind a udp port, you can add / udp after the port

23. Container naming

docker run -d -P --name runoob training/webapp python app.py
## We can use the -- name identifier to name the container

Topics: Docker Ubuntu Python CentOS