Docker Learning Diary

Posted by fabrice on Sat, 13 Jun 2020 19:22:01 +0200

Introduction:
Docker is an open source application container engine based on the Go language and compliant with the Apache 2.0 protocol.
Docker s can allow developers to package their applications and dependencies into a lightweight, portable container that can then be published to any popular Linux machine, or they can be virtualized.
Containers are fully sandboxed and do not have any interfaces with each other (app s like the iPhone), more importantly, they have very low performance overhead.

Common Commands

**
# docker PS //View the currently started container
 # docker PS-A //View all containers
 # docker start 5917eac21c36 //start container ID 5917eac21c36 container
 # docker stop 5917eac21c36 //close container ID: container 5917eac21c36
 # docker restart 5917eac21c36 //restart container ID: container 5917eac21c36
 # docker logs 2b1b7a428627 //View container ID is:..Container log for
 # docker pull ubuntu //get mirror
 # docker run-it ubuntu/bin/bash // Enter a container mirroring Ubuntu in command line mode
 # docker run-itd ubuntu/bin/bash // -d is run in the background without entering the container by default
 # docker rmi-f //Remove mirror with mirror ID b93646ae03d0
 # docker rm-f //Delete container ID 4381b8e35d1
 # docker run-it-p 808:80 centos/Dvwa:v3/bin/bash//Affect the internal 80 ports on the native 808 port, continue adding the -p parameter eg: -p 223:22 -p 63306:3306 if you need to map multiple ports
 # docker run-d -P centos/Dvwa:v3/bin/bash//Randomly map internal ports to physical machines
 # docker top 67094b1dec19 //View container processes
 # docker search httpd //Find Mirrors can also go to the docker website to search, Docker Hub address:https://hub.docker.com/

**

[root@1inux ~]# docker run ubuntu:15.10 /bin/echo "Hello world"
Hello world

Parameter parsing:

Docker: The binary execution file of the Docker.
Run: Combines with the previous docker to run a container.
Ubuntu:15.10Specify the mirror to run. Docker first looks for the existence of the mirror from the local host. If it does not, Docker downloads the public mirror from the mirror repository Docker Hub.
/bin/echo "Hello world": commands executed in the boot container

Run an interactive container:

 [root@1inux ~]# docker run -it ubuntu:15.10 /bin/bash
root@9d9162035d58:/# cat /etc/issue
Ubuntu 15.10 \n \l

Parameter parsing:

-t: Specify a pseudo terminal or terminal within the new container.
-i: Allows you to interact with standard input (STDIN) in a container.

Exit the container using exit or CTRL+D

Enter Container

When the -d parameter is used, the container will go into the background when it is started.If you want to enter the container at this time, you can do so by following the instructions below:
docker attach
 Docker exec: It is recommended that you use the docker exec command, as this exit from the container terminal will not cause the container to stop.

Export and import containers

Export Container
[root@1inux ~]# Docker export 67094b1dec19 >Centos.dvwa.tar//Export container ID:67094b1dec19 to local

//Import container:
# CatCentos.dvwa.tar| docker import - centos/Dvwa:v1 //Notice that there is'-'after import
sha256:6dc11a8857af3eabf0f42a270bda3bd98849ab237f1d3277562e079b2fbdca8f

create mirror
1. Update the image from the created image and submit the image
2. Use the Dockerfile directive to create a new image

Update Copy:
# docker commit -m="has dvwa" -a="glodon_1inux" 67094b1dec19 centos/dvwa:v1
 -m: Descriptive information submitted
 -a: Specify mirror author
 67094b1dec19: Container ID
 centos/dvwa:v1: Specify the target image name to create
 #Start with the following command
# docker run -it -p 80:80 centos/dvwa:v1

Build a new image

We use the command docker build to create a new image from scratch.To do this, we need to create a Dockerfile that contains a set of instructions to tell Docker how to build our image.
Write Dockerfile

# cat Dockerfile
FROM 1inux/centos:6.6

MAINTAINER      Fisher "MQ@1inux"

COPY ./rasp-*/rasp /rasp      // .rasp-* and Dockerfile in one directory

RUN echo "cloud.enable: true" >> /rasp/conf/openrasp.yml \
    && echo "cloud.backend_url: https://grasp-test.glodon.com/" >> /rasp/conf/openrasp.yml \
    && echo "cloud.app_id: d3f92106daa329948879e683ee1d6d39641723ca" >> /rasp/conf/openrasp.yml \
    && echo "cloud.app_secret: MRLTPAdlR2gG2iJrv6O1pEZrgrauVoxlOKH8nOWnuDa" >> /rasp/conf/openrasp.yml

After writing, use the docker build command to build a mirror

#** docker build -t 1inux/centos-rasp:6.6 .**
Sending build context to Docker daemon  42.06MB
Step 1/4 : FROM 1inux/centos:6.6
 ---> b93646ae03d0
Step 2/4 : MAINTAINER      Fisher "MQ@1inux"
 ---> Running in f34a84c360ce
Removing intermediate container f34a84c360ce
 ---> d6104f825a18
Step 3/4 : COPY ./rasp-*/rasp /rasp
 ---> 70c531b49efe
Step 4/4 : RUN echo "cloud.enable: true" >> /rasp/conf/openrasp.yml     && echo "cloud.backend_url: https://grasp-test.glodon.com/" >> /rasp/conf/openrasp.yml     && echo "cloud.app_id: d3f92106daa329948879e683ee1d6d39641723ca" >> /rasp/conf/openrasp.yml     && echo "cloud.app_secret: MRLTPAdlR2gG2iJrv6O1pEZrgrauVoxlOKH8nOWnuDa" >> /rasp/conf/openrasp.yml
 ---> Running in 98952e495c27
Removing intermediate container 98952e495c27
 ---> 3f4001f43793
Successfully built 3f4001f43793
Successfully tagged 1inux/centos-rasp:6.6

Parameter description:
-t: Specify the target image name to create
The directory where the Dockerfile is located, you can specify the absolute path to the Dockerfile

Container interconnection:

New Network:

# Docker network create-d bridge 1inux-test // -d Specifies the Docker network type, with bridge, overlay.

# docker network ls //view network
//Start two containers of the same network
# docker run -idt --name test1 --network 1inux-test 1inux/centos-rasp:6.6 /bin/bash
#  docker run -idt --name test2 --network 1inux-test 1inux/centos-rasp:6.6 /bin/bash
//The results are as follows;
[root@localhost /]# docker exec -it e8cdc773df3e /bin/bash
[root@e8cdc773df3e /]# **ping test1**
PING test1 (172.20.0.2) 56(84) bytes of data.
64 bytes from test1.1inux-test (172.20.0.2): icmp_seq=1 ttl=64 time=0.414 ms
--- test1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 968ms
rtt min/avg/max/mdev = 0.414/0.414/0.414/0.000 ms
[root@e8cdc773df3e /]# ping test2
PING test2 (172.20.0.3) 56(84) bytes of data.
64 bytes from e8cdc773df3e (172.20.0.3): icmp_seq=1 ttl=64 time=0.082 ms
64 bytes from e8cdc773df3e (172.20.0.3): icmp_seq=2 ttl=64 time=0.100 ms

Push Mirror
After a user logs in, he or she can push his or her own image to Docker Hub through the docker push command.
Replace username with your Docker account username in the following command.

$ docker tag ubuntu:18.04 username/ubuntu:18.04
$ docker image ls

Topics: Operation & Maintenance Docker CentOS Ubuntu network