Docker quick start and common instruction set

Docker quick start and common instruction set

It is recommended to read all three docker articles of Bubble slam [Bubble Docker Paradise] Hand-
in-hand to walk you into the Docker world
[Bubble Docker Paradise] Dockerfile simple tutorial & LARVIO mirror
[Bubble Docker Paradise] Bubble Docker basics Mirror set release

Docker quick installation configuration

Reference blog:
Docker study notes: Docker installation and basic usage on Ubuntu 16.04

1. Install docker

ubuntu mounting step Tsinghua mirror source Installation

First install the dependencies:

sudo apt-get install apt-transport-https ca-certificates curl gnupg2 software-properties-common

Trust Docker's GPG public key:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

For amd64 architecture computers, add software warehouse:

sudo add-apt-repository \
   "deb [arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

Final install

sudo apt-get update
sudo apt-get install docker-ce

2. DockerHub mirror configuration

Write in the /etc/docker/daemon.json (create new if no) file

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

Restart service

sudo systemctl daemon-reload
sudo systemctl restart docker

3. Verify the installation

 sudo docker -v

Check the version of docker-ce, if the output is similar to the following text, it means that the software has been installed successfully.

Docker version 19.03.13, build 4484c46d9d

4. Configure to run docker commands without sudo

Join the docker user group

sudo groupadd docker
sudo gpasswd -a ${USER} docker
newgrp docker

Check whether the exemption of sudo is successful.
Enter the following command if no error is reported.

docker ps

5. Start Docker

Check whether the docker service is started:

sudo systemctl status docker

If Docker is not started, start Docker:

sudo systemctl start docke

6. Three pull docker image demo

6.1 docker 拉取 hello-world image

Search for hello-world image

docker search hello-world

Insert picture description here
The table contains five columns with the following meanings:

  • NAME: The name of the mirror warehouse.

  • DESCRIPTION: The description of the mirror warehouse.

  • STARS: The number of mirrored collections indicates the popularity of the mirrored warehouse, similar to GitHub's Stars.

  • OFFICAL: Indicates whether it is an official warehouse. The images marked as [OK] in this column are created and maintained by the official project team of the software. From the results, it can be seen that the mirror repository of java is an official repository, while other repositories are not official mirror repositories.

  • AUTOMATED: Indicates whether to build the mirror warehouse automatically.

Download image from dockerhub

docker pull hello-world

Insert picture description here
View the downloaded image

docker images

Insert picture description here

Build a container and run hello-world

docker run hello-world

Insert picture description here

6.2 docker pulls Nginx

Reference blog: Docker installs Nginx to
find the latest version

docker search nginx

Get the latest version of Nginx mirror

 docker pull nginx:latest

Insert picture description here
Run the container

docker run --name nginx-test -p 8080:80 -d nginx

Parameter description:
--name nginx-test: container name.
-p 8080:80: port mapping, mapping the local 8080 port to the 80 port inside the container.
-d nginx: Set the container to always run in the background.

Finally, we can directly access the nginx service on port 8080 through the browser:

noVnc visualization (browser visualization)

Type in the browser to see the results of nginx

http://localhost:8080/

Insert picture description here

6.3 Docker practice example: build a Docker desktop system based on VNCServer + noVNC

Reference blog:
Add link description
[Bubble Docker Paradise] Bubble Docker basic image collection released

Download the specified mirror images

docker pull dorowu/ubuntu-desktop-lxde-vnc

Enter the run docker command

docker run -it -p 5900:5900 dorowu/ubuntu-desktop-lxde-vnc

Insert picture description here

noVNC visualization

docker run -it --rm -p 8080:80 dorowu/ubuntu-desktop-lxde-vnc

Browser http://127.0.0.1:8080/Insert picture description here

VNC visualization

vnc download address
Insert picture description here
VNC input: 5900
Insert picture description here
to see the interface
Insert picture description here

Docker common instruction set

Reference blog:
Docker study notes: Docker installation and basic usage on Ubuntu 16.04

Search mirror

sudo docker search java

Download mirror

sudo docker pull java

List local mirrors

docker images

Insert picture description here

  • REPOSITORY: The name of the warehouse to which the image belongs

  • TAG: Mirror tag. The default is latest, which means the latest.

  • IMAGE ID: Mirror ID, which means the unique identification of the mirror

  • CREATED: Mirror creation time

  • SIZE: mirror size

Delete the local mirror:

Delete the mirror with the specified name

sudo docker rmi hello-world

Delete all mirrors

sudo docker rmi -f $(docker images)

-f parameter means forced deletion

Common commands for Docker containers

Create and start the container

Use the docker run command to create and start a container.

This command is the most commonly used command, it has very frustrating options, some commonly used options are listed below.

  • d option: means running in the background

  • P option: random port mapping

  • p option: specify port mapping, there are four formats

    • ip:hostPort:containerPort

    • ip::containerPort

    • hostPort:containerPort

    • containerPort

  • network option: specify the network mode, this option has the following optional parameters:

    • --Network=bridge: The default option, which means to connect to the default bridge.

    • --Network=host: The container uses the host's network.

    • --Network=container:NAME_or_ID: Tell Docker to let the newly created container use the network configuration of the existing container.

    • --Network=none: Do not configure the container's network, users can customize the network configuration.

Example 1:

sudo docker run java /bin/echo 'Hello World'

In this way, the terminal will print the words Hello World, just like executing /bin/echo'Hello World' directly locally.

Example 2:

sudo docker run -d -p 91:80 nginx

This will start an Nginx container. In this example, two parameters are added to docker run, with the following meanings:

-d # 后台运行
-p 宿主机端口:容器端口 # 开放容器端口到宿主机端口

Enter the following address in the browser to access Nginx

localhost:91
List local containers
docker ps

If you need to list all containers (including stopped containers), you can use the -a parameter.

docker ps -a

The table contains 7 columns with the following meanings:

  • CONTAINER_ID: Represents the container ID

  • IMAGE: indicates the name of the image

  • COMMAND: indicates the command to run when the container is started

  • CREATED: indicates when the container was created

  • STATUS: indicates the running status of the container. UP means running, Exited means stopped.

  • PORTS: indicates the external port number of the container

  • NAMES: indicates the name of the container, which is automatically generated by Docker by default, or you can use the -name option of the docker run command to specify it yourself.

Stop container

Use the docker stop command to stop the container, for example:

sudo docker stop 2730ed88f8e5

2730ed88f8e5 is the container ID. Of course, you can also use the docker stop container name to stop the specified container.

Start a stopped container

Use the docker run command to create and start a container. For stopped containers, you can use the docker start command to start. E.g:

sudo docker start 2730ed88f8e5
Restart container

You can use the docker restart command to restart the container. This command actually executes the docker stop command first, and then executes the docker start command.

Enter the container

In certain scenarios, you may need to enter the running container.

Use the docker attach command to enter the container. E.g:

sudo docker attach 2730ed88f8e5
Delete container

Delete the specified container

sudo docker rm 2730ed88f8e5

Delete all containers

 sudo docker rm -f $(docker ps -a -q)

Guess you like

Origin blog.csdn.net/weixin_41281151/article/details/109966209