Wiki source code of How to Install and Use Docker on Rocky Linux 8
Last modified by Сергей Коршунов on 2021/12/19 14:46
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | Step 1: Update Rocky Linux 8 System | ||
2 | |||
3 | It should return something like this: | ||
4 | |||
5 | Check Docker status | ||
6 | |||
7 | Press q to exit back to the command prompt. | ||
8 | |||
9 | If the Docker service is not started and running, use the start subcommand to start it. | ||
10 | |||
11 | sudo systemctl start docker | ||
12 | |||
13 | Step 4: Adding New User | ||
14 | |||
15 | Once the installation is done, if we want to use Docker, we need to do it as a root user. To solve this problem, give the user access to the Docker group. Now they can run containers as a regular user. | ||
16 | |||
17 | To do this, you should add the regular user to the ‘docker’ group. This is done with the command usermod. In this example, we will add a vitux user with the usermod command as follows: | ||
18 | |||
19 | sudo usermod -aG docker vitux | ||
20 | |||
21 | If you want to add another user to the docker group, replace “vitux” with their username. Then log out and log back in to activate the docker group membership. | ||
22 | |||
23 | You have added a new user to the docker group. You can check it by the following command: | ||
24 | |||
25 | sudo id vitux | ||
26 | |||
27 | Step 5: Using the Docker Command | ||
28 | |||
29 | There are now 13 management commands and 41 general commands available to us, plus a few utility commands. | ||
30 | |||
31 | Most of these commands have the same syntax as they do on other Linux distributions. | ||
32 | |||
33 | The docker command consists of a set of functions and options and arguments. | ||
34 | |||
35 | docker [option] [command] [arguments] | ||
36 | |||
37 | To view all available options and commands, type: | ||
38 | |||
39 | docker | ||
40 | |||
41 | docker command | ||
42 | |||
43 | Use the following command to learn more about Docker across the system. | ||
44 | |||
45 | docker info | ||
46 | |||
47 | Docker info Step 6: Testing Docker in Rocky Linux 8 | ||
48 | |||
49 | To test Docker, we will do a very simple task. We want to pull the hello-world image. This image is very popular, and it will give you an idea about docker images. It’s really easy to do this task, just type: | ||
50 | |||
51 | docker run hello-world | ||
52 | |||
53 | The command searches the hello-world image on your computer. If it’s not found, the command will pull the hello-world image from Docker Hub, then automatically runs it. After that, you should see the Hello from Docker! message on your screen. This message confirms that your installation is up and running properly. | ||
54 | |||
55 | Test Docker Step 7: Working with Docker Images | ||
56 | |||
57 | A container is a version of an image that can be executed. Docker gets these images from Docker Hub by default, which is a repository maintained by the organization that created Docker. Anyone can put their own pictures of their things on this site. Unlike virtual machines, which use emulation to run an operating system on the host computer (a copy of Windows or Linux), a container runs entirely within a single operating system on the host computer. | ||
58 | |||
59 | You can use the search function on the Docker Hub to look for images. To find an image, execute this command: | ||
60 | |||
61 | docker search imagename | ||
62 | |||
63 | We will be looking for the Ubuntu image in this case. | ||
64 | |||
65 | docker search ubuntu | ||
66 | |||
67 | As you can see below, there are many images available on the Docker Hub server. | ||
68 | |||
69 | Search for Docker images | ||
70 | |||
71 | If the word OK appears in the OFFICIAL column, it indicates that the image was created and is being supported by the business behind the project. You may download images for your project using the pull subcommand after you have discovered the images you want to use. In this example, we’ll use the Ubuntu operating system image. | ||
72 | |||
73 | docker pull ubuntu | ||
74 | |||
75 | It should return an output like this: | ||
76 | |||
77 | Get Ubuntu Docker image | ||
78 | |||
79 | To see the images that are on your server, type: | ||
80 | |||
81 | docker images | ||
82 | |||
83 | It should return an output like this: | ||
84 | |||
85 | List Docker images | ||
86 | |||
87 | You can modify images and use them for building new images. This is a very efficient way to work with containers because you do not have to download the whole image every time you need it. | ||
88 | |||
89 | It’s pretty simple to modify an image, change something or add some code, then save this as a new image and share it with your friends, or whoever needs this new image by uploading it to the Docker Hub or any other Docker registry. Step 8: Running a Docker Container | ||
90 | |||
91 | The hello-world container in the previous step was an example of a container that runs, emitting a Hello from Docker and then stops. Containers can be useful for more than just running one thing and stopping, though. | ||
92 | |||
93 | You can also run a container that will stay running, listening on a port and doing whatever you want it to do. To try this out, we’ll use the Ubuntu image and run a container. | ||
94 | |||
95 | docker run -it ubuntu | ||
96 | |||
97 | The combination of the -i and -t options tells docker to create a container and give you an interactive console to it. That sounds complicated, but it’s not. | ||
98 | |||
99 | The first time a container is started, the process that starts the container must be attached so that it can see any signals sent from bash. The -t option tells the docker which tty to open. Once the container has started, the -i option means you will get an interactive console, like this: | ||
100 | |||
101 | Run Docker image | ||
102 | |||
103 | Now you can type any command inside the container. Suppose you want to update the list of packages inside the container. You don’t need to type sudo before each command because you are in charge of running commands as the root user of this container. | ||
104 | |||
105 | apt update | ||
106 | |||
107 | Update Docker image | ||
108 | |||
109 | The container is effectively a microservice itself, and it has its own restrictions. Any changes you make inside the container are solely applicable to that specific container. | ||
110 | |||
111 | To quit the container shell, type exit at the prompt then hit Enter key. |