Docker basic summary

Life is like a play, and if you are not an actor?

I recently played Docker and it feels good. I will share some basic usages of docker in this record. This content will be supplemented by my continued in-depth study of docker, and everyone is welcome to correct it.
The advantages of Docker are not elaborated here. The purpose of docker is very wide. I recently prepared to use it to build a test environment (vulnerability test environment, development test environment, etc.), and can also be used to deploy distributed projects (which can greatly utilize the server). Resources), of course, there are many uses for docker, which are not detailed here.

docker Introduction

  • docker client and server (daemon)
  • docker image (image)
  • registry
  • docker container (container)

The docker container is built on top of the image. We can understand image as a defined class, and container is an instance. A class can instantiate many instances. Similarly, docker can also run multiple containers on the image. Containers can be the same or they can be customized. The docker client and server can run on the same host or different.
The registry is used to store the user’s image. It is divided into public and private. The public registry operated by docker is called the docker hub. Users can register their accounts on the docker hub and share and save their own images.

docker install

Docker can run on linux, mac, windows.

install docker for mac

Go to the official website to download the installation package: https://www.docker.com/products/docker#/mac
After the download is complete, install it directly. After the installation is complete, run docker run hello-world. If no error is reported, the installation is successful.
Replace the mirror source (fill in the domestic mirror source):

install docker for linux

Install docker:

1
sudo yum -y install docker-io

Start the docker daemon:

1
2
sudo service docker enable(start)
sudo /etc/init.d/docker start

Boot from boot:

1
sudo systemctl start(enable) docker

Replace domestic image

Alibaba Cloud image is recommended, address: http://dev.aliyun.com/search.html
After registering an account to log in, enter the console accelerator, you will get a mirror address, add the address to the /etc/default/docker file, and restart the docker service.

docker Basic Command

The docker basic commands include docker operations, mirror operations, container operations, and other related operations. The following lists some commonly used commands. For more details, please refer to the official documentation, or use the –help command.

docker operation

View docker information

1
2
3
4
5
6
7
8
$docker info
Containers: 1 #1 containers
Running: 1 #1 containers are running
Paused: 0
Stopped: 0
Images: 5 #5 mirrors
Server Version: 1.12.3
......

View docker version

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$docker -v
Docker version 1.12.3, build 6b644ec
$docker version
Client:
Version: 1.12.3
API version: 1.24
Go version: go1.6.3
Git commit: 6b644ec
Built: Wed Oct 26 23:26:11 2016
OS/Arch: darwin/amd64
Server:
Version: 1.12.3
API version: 1.24
Go version: go1.6.3
Git commit: 6b644ec
Built: Wed Oct 26 23:26:11 2016
OS/Arch: linux/amd64

Mirroring operation

Local images are saved in the /var/lib/docker directory.
View the local mirror list:

1
Docker images -a #-a can view all images


Other mirroring operations:

1
2
3
4
5
6
7
8
Docker search (image-name) query image example:docker search ubuntu
Docker history (image-name) View the historical version of the image
Docker push (image-name) push the image to the registry
Docker rmi <image id> delete the image (stop and delete all the containers on the mirror)
Docker rmi $(docker images -q) remove all images
Docker tag image-id imagename:tag modify the tag tag of the image
Docker load -i test.tar Loading images from tar files
Docker save image > test.tar save image as tar file

Container Operation

Create a container:

1
docker run --rm -ti ubuntu /bin/bash

  • –rm Delete the container once the process exits
  • -ti enter interactive mode
  • Mirror name of the ubuntu container
  • /bin/bash command to run
1
docker run -d -p 8000:80 --name test image-name
  • -d running in the background
  • -p mapped port, pre-local, post-container
  • –name custom name

Note: After the container is created, an ID is returned, which is randomly generated.

Check the container running status:

1
Docker ps -a (display all containers, including already stopped)

View container specific information:

1
Docker inspect container id (container name)

More detailed than the ps-a command, including network information, configuration information, etc., can be matched with -forma, such as:

1
Sudo docker inspect --format '{{ .NetworkSettings.IPAddress }}' c18acd6a8a32 #View container ip address

View the process inside the container:

1
Docker top container id

Enter the inside of the container:

1
Sudo docker attach container id

Or you can use:

1
Docker exec -ti container name /bin/bash

The exec command can execute commands inside the container. The above code indicates that a new shell is created in the container.

Exit the container:

1
[rootq3e1]exit

Restart the container:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
```
The restart parameter can set the following:
* always restarts the docker container regardless of what code is exited inside the container.
* on-failure can specify an exit code
More container operations:
```bash
Docker attach container enters the container interactive interface
Docker diff container lists files and directories that have changed within the container
Docker logs ID (the ID of the container, which can be viewed with docker ps) View the status of the container on docker
Docker stop ID (or container name) close the running container
Docker start ID open container
Docker restart ID restart the running container
Docker stop ID docker rm ID remove container (close and delete first)
Docker kill [options] container kills the main process of the container
Docker stop $(docker ps -a -q) Stop all containers
Docker rm $(docker ps -a -q) remove all containers
Docker commit ID new image name (only letters plus numbers) Save the state of the container as a mirror
Docker export container > test.tar package the container into a tar file
Docker cp container:path hostpath Copy files from the container to the specified path

Container Network Management: (Thanks @DarkEvil for sharing)

  • host mode, use dockerrun with –net=host specified (docker uses the same network as the host, the network card ip seen in the container is the ip on the host)
  • container mode, use –net=container:container_id/container_name (multiple containers use the same network, see the ip is the same)
  • none mode, specified with –net=none (in this mode, no network will be configured)
  • bridge mode, specified with –net=bridge
  • Default mode, this network mode is not required to specify the default. (This mode assigns a separate Network Namespace to each container. Similar to vmware’s nat network mode. All containers on the same host will be on the same network segment and can communicate with each other.)

other operation

1
2
3
4
Docker import http://example.com/example.tar remote import file
Docker login [options][server] is used to log in to their registry
Docker inspect container/image Collect container information (ip address, port, etc.)
Docker wait container

dockerfile

The dockerfile can be used to dynamically generate new images. For example, we have pulled a basic centos image. Now we need to install some software in this image so that we can run the project code we prepared smoothly. Then we can use the following two schemes:

Option 1: (manual)

Create a container on the centos image, enter the container interactive interface, manually install some required software, configure the environment. When all the changes have been made, create a new image using docker commit container-id newimagename. Then use the new image to create the container and run our project code.

Scheme 2: (Automatic)

The so-called automation, that is, you do not need to enter the container to manually enter the command to configure, everything is automatically processed when the container is running, then this will use the dockerfile. The dockerfile is simply a configuration file. The docker container handles the contents of the file at runtime, such as installing software, modifying environment variables, running programs, and so on. The advantage of using dockerfile is that it is very convenient to modify the contents of the configuration file to achieve the effect of creating a dynamic image.

Creating a dockerfile

We need to create a directory to store the Dockerfile, the directory name can be arbitrarily selected, in this directory to create a Dockerfile. Here I create an instance based on centos, the container will automatically open a python webserver (local monitor 8080 port) after running.

Writing a dockerfile

Write in the Dockerfile:

1
2
3
4
5
6
7
# Version 0.1
FROM centos:latest
# Maintainer information
MAINTAINER http://thief.one
# Mirror operation command
RUN yum install wget
CMD python -m SimpleHTTPServer 8080

The dockerfile syntax is similar to MakeDown. The basic content is as follows:

  • FROM based base image name
  • MAINTAINER maintainer information
  • RUN running commands (installing software, etc.)
  • CMD command to run when starting the container (only one can be written)

The grammar is more than this, more content, you can refer to the official documentation.

Generating a dockerfile image

Go to the directory where the Dockerfile is located and run:

1
2
3
docker build -t centos_test:01 .
or
Docker build -t centos_test:01 git@github:......(remote git address)

At this point, run docker images -a view, you will find an additional image, the name is centos_test, the tag is 01

If there is a problem with the dockerfile, an error will be reported during the build. At this point, you can use the docker run container id to enter the container in the final state for debugging.

Using the dockerfile image

Run the container on this image:

1
docker run -d -p 80:8080 centos_test:0.1

At this point, turn on the unit’s 127.0.0.1:80

dockerfile rules

Each instruction must be in uppercase letters, such as FROM, RUN, and followed by a content, the docker file will execute the content in order from top to bottom.

WORKDIR

Role: set the working directory, similar to cd

1
2
3
4
WORKDIR /root/
RUN apt-get install pip
WORKDIR /root/test
......

You can override the container working directory with the -w parameter

1
docker run -w /root/nmasktools ......

ENV

Role: set the environment variables in the container
Can be covered with -e.

USER

Role: Specify what kind of user to run the container

1
USER nmask (username or id)

Can be overridden with docker -u.
Both add and copy are used to add files to the image. The difference is that copy can only copy files without decompression.

docker has a lot of content, these are just some of the most basic usages, and this article will continue to be updated

本文标题:Docker basic summary

文章作者:nmask

发布时间:2017年05月04日 - 15:05

最后更新:2019年07月11日 - 16:07

原始链接:https://thief.one/2017/05/04/01/en/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

nmask wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!

热门文章推荐: