Explain how to install Docker and the operation command under CentOS 7

  • 2020-05-15 03:11:52
  • OfStack

Docker installation

There are installation guides for various environments on the official website, such as the CentOS, Ubuntu, and Debian series installations.

We'll focus on the installation above CentOS 7.x.

1. Check if docker has been installed


[root@localhost ~]# yum list installed | grep docker

docker.x86_64      2:1.12.6-55.gitc4618fb.el7.centos @extras 
docker-client.x86_64    2:1.12.6-55.gitc4618fb.el7.centos @extras 
docker-common.x86_64    2:1.12.6-55.gitc4618fb.el7.centos @extras 

2. The above means that it has been installed. Delete docker


[root@localhost ~]# yum remove  � y docker.x86_64

[root@localhost ~]# yum remove  � y docker-client.x86_64

[root@localhost ~]# yum remove  � y docker-common.x86_64

3. Install docker (-y means silent installation, and the installation process will not be inquired)

[root@localhost ~]# yum install -y docker

4. Start docker

[root@localhost ~]# systemctl start docker.service

5. Stop docker

[root@localhost ~]# systemctl stop docker

6. Check the current state of docker

[root@localhost ~]# systemctl status docker

Docker mirror

7. Pull the mirror image

7.1 pull from docker hub

https://hub.docker.com/

The following example downloads a server image of version 7 of Tomcat from the Docker Hub repository.

[root@localhost ~]# docker pull tomcat:7

This is the default public repository for docker, but the disadvantage is that domestic downloads can be slow.

7.2 pull from ustc (recommended)

The host machine edit file: vi etc/docker/daemon json

Please add to this profile (if you don't have it, please create one first) :


{

 "registry-mirrors":["https://docker.mirrors.ustc.edu.cn"]

}

Finally, you need to restart the docker service

[root@localhost ~]# systemctl restart docker

Then use the pull command to pull the image from ustc.

8. List the mirrors


docker images
[root@localhost ~]# docker images

9. Delete the image

docker rmi mirror

[root@localhost ~]# docker rmi tomcat:7

Note: the image file cannot be deleted when the container created by the image exists. You need to delete all containers that depend on the image before deleting the image.

10. Import and export images

Export image: docker save image > /root/xx.tar.gz

Import image: docker load < /root/xx.tar.gz

>

Docker container

11. Start the container

Start the container interactively: docker run-it --name container name mirror /bin/bash


[root@localhost ~]# docker run -it --name mytomcat tomcat:7 /bin/bash
[root@28cc2a4f8c90 /]#

Out of the container


[root@28cc2a4f8c90 /]# exit
exit
[root@localhost ~]#

Start the container as a daemon: docker run-d --name container name mirror


[root@localhost ~]# docker run -d --name mytomcat tomcat:7
f9b59aed3c7de509d6c9d9cc14c3d7774fc50f250b70661a4354df948588393b
[root@localhost ~]#

12. Stop the container

docker stop container name or container ID

[root@localhost ~]# docker stop mytomcat

Restart the container

docker start container name or container ID

[root@localhost ~]# docker start mytomcat

14. Delete the container

Deletes the specified container: docker rm container name or container ID

Delete all containers: docker rm 'docker ps-a-q '

15. View the container

docker ps: view the running container

[root@localhost ~]# docker ps

docker ps, a: view containers that have been run historically

[root@localhost ~]# docker ps �a

Docker application

16. Set up Tomcat service


docker run -d --name mytomcat -p 8888:8080  The mirror 

[root@localhost ~]# docker run -d --name mytomcat -p 8888:8080 tomcat:7
020c28055968b21ccb584d8b60b51b38f44678930b3bc2456eafb0fe40efcd78
[root@localhost ~]#

Deploy the Web application

1. Upload the war package to the host machine

2. Upload the war package from the host machine to the webapps directory of tomcat in the container via the docker cp command.

Enter the container as an interactive interface


[root@localhost ~]# docker exec -it mytomcat /bin/bash
root@020c28055968:/usr/local/tomcat# ls
LICENSE RELEASE-NOTES bin include logs   temp work
NOTICE RUNNING.txt conf lib  native-jni-lib webapps
root@020c28055968:/usr/local/tomcat# exit
exit
[root@localhost ~]# docker cp DockerDemo.war mytomcat:/usr/local/tomcat/webapps
[root@localhost ~]#

3. tomcat will be automatically hot-deployed, and you can directly access the application path of Web.

conclusion


Related articles: