search
HomeSystem TutorialLINUXGet started with Docker in five minutes

Get started with Docker in five minutes

Jan 02, 2024 pm 02:29 PM
linuxlinux tutorialRed Hatlinux systemlinux commandlinux certificationred hat linuxlinux video

What is Docker?

Docker is an open source container engine, and a container is actually a virtualized independent environment. Therefore, developers can package applications into such a docker container and then publish it to any machine that can run the docker container. , realizing one-time packaging and multiple deployments, solving deployment problems caused by environmental problems.

A concept corresponding to the container is the image. The image can be regarded as the image of our daily installation system, which is a running environment. Of course, I prefer to compare an image to a class in our object-oriented programming, and a container is an instance of a class, so many containers can be created based on one image. Each container is specific. We can make changes to the container and then package the container into a new image, so that new containers can be created based on the modified image in the future. The container itself can be simply understood as a virtual independent running environment. What we have to do is to package our application in this environment for easy deployment again.
Get started with Docker in five minutes

Install Docker

Gentoo

# emerge --ask docker

CentOS

# yum install docker-ce

Ubuntu

# apt-get install docker-ce

Arch

# pacman -S docker
Start the docker daemon
# systemctl start docker
docker command introduction
# docker --help
管理命令:
  container   管理容器
  image       管理镜像
  network     管理网络
命令:
  attach      介入到一个正在运行的容器
  build       根据 Dockerfile 构建一个镜像
  commit      根据容器的更改创建一个新的镜像
  cp          在本地文件系统与容器中复制 文件/文件夹
  create      创建一个新容器
  exec        在容器中执行一条命令
  images      列出镜像
  kill        杀死一个或多个正在运行的容器    
  logs        取得容器的日志
  pause       暂停一个或多个容器的所有进程
  ps          列出所有容器
  pull        拉取一个镜像或仓库到 registry
  push        推送一个镜像或仓库到 registry
  rename      重命名一个容器
  restart     重新启动一个或多个容器
  rm          删除一个或多个容器
  rmi         删除一个或多个镜像
  run         在一个新的容器中执行一条命令
  search      在 Docker Hub 中搜索镜像
  start       启动一个或多个已经停止运行的容器
  stats       显示一个容器的实时资源占用
  stop        停止一个或多个正在运行的容器
  tag         为镜像创建一个新的标签
  top         显示一个容器内的所有进程
  unpause     恢复一个或多个容器内所有被暂停的进程

There are more rich options in subcommands, you can use docker COMMAND --help to view. For example:

# docker run --help

Docker usage in practice

Next, I will use docker to deploy an Nginx server as an example. To create a container, we must first have an image used to create the container. Find nginx related images in docker hub.

# docker search nginx
NAME                                                   DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
nginx                                                  Official build of Nginx.                        6959      [OK]
jwilder/nginx-proxy                                    Automated Nginx reverse proxy for docker c...   1134                 [OK]
richarvey/nginx-php-fpm                                Container running Nginx + PHP-FPM capable ...   452                  [OK]
...
...

Pull the official image. The unofficial image above is the image made by users according to their own needs for everyone's convenience.

# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
afeb2bfd31c0: Pull complete
7ff5d10493db: Downloading [===============>                                   ] 6.651 MB/21.87 MB
d2562f1ae1d0: Download complete

By the way, docker images are stored in layers, so the nginx image above has 3 layers. In addition, every time a new image is made based on a new container, one layer will be added to the image.
Next, use this image to start a new container directly:

# docker run --name my-nginx -d -p 8080:80 nginx

At this time, enter: http://localhost:8080/ in the browser to access nginx. Parameter explanation:

  • --name Give the container a name
  • -p parameter syntax is -p host port:container port; -p 8080:80 binds port 8080 on the host to port 80 on the container, so when accessing port 8080 on the host, it actually accesses the nginx container Port 80
  • -d Run container in background

Then let’s look at a more complex example:

# docker run --name my-nginx \ 
    -v /host/path/nginx.conf:/etc/nginx/nginx.conf:ro \
    -v /some/html:/usr/share/nginx/html:ro \
    -p 8080:80 \
    -d nginx

This example has an additional parameter -v. The function of this parameter is to mount local files or folders into the container. The last ro or rw controls whether the mount is writable.

  • -v parameter syntax is -v host dir:container dir[:ro|rw]

The above command mounts the nginx.conf configuration file in the local file to the container, and also mounts the static page to be displayed to the container. Note: After the container executes a command, when the command ends, the container will also end.

Next let’s look at other docker commands.

View all running containers:

# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
1fe91b5a4cd4        nginx               "nginx -g 'daemon ..."   39 minutes ago      Up 39 minutes       0.0.0.0:8080->80/tcp   my-nginx
# docker ps -a ### 列出包括未运行的容器

View container running log:

# docker logs my-nginx
172.17.0.1 - - [05/Oct/2017:07:31:11 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0" "-"
2017/10/05 07:31:11 [error] 7#7: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "localhost:8080"

Restart container:

# docker restart my-nginx
my-nginx

Stop running a container:

# docker stop my-nginx
my-nginx
# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

Start an existing container:

# docker start my-nginx
my-nginx
# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
1fe91b5a4cd4        nginx               "nginx -g 'daemon ..."   50 minutes ago      Up 3 seconds        0.0.0.0:8080->80/tcp   my-nginx

Kill a running container directly:

# docker kill my-nginx

Rename container:

# docker rename my-nginx new-nginx

Delete container:

# docker rm new-nginx

View all images on this machine:

# docker imags

Create an image using an existing container:

# docker commit -m "nothing changed" my-nginx my-nginx-image
sha256:f02483cdb842a0dc1730fe2c653603fa3271e71c31dbb442caccd7ad64860350
# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
my-nginx-image      latest              f02483cdb842        6 seconds ago       108 MB
nginx               latest              da5939581ac8        3 weeks ago         108 MB

Create a container using the image we created ourselves:

# docker run --name test -d -p 8081:80 my-nginx-image
12ba86eabdef0121d875bdb547f1101d4eb54ff7cb36e20214fb1388659af83d
# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
12ba86eabdef        my-nginx-image      "nginx -g 'daemon ..."   7 seconds ago       Up 5 seconds        0.0.0.0:8081->80/tcp   test
299f4ede9e79        nginx               "nginx -g 'daemon ..."   11 minutes ago      Up 11 minutes       0.0.0.0:8080->80/tcp   my-nginx

If the startup command of the container we use is a shell, then we can use the attach command to intervene in the container, just like we use ssh to connect to a server.

# docker attach some-container

Start an interactive shell directly when creating a container, and automatically delete the container after exiting:

# docker run -it --rm  centos
Summarize

This article introduces the simple usage of docker, and also gives a practical example to demonstrate the various operations of the docker command. As a quick-start article, this article is relatively simple. Many conceptual things require reading other information to understand, but after reading this article, you should have the basic ability to use docker. In addition, subsequent articles should write about Dockerfile and docker-compose.

The above is the detailed content of Get started with Docker in five minutes. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:Linux就该这么学. If there is any infringement, please contact admin@php.cn delete
Explain the architectural differences between Linux and Windows.Explain the architectural differences between Linux and Windows.May 06, 2025 am 12:01 AM

The main differences in architecture between Linux and Windows include: 1) Design philosophy and kernel structure: Linux uses a modular kernel, Windows uses a single kernel; 2) File system: Linux supports multiple file systems, Windows mainly uses NTFS; 3) Security: Linux is known for its permission management and open source features. Windows has a unique security mechanism but lags in repair; 4) Usage experience: Linux command line operation is more efficient, and Windows graphical interface is more intuitive.

What are some common security threats targeting Linux versus Windows?What are some common security threats targeting Linux versus Windows?May 05, 2025 am 12:03 AM

Linux and Windows systems face different security threats. Common Linux threats include Rootkit, DDoS attacks, exploits, and permission escalation; common Windows threats include malware, ransomware, phishing attacks, and zero-day attacks.

How does process management differ between Linux and Windows?How does process management differ between Linux and Windows?May 04, 2025 am 12:04 AM

The main difference between Linux and Windows in process management lies in the implementation and concept of tools and APIs. Linux is known for its flexibility and power, relying on kernel and command line tools; while Windows is known for its user-friendliness and integration, mainly managing processes through graphical interfaces and system services.

What are the typical use cases for Linux versus Windows?What are the typical use cases for Linux versus Windows?May 03, 2025 am 12:01 AM

Linuxisidealforcustomization,development,andservermanagement,whileWindowsexcelsineaseofuse,softwarecompatibility,andgaming.Linuxoffershighconfigurabilityfordevelopersandserversetups,whereasWindowsprovidesauser-friendlyinterfaceandbroadsoftwaresupport

What are the differences in user account management between Linux and Windows?What are the differences in user account management between Linux and Windows?May 02, 2025 am 12:02 AM

The main difference between Linux and Windows in user account management is the permission model and management tools. Linux uses Unix-based permissions models and command-line tools (such as useradd, usermod, userdel), while Windows uses its own security model and graphical user interface (GUI) management tools.

How does the command line environment of Linux make it more/less secure than Windows?How does the command line environment of Linux make it more/less secure than Windows?May 01, 2025 am 12:03 AM

Linux'scommandlinecanbemoresecurethanWindowsifmanagedcorrectly,butrequiresmoreuserknowledge.1)Linux'sopen-sourcenatureallowsforquicksecurityupdates.2)Misconfigurationcanleadtovulnerabilities.Windows'commandlineismorecontrolledbutlesscustomizable,with

How to Make a USB Drive Mount Automatically in LinuxHow to Make a USB Drive Mount Automatically in LinuxApr 30, 2025 am 10:04 AM

This guide explains how to automatically mount a USB drive on boot in Linux, saving you time and effort. Step 1: Identify Your USB Drive Use the lsblk command to list all block devices. Your USB drive will likely be labeled /dev/sdb1, /dev/sdc1, etc

Best Cross-Platform Apps for Linux, Windows, and Mac in 2025Best Cross-Platform Apps for Linux, Windows, and Mac in 2025Apr 30, 2025 am 09:57 AM

Cross-platform applications have revolutionized software development, enabling seamless functionality across operating systems like Linux, Windows, and macOS. This eliminates the need to switch apps based on your device, offering consistent experien

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software