Docker Tutorial Series 2

Time:2021-6-12

Navigation

Manually run a container from the mirror

Start a container manually

Manually run a web application in the docker container of the local computer

``` sh
    #The following command starts a docker container
    docker run -d -t -p 8000:5000 --name demo ubuntu:18.04

    #The interpretation of the order
    -D means to let the container run in the background
    -T is rarely used to make a blank Ubuntu image run in the background
    -P is used to specify the port mapping, which means that the local access to 8000 will be automatically transferred to port 5000 in the container
       You must ensure that no other application on this machine occupies port 8000, otherwise it will fail here
    --Name demo defines the name of the container as demo
    ubuntu:18.04  Is the image name used when starting the container. Docker will automatically download the image from the image server
```

Common problems of starting container

#Start a process with port 3456
    docker run -t -d -p 3456:5000 --name test ubuntu:18.04
    #Error 1, port occupied, delete test container to avoid 3456 port occupied
    docker rm -f test
    #Error 2, the name already exists, then change the name, you can also use the above delete container command
    docker run -t -d -p 3456:5000 --name test1 ubuntu:18.04

About container related commands

#Lists the containers that this machine is running
    docker container ls

    #Lists all containers on the machine, including those that terminate operation
    docker container ls --all

    #View running containers
    docker ps

    #View the status of the latest specified number of containers
    docker ps -n 10

    #Terminate container
    docker stop <container id>/<container name>

    #Restart the terminated container, and query the container ID through docker PS
    docker start <container id>/<container name>

    #View container log
    docker logs <container id>/<container name

Install prerequisite software in container

After starting the container, it shows that a mirror instance is running, such as ubuntu:18.04 Test container generated by image. This container has the file management function, memory management function and process scheduling function of Ubuntu operating system. Now you need to have Python 3, PIP environment and apt toolkit.

```sh
    docker run -d -t -p 8000:5000 --name unbuntu:18.04
    #Download apt Toolkit
    docker exec demo apt update
    docker exec apt -y install python3 python3-pip
    #Install flash Library
    docker exec demo pip3 install flask
```

Running a program in a container

As mentioned above, the purpose of using docker is to install software application with environment. Now that the environment is good, you need to run the software application

```sh
    #Create / code directory in demo container
    docker exec demo mkdir /code
    #CP parameter copies a.py of the current folder to / code / a.py of the demo container
    docker cp a.py demo:/code/a.py
    #Run a.py in the demo container
    docker exec demo python3 /code/a.py
```

Configure the container by script, then run the script to configure and open the new container

In fact, a better way is to write a script on the host and run it directly in the docker container
See the example operation for specific operation

Instance operation

  1. Create a.py file in the current directory

    from flask import Flask
    
     app = Flask(__name__)
    
     @app.route('/')
     def index():
         return 'Hello from Docker'
    
     #Default port 5000
     if __name__ == "__main__":
         app.run(host="0.0.0.0", debug=True)
  2. Create the install.sh file in the current directory

     # install.sh
     apt update
     apt -y install python3 python3-pip
     pip3 install flask
  3. Create the run.sh file in the current directory

     # run.sh
     cd /code
     python3 a.py
  4. Open the terminal and enter the following command

    docker run -d -t -p 8000:5000 --name unbuntu:18.04
     #Create / code directory in demo container
     docker exec demo mkdir /code
     docker cp install.sh "demo:/code/install.sh"
     docker cp run.sh "demo:/code/run.sh"
     docker cp a.py "demo:/code/a.py"
     docker exec demo bash /code/install.sh
     docker exec demo bash /code/run.sh
  5. Open the browser and enter: localhost:8000/