The CVM bought on Tencent Cloud has expired, and it will cost money to buy it again, so use Docker. I haven't touched Docker for more than a year, and I forgot a lot of operations, so I just took this opportunity to record it. This combing installs Docker under Mac and uses Docker.
relation
There are several important concepts in Docker:
Image: image, corresponding to the ISO of the operating system, this is a static concept
Container: Containers can be created through mirroring, corresponding to the installed operating system. This is a dynamic concept. We can log in to the container at any time, that is, log in to this operating system
Install Docker
It is very convenient to install Docker on Mac. Download the software from https://docs.docker.com/desktop/mac/install/ and install it. Remember to check whether the computer is an Intel chip or an Apple chip.
Execute the command to check the installation status:
➜ ~ docker version
Client:
Cloud integration: v1.0.23
Version: 20.10.14
API version: 1.41
Go version: go1.16.15
Git commit: a224086
Built: Thu Mar 24 01:49:20 2022
OS/Arch: darwin/amd64
Context: default
Experimental: true
download mirror
You can go to https://hub.docker.com/ to choose the image you need. The mirror image made three years ago is still there, just use this.
Download mirror:
➜ ~ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
shidawuhen/php-56-centos7 latest 92ed8b3a7cb4 3 years ago 617MB
Of course, we can also find the image we want in the hub and download it using commands, such as:
docker pull centos:centos7
container operations
create container
Once you have the image, you can create a container with the following command:
# centos容器
docker run -itd \
-p 8080:8080 \
-p 8081:8081 \
-p 8082:8082 \
--privileged=true \
--name centos7 \
centos:centos7 \
/bin/bash
# mysql容器,可设置mysql密码
docker run -p 8083:8083 --name mysql8 -e MYSQL_ROOT_PASSWORD=1234Abcd -d mysql:8.0
-
p: indicates the port mapping of the host machine and the container machine
-
name: Indicates the name given to the container
-
centos:centos7: Indicates which version of which mirror to use
view container
Use the docker ps command to view a list of running containers, and use docker ps -a to view a list of all containers
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fe13b347d938 centos:centos7 "/bin/bash" 4 seconds ago Up 4 seconds 0.0.0.0:8080-8082->8080-8082/tcp centos7
into the container
You can use the docker exec command to enter the container, it is followed by the container name, /bin/bash means to use the bash command in the container:
docker exec -it centos7 /bin/bash
docker exec -it mysql8 /bin/bash
Entering Centos7 for the first time, you can change the root password
[root@fe13b347d938 /]# passwd
Create a user user
[root@fe13b347d938 /]# useradd user
[root@fe13b347d938 /]# passwd user
add sudoer
yum install sudo
visudo
# 文件末尾添加 user ALL=(ALL) ALL
close container
You can use the command to close the container, or you can directly use the graphical interface to operate
docker stop centos7
Start the container
After the container is closed, it needs to be started to enter again
docker start centos7
Restart the container
Containers can be restarted
docker restart centos7
Docker File
The above operations are all manual operations. It is enough to manage a single container, but if you need to manage multiple images and containers, you can use Docker File and Docker Compose.
Dockerfile is a text file used to build images, and the text content contains instructions and instructions for building images one by one.
Compose is a tool for defining and running multi-container Docker applications. With Compose, you can use YML files to configure all the services your application needs. Then, with a single command, all services can be created and started from the YML file configuration.
Give a simple example to describe how to use it.
Create Docker File
Create two Dockerfiles and put them in the t1 and t2 directories, the content is:
FROM centos:centos7
RUN echo 't1' > /home/1.txt
CMD ["/bin/bash"]
FROM centos:centos7
RUN echo 't2' > /home/1.txt
CMD ["/bin/bash"]
Create Compose
Create the corresponding docker-compose.yml, the file content is:
version: '2.4'
services:
centos-t1:
container_name: centos-t1
privileged: true
build: ./t1
tty: true
ports:
- "5000:5000"
centos-t2:
container_name: centos-t2
privileged: true
build: ./t2
tty: true
ports:
- "5001:5001"
build image
docker-compose -f docker-compose.yml up -d
After the execution is complete, you can see that there are two new images
➜ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
macdocker_centos-t2 latest 0e1d76e166fc 5 seconds ago 204MB
macdocker_centos-t1 latest 85bd53e29399 35 seconds ago 204MB
You can also see two more containers
➜ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
905c975e225d macdocker_centos-t2 "/bin/bash" 14 seconds ago Up 13 seconds 0.0.0.0:5001->5001/tcp centos-t2
b5be8096b5e6 macdocker_centos-t1 "/bin/bash" 14 seconds ago Up 13 seconds 0.0.0.0:5000->5000/tcp centos-t1
Turn on/off/restart
We can use the following commands to start, shut down, and restart Compose containers
docker-compose -f docker-compose.yml start
docker-compose -f docker-compose.yml stop
docker-compose -f docker-compose.yml restart
Compose manages all containers in the yml file in a unified way, and this difference can also be seen in the UI interface
Summarize
Docker greatly facilitates operation and maintenance students, and is also of great help to R&D students. You can easily build an operating system that meets your own requirements, upload it as a mirror image, and reuse it in various locations in the future without repeated operations.
At first, I planned to use my own image, but later found that there was a D-Bus problem that could not be solved. Finally, I used docker pull mysql:8.0 to obtain the existing image. If you have needs, you can first go to GitHub to find some official images, which can save a lot of time.
material
-
MacOS Docker installation
-
Docker container installation
-
解决:Failed to get D-Bus connection: Operation not permitted
-
system
-
Docker run command
-
Docker Dockerfile
at last
If you like my article, you can follow my official account (Programmer Malatang)
My personal blog is: https://shidawuhen.github.io/
Review of previous articles: