Home  >  Article  >  System Tutorial  >  Get started with Docker in five minutes

Get started with Docker in five minutes

WBOY
WBOYforward
2024-01-02 14:29:52816browse
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:linuxprobe.com. If there is any infringement, please contact admin@php.cn delete