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

Installation and simple use of Docker

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains the "installation and simple use of Docker", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Docker installation and simple use" bar!

This paper gives an example of simple installation and application of Docker. Share with you for your reference, the details are as follows:

As a first-time contact with Docker, the biggest question is what is Docker? Docker is an open source engine that can easily create a lightweight, portable, self-sufficient container for any application. For example, containers that we have compiled and tested on notebooks can be deployed in batches in a production environment and can be used for automated testing and continuous integration and release.

It can also be used for automatic packaging and distribution of web applications; deploy and adjust databases or other background applications in a service-oriented environment; and compile or expand existing OpenShift or Cloud Foundry platforms from scratch to build their own PaaS environment.

Install Docker under Mac

Open the Docker download site to download the stable version, and install and run Docker. The whale in the top status bar indicates that Docker is running and can be accessed from the terminal.

Click the 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 the docker, docker-ompose, and 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 some Docker commands to verify that Docker is working properly. For example, use docker version to check if you have the latest version installed, use docker ps to list the containers that are running, and use docker run hello-world to run the sample program.

Using the docker run command, you can start a new container, and this single command triggers the installation run sequence: look for the image locally, download the image on Docker Hub if it can't be found locally, load the image into the container, and then start the program so that the container runs. Other systems can be installed by referring to the official website documentation.

Docker sample project

Now start using Docker to build the application, starting at the bottom of the Docker application hierarchy, where the application is a container, at this level is a service that defines how the container behaves at runtime, and finally, at the top level is the stack, which defines the interaction 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. Using Docker, you can obtain a portable Python runtime environment as a mirror without installation. The build can then include the underlying Python image with the application code to ensure that your application, dependencies, and runtime environment all move together.

These portable images are defined by something called Dockerfile.

Define containers with Dockerfile

Dockerfile defines what happens in the environment within the container, where access to resources such as network interfaces and disk drives is virtualized and isolated from the rest of the system, so you must 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 the Dockerfile will behave exactly the same anywhere.

Create a new directory, create a file called Dockerfile under the new directory, and copy and paste the following into the file to save.

# use the official Python runtime environment as the parent image FROM python:2.7-slim# to set the working directory to "/ app" WORKDIR / app# to copy the contents of the current directory to the container "/ app" ADD. / app# installs all required packages RUN pip install specified in requirements.txt-- trusted-host pypi.python.org-r requirements.txt# maps port 80 to the outside world EXPOSE 8 defines environment variables ENV NAME World# container runs on startup app.pyCMD ["python", "app.py"]

Create and edit the application

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

FlaskRedis

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

From flask import Flaskfrom 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 easily completed. When Dockerfile is built into the image, app.py and requirements.txt will appear due to Dockerfile's ADD command, and the output of app.py will be accessible through HTTP, thanks to the EXPOSE command.

Build an application

Open a command line terminal, make sure it is still at the top level of the new directory, and run the build command, which creates a Docker image.

$docker build-t friendlyhello.

The constructed image can be seen in the local Docker image registry.

$docker images

Run the application

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

$docker run-p 4000 purl 80 friendlyhello

You should now see a message that Python provides the application on http://0.0.0.0:80, but this message comes from inside the container, and it doesn't know that port 80 of the container is mapped to 4000, so the correct URL is http://localhost:4000.

You can also use the curl command in the command line terminal to view the same content.

$curl http://localhost:4000

The purpose of this port remapping of 4000 docker run 80 is to demonstrate the difference between EXPOSE in Dockerfile and content published using docker run-p. In the next steps, 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. Under Windows, CTRL + C does not stop the container. So first type CTRL + C to get the prompt, or open another Shell, then type docker container ls to list the running containers, followed by docker container stop to stop the containers. Otherwise, when you try to rerun the container in the next step, you will receive an error response from the daemon.

Now run the application in the background in detached mode.

$docker run-d-p 4000 purl 80 friendlyhello

The container is running in the background, and you can also view the CONTAINER ID using docker container ls.

$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

Share the image

To demonstrate the portability of the image, upload the image we built and run it elsewhere. After all, you need to learn how to use the registry when deploying the container to a production environment.

A registry is a collection of repositories, and a repository is a collection of images, a bit like a GitHub repository, except that the code has been established. Accounts on the registry can create many repositories. Docker CLI defaults to the public registry of Docker. 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 use Docker Trusted Registry to set up your own private registry.

Log in using Docker ID

If you do not have a Docker account, please register one on the Docker website. Then, log in to the Docker public registry on the local machine.

$docker login

Tag mirroring

The symbol that associates the local image with the repository in the registry is username/repository:tag, which is optional but recommended because this is the mechanism by which the registry provides versions of Docker images. Mark the repository with a meaningful name, such as get-started:part2, which places the image in the startup repository and marks it as part2.

Now, put them together to mark the image, and run docker tag image with the user name, repository, and tag name so that the image will be uploaded 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 view the mirror of the new tag.

Publish an image

Upload the image you marked to the repository.

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

Once completed, the result of this upload is public, and if you log in to Docker Hub, you will see the new image and its pull command.

Extract and run the mirror from the remote repository

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

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

If the image is not available locally on the machine, Docker will be removed 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 mainframe does not need to install anything except the Docker environment to run it.

I hope what is described in this article will be helpful to the use of docker containers.

Thank you for your reading, the above is the content of "installation and simple use of Docker". After the study of this article, I believe you have a deeper understanding of the installation and simple use of Docker, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Servers

Wechat

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

12
Report