Docker tutorial series 3

Time:2021-6-11

Navigation

Using dockerfile to package images

In the last article, we implemented running our program in a basic Ubuntu image. But it's very troublesome to run the program manually.
Next, we use the executed commands as scripts to improve the efficiency. However, if we want to start multiple containers, our self-made script needs to be re installed and configured every time. This process is very time-consuming. Is there an easier way? Yes, here's dockerfile

Introduction to dockerfile

Docker allows us to write our own configuration file (named dockerfile) to build an image, so that we can easily start multiple containers from the custom built image, so in practical use, we will package the whole program into a new docker, All start and run operations are written to your own custom image configuration file, so that the generated image instance (container) is just run. No additional configuration is required

About mirror related commands

#You can use the following command to view the images stored locally (including downloaded and built images)
    docker images
    #Delete image
    docker image rm <webimage>
    or
    docker rmi -f <webimage>
    #Build the image, - t represents the name and label of the image,. Represents the current working directory
    docker build -t webimage .

Image startup container built with dockerfile

The following is an explanation of the contents of the dockerfile file

```dockerfile
    #In the dockerfile file, # is a comment
    #From is used to specify the underlying image to be used for building the image
    FROM ubuntu:18.04

    #Run is used to execute commands in the image when building the image
    #Here we install Python 3 and flash web framework
    RUN apt update
    RUN apt -y install python3 python3-pip
    RUN pip2 insatll flask

    #Copy is equivalent to the docker CP of the command
    #Copy the app.py file in the current directory of the local computer to / code / app.py of the image
    #Unlike docker CP, copy automatically creates a directory that does not exist in the image, such as / code
    COPY app.py /code/app.py

    #Workdir is used to specify the working directory in the container started from the image
    WORKDIR /code

    #CMD is used to specify the list of commands and parameters to be executed after the container runs
    #In this way, after starting the container from this image, the command python3 app.py will be executed automatically
    #Since we have specified the working directory of the container with worddir
    #So the following commands are executed under / code
    CMD ["python3", "app.py"]

```

Instance operation

  1. Create app.py file in current directory

     # app.py
     from flask import Flask
    
    
     app = Flask(__name__)
    
    
     @app.route('/')
     def index():
         return 'Hello from Dockerfile'
    
    
     if __name__ == "__main__":
         app.run(host="0.0.0.0", debug=True)
  2. Create dockerfile file in current directory

    FROM ubuntu:18.04
    
     RUN apt update
     RUN apt -y install python3 python3-pip
     RUN pip3 install flask
    
     #Because docker build - t webimage is used
     #Indicates that the working directory of the image is the current directory
     COPY app.py /code/app.py
    
     WORKDIR /code
    
     CMD ["python3", "app.py"]
  3. Open the terminal and enter the following command

    #The - t webimage parameter in the command specifies the name of the image as webimage
     #This name can be used to start the container later from the image
     #The last one is used to specify that the working directory when building the image is the current directory of the local machine
     docker build -t webimage .
    
     #You can use the following command to view the images stored locally (including downloaded and built images)
     docker images
    
     #Run container
     docker run -p 8000:5000 --name demo webimage
  4. In the browser, enter: localhost:8000

Recommended Today

The road of high salary, a more complete summary of MySQL high performance optimization in history

preface MySQL for many linux practitioners, is a very difficult problem, most of the cases are because of the database problems and processing ideas are not clear. Before the optimization of MySQL, we must understand the query process of MySQL. In fact, a lot of query optimization work is to follow some principles so that […]