Docker Simple Getting Started Tutorial

  • 2021-08-31 09:45:14
  • OfStack

Foreword:

Docker is an open source application container engine that allows developers to package their applications and dependencies into a portable image and distribute them to any popular Linux or Windows machine. In recent years, Docker has developed in full swing in China, especially in Internet companies. The use of Docker is 10 points common, which greatly improves the maintenance efficiency of applications and reduces the cost of cloud computing application development. This article mainly takes you to get started with Docker, and introduces the installation and simple use of Docker.

1. Install Docker

To learn Docker, we must first install Docker, which is divided into CE (Community Edition: Community Edition) and EE (Enterprise Edition: Enterprise Edition) from version 17.03. Let's take CentOS system as an example to introduce the installation of Docker Community Edition:

Uninstall the old version

The older version of Docker, called docker or docker-engine, is uninstalled using the following command:


$ sudo yum remove docker \
         docker-client \
         docker-client-latest \
         docker-common \
         docker-latest \
         docker-latest-logrotate \
         docker-logrotate \
         docker-engine

Install dependency packages


# Configure yum Source 
sudo yum-config-manager \
--add-repo \
https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-ce.repo

# Install dependency packages 
sudo yum install -y yum-utils \
device-mapper-persistent-data \
lvm2

Install the latest version of Docker CE


sudo yum-config-manager --enable docker-ce-edge
sudo yum makecache fast
sudo yum install docker-ce

Start Docker CE


sudo systemctl enable docker
sudo systemctl start docker

Establish an docker user group


sudo groupadd docker
sudo usermod -aG docker $USER

Run the hello-world test


$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
ca4f61b1923c: Pull complete
Digest: sha256:be0cd392e45be79ffeffa6b05338b98ebb16c87b255f48e297ec7f98e123905c
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/

For more examples and ideas, visit:
https://docs.docker.com/get-started/

So far, we have successfully installed Docker. Similarly, it is easy to install Docker in Windows system and macOS system. Download Docker Desktop installation package to install and use it. For details, please refer to the following official documents:

https://docs.docker.com/docker-for-windows/install/
https://docs.docker.com/docker-for-mac/install/

2. Introduction to Common Commands

To learn Docker, we must first know its overall architecture. Here, we briefly introduce three basic concepts in Docker:

Mirror (Image): The Docker mirror (Image) is equivalent to an root file system. For example, the official image ubuntu: 16.04 contains a complete root file system of the smallest system of Ubuntu 16.04. Container (Container): The relationship between the image (Image) and the container (Container), like classes and instances 1 in object-oriented programming, the image is a static definition, and the container is an entity at the time of the image run. Containers can be created, started, stopped, deleted, paused, and so on. Warehouse (Repository): Warehouse can look at a code control center, which is used to store mirrors.

Mirroring related commands:

1) Mirror lookup
docker search mirror name (for example, redis)

2) Download of mirrors
docker pull Mirror Name

3) View the local mirror list
docker images

4) Delete the image
docker rmi Mirror ID

Container-related commands:

1) Run the image as a container
docker run--Name of name container--Name of d image
-d means detached, which means that after executing this command, the console will not be blocked and can continue to enter commands.
2) Gets a list of running containers
docker ps
3) Gets all container lists that contain opinion exits
docker ps -a
4) Stop and start the container
docker start/stop Container Name/id

5) Port mapping
The port of the software running in the container needs to be mapped to the port of the host, otherwise the host in the LAN cannot be accessed.
docker run -d -p 6378:6379 --name myRedis redis
-p: Port 6379 in the container maps to port 6378 on the host
6) Delete container
docker rm id
7) View the current container log
docker logs name/id
8) Login container
docker exec-it Container Name bash
-i: Ensure our input is valid
-t: 1 pseudo terminal will be allocated
Login accesses the current container. After logging in, you can perform regular Linux command operations in the container, and you can also use exit command to exit the login.

Summary:

This article briefly introduces the installation of Docker and commonly used commands, as an introductory article, I hope to help you. In fact, as a basic tool, Docker is recommended for everyone to learn 1. For example, you can start an instance of MySQL in seconds, and new versions can also be tested by running Docker. The next article intends to write how to run and configure MySQL in Docker, look forward to it!

The above is the Docker simple introduction to use the detailed content of the tutorial, more about Docker introduction and use of information please pay attention to other related articles on this site!


Related articles: