Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to install and apply Docker

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

Today Xiaobian to share with you how to install Docker and application related knowledge points, detailed content, clear logic, I believe most people still know too much about this knowledge, so share this article for your reference, I hope you have some gains after reading this article, let's learn about it together.

Install Docker on Mac

Open docker download URL to download stable version and install and run docker. The whale in the top status bar indicates docker is running and accessible from the terminal.

Click on Whale in the status bar at the top and select about docker to verify that the latest version is available.

Run these commands to test whether docker, docker-assemble, docker-machine versions are up to date and compatible with docker. app.

$ docker --version$ docker-compose --version$ docker-machine --version

Open a command line terminal and you can run docker commands to verify that docker works. For example, use docker version to check if you have the latest version installed, docker ps to list containers running, and docker run hello-world to run sample programs.

The docker run command can be used to start a new container. This single command will trigger the installation run sequence: first look for the image locally, if the image cannot be found locally, download the image from the docker hub, load the image into the container, and then start the program, so that the container is running. Other systems can be referred to for installation.

docker sample project

Now start building your application with docker, starting at the bottom of the docker application hierarchy, where the application is a container, above this hierarchy is a service that defines how the container behaves at runtime, and finally at the top is the stack that defines the interactions of all services.

In the past, if you wanted to start writing a python application, the first step was to install a python runtime on your machine, but this caused the application to run only on your machine. Docker allows you to obtain a portable python runtime environment as an image without installing it. Build can then include the base python image along with your application code, ensuring that your application, dependencies, and runtime environment all move together.

These portable images are defined by something called a dockerfile.

Defining containers with dockerfile

Dockerfile will define what happens in the environment inside the container, access to resources like network interfaces and disk drives is virtualized in this environment, and this environment is isolated from the rest of the system, so you have to map ports to the outside world and specify which files you want to copy to that environment. Once this is done, the application defined in this dockerfile will behave exactly the same anywhere it runs.

Create a new directory, create a file named dockerfile in the new directory, copy and paste the following contents into the file and save.

#Use the official python runtime environment as the parent image from python:2.7-slim#Set the working directory to "/app"workdir /app#Copy the contents of the current directory to the container of "/app" add . /app#Install all required packages specified in requirements.txt run pip install --trusted-host pypi.python.org-r requirements.txt#Map port 80 to the outside world expose 80 #Define environment variables env name world#Run app.pycmd ["python", "app.py"] at container startup

Create and edit applications

Create a requirements.txt file and place it in the same directory as dockerfile.

flaskredis

Create app.py file and place it in the same directory as dockerfile.

from flask import flask from redis import redis, rediserrorimport osimport socket#Connect to redisredis = redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)app = flask(__name__)@ app. route("/")def hello(): try: visits = redis.incr("counter") except rediserror: visits = "cannot connect to redis, counter disabled" html = "hello {name}! " \ "hostname: {hostname}" \ "visits: {visits}" return html.format(name=os.getenv("name", "world"), hostname=socket.gethostname(), visits=visits)if __name__ == "__main__": app.run(host='0.0.0.0', port=80)

The application part is done quite simply, when dockerfile is built into the image, app.py and requirements.txt will appear thanks to dockerfile's add command, and app.py's output will be accessible via http thanks to expose.

build applications

Open a command line terminal, make sure you are still at the top of the new directory, and run the build command, which creates a docker image.

$ docker build -t friendlyhello .

The image built can be seen in the local docker image registry.

$ docker images

run the application

Run the application to map the machine's port 4000 to the container's published port 80 using-p.

$ docker run -p 4000:80 friendlyhello

You should now see a message that python provides an application at http://0.0.0:80, but this message comes from inside the container, it doesn't know that port 80 of the container maps to 4000, so the correct url is http://localhost:4000.

You can also view the same content using the curl command from the command line terminal.

$ curl http://localhost:4000

This 4000:80 port remapping is to demonstrate the difference between expose in dockerfile and content published using docker run -p. In a later step, port 80 on the host is mapped to port 80 in the container and http://localhost is used.

Under mac, click ctrl + c in the command line terminal to exit. On windows, ctrl + c does not stop containers. So first type ctrl + c to get a prompt, or open another shell, then type docker container ls to list the containers that are running, followed by docker container stop to stop the container. Otherwise, you will receive an error response from the daemon when you try to rerun the container in the next step.

The application now runs in the background in detached mode.

$ docker run -d -p 4000:80 friendlyhello

The container is running in the background, you can also use docker container ls to see the container id.

$ docker container ls

You can see that the container id matches the content on http://localhost:4000. Now use docker container stop to end the process.

$ docker container stop 9dfc794cdc52

shared image

To demonstrate image portability, upload the image we built and run it elsewhere; after all, you need to learn how to use the registry when deploying containers to production.

A registry is a collection of repositories, and a repository is a collection of mirrors, somewhat like a github repository, except that the code is already established. Accounts on the registry can create many repositories. Docker cli uses docker's public registry by default. Docker's public registry is used here because it is free and preconfigured, but there are many public options to choose from, and you can even set up your own private registry using docker trusted registry.

Log in with docker id

If you don't have a docker account, register one on docker's website. Then, log in to the docker public registry on your local machine.

$ docker login

mark mirror

The symbol that associates a local image with a repository in the registry is username/repository:tag, which is optional but recommended because it is the registry's mechanism for providing versions for docker images. Tagging the repository with a meaningful name, such as get-started:part2, puts the mirror into the startup repository and marks it part2.

Now, put them together to tag the image, run docker tag image with username, repository, tag name so that the image will upload to the desired destination.

#syntax $ docker tag image username/repository:tag#example $ docker tag friendlyhello john/get-started:part2

Run docker images or docker image ls to see the image of the new tag.

release image

Upload your tagged image to the repository.

#Syntax $ docker push username/repository:tag#Example $ docker push john/get-started:part2

Once done, the results of this upload are public, and if you log into docker hub, you will see the new mirror and its pull command.

Extract and run mirrors from remote repositories

From now on, you can use docker run and use this command to run your application on any machine.

#Syntax $ docker run -p 4000:80 username/repository:tag#Example $ docker run -p 4000:80 john/get-started:part2

If the image is not available locally on the machine, docker will be pulled from the repository. Wherever docker run is, it will extract your image, python, and all dependencies from requirements.txt and run your code. They are packed in suitcases to travel together, and the host doesn't need to install anything except a docker environment to run it.

That's all for Docker How to Install and Apply. Thank you for reading! I believe everyone has a great harvest after reading this article. Xiaobian will update different knowledge for everyone every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report