<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Renan Maringolo</title>
    <description>The latest articles on Forem by Renan Maringolo (@renanmaringolo).</description>
    <link>https://forem.com/renanmaringolo</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F613188%2Fefbbd631-d5e2-45dc-89f9-75a2172e05fc.jpg</url>
      <title>Forem: Renan Maringolo</title>
      <link>https://forem.com/renanmaringolo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/renanmaringolo"/>
    <language>en</language>
    <item>
      <title>Construindo Busca Completa no PostgresSQL / Building Complete Search in PostgresSQL</title>
      <dc:creator>Renan Maringolo</dc:creator>
      <pubDate>Tue, 17 Dec 2024 15:14:45 +0000</pubDate>
      <link>https://forem.com/renanmaringolo/busca-de-texto-completo-poderosa-no-postgresql-3i25</link>
      <guid>https://forem.com/renanmaringolo/busca-de-texto-completo-poderosa-no-postgresql-3i25</guid>
      <description>&lt;p&gt;PT-BR:&lt;/p&gt;

&lt;p&gt;A busca de texto completo (FTS) no PostgreSQL é poderosa, rápida e fácil de configurar, eliminando a necessidade de ferramentas externas como Elasticsearch em muitos cenários. Inclusive, nunca havia feito essa funcionalidade e inicialmente pensei em usar ES e até questionei colegas a respeito. Só que o problema do ES é que pode ser "too much" e caro ao que você precisa!&lt;/p&gt;

&lt;h2&gt;
  
  
  Passo 1: Criando a tabela e populando com dados
&lt;/h2&gt;

&lt;p&gt;Primeiro, crio uma tabela simples e adiciono dados para realizar nossas buscas: Artigos com colunas id, titulo e descrição!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE artigos (
  id SERIAL PRIMARY KEY,
  titulo VARCHAR(80) NOT NULL,
  descricao VARCHAR(200) NOT NULL
);

INSERT INTO artigos (titulo, descricao) VALUES
  ('Aprenda PostgreSQL', 'Um guia completo sobre PostgreSQL'),
  ('Introdução ao SQL', 'Entenda os fundamentos de SQL'),
  ('Busca de texto no Postgres', 'Como implementar busca de texto no PostgreSQL'),
  ('Programação em Java', 'Um curso básico de Java'),
  ('Desenvolvimento web com Rails', 'Aprenda a criar aplicações com Rails'),
  ('Curso avançado de SQL', 'Domine SQL com exemplos práticos'),
  ('Aprendizado de Máquina', 'Introdução ao Machine Learning');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Executando a query para verificar:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM artigos;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Passo 2: Implementando a busca simples com LIKE
&lt;/h2&gt;

&lt;p&gt;Uma busca simples em SQL usa LIKE ou ILIKE (case-insensitive):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT id, titulo, descricao
FROM artigos
WHERE titulo ILIKE '%PostgreSQL%' OR descricao ILIKE '%PostgreSQL%';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Problemas com o LIKE:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;É case-sensitive (exceto ILIKE).&lt;/li&gt;
&lt;li&gt;Não oferece relevância ou ranqueamento.&lt;/li&gt;
&lt;li&gt;Torna-se lento com grandes volumes de dados, pois não há índice.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Passo 3: Configurando a busca de texto completo (FTS)
&lt;/h2&gt;

&lt;p&gt;Como falei no início em relação ao ES, o PostgreSQL possui recursos nativos para busca de texto completo. Aqui estão os dois principais:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;to_tsvector: Converte um texto em um vetor de busca (documento pesquisável).&lt;/li&gt;
&lt;li&gt;to_tsquery: Converte um texto em uma query para busca.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Exemplo básico:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * 
FROM artigos
WHERE to_tsvector('portuguese', titulo || ' ' || descricao) 
      @@ to_tsquery('portuguese', 'Rails');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Aqui, buscamos pela palavra Rails nos títulos e descrições.&lt;/p&gt;

&lt;h2&gt;
  
  
  Passo 4: Adicionando relevância com ts_rank
&lt;/h2&gt;

&lt;p&gt;O ts_rank calcula a relevância da busca, permitindo ranquear os resultados.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT id, titulo, descricao,
       ts_rank(to_tsvector('portuguese', titulo), to_tsquery('portuguese', 'PostgreSQL')) AS rank_titulo,
       ts_rank(to_tsvector('portuguese', descricao), to_tsquery('portuguese', 'PostgreSQL')) AS rank_descricao
FROM artigos
WHERE to_tsvector('portuguese', titulo || ' ' || descricao) 
      @@ to_tsquery('portuguese', 'PostgreSQL')
ORDER BY rank_titulo DESC, rank_descricao DESC;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Passo 5: Usando GIN e sendo feliz com sua performance!
&lt;/h2&gt;

&lt;p&gt;Adicionando um índice GIN acelera a busca significativamente:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE INDEX idx_artigos_fts 
ON artigos USING GIN (to_tsvector('portuguese', titulo || ' ' || descricao));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Agora a busca utiliza o índice GIN para maior performance!&lt;/p&gt;

&lt;h2&gt;
  
  
  Passo 6: Adicionando busca aproximada com pg_trgm
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Para permitir buscas com erros de digitação (reann), usamos a extensão pg_trgm e a função SIMILARITY:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Habilite a extensão:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE EXTENSION IF NOT EXISTS pg_trgm;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Combine busca FTS com busca aproximada:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT id, titulo, descricao,
       ts_rank(to_tsvector('portuguese', titulo), to_tsquery('portuguese', 'PostgreSQL')) AS rank_titulo,
       similarity('PostgreSQL', titulo || ' ' || descricao) AS similaridade
FROM artigos
WHERE to_tsvector('portuguese', titulo || ' ' || descricao) 
      @@ to_tsquery('portuguese', 'PostgreSQL')
   OR similarity('PostgreSQL', titulo || ' ' || descricao) &amp;gt; 0.2
ORDER BY rank_titulo DESC, similaridade DESC;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;O resultado final: Busca de texto completo com ranking e similaridade&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT id, titulo, descricao,
       ts_rank(to_tsvector('portuguese', titulo), to_tsquery('portuguese', 'SQL')) AS rank_titulo,
       ts_rank(to_tsvector('portuguese', descricao), to_tsquery('portuguese', 'SQL')) AS rank_descricao,
       similarity('SQL', titulo || ' ' || descricao) AS similaridade
FROM artigos
WHERE to_tsvector('portuguese', titulo || ' ' || descricao) 
      @@ to_tsquery('portuguese', 'SQL')
   OR similarity('SQL', titulo || ' ' || descricao) &amp;gt; 0.2
ORDER BY rank_titulo DESC, rank_descricao DESC, similaridade DESC;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Conclusão&lt;/p&gt;

&lt;p&gt;Antes de colocar inúmeras ferramentas para resolver problema de Busca Completa, tente utilizar isso na sua aplicação, pois na maioria esmagadora das vezes resolverá os problemas. Com o uso de índices GIN e a extensão pg_trgm, conseguimos um sistema de busca rápido e eficiente. 🚀&lt;/p&gt;

&lt;p&gt;fontes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://leandronsp.com/a-powerful-full-text-search-in-postgresql-in-less-than-20-lines" rel="noopener noreferrer"&gt;Artigo&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pganalyze.com/blog/gin-index" rel="noopener noreferrer"&gt;Entendendo Index GIN&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pganalyze.com/blog/similarity-in-postgres-and-ruby-on-rails-using-trigrams#:~:text=The%20similarity%20function%20compares%20the,whether%20two%20strings%20are%20similar." rel="noopener noreferrer"&gt;Similaridade no Postgres e usando migrações do Rails&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;EN:&lt;/p&gt;

&lt;p&gt;Full-text search (FTS) in PostgreSQL is powerful, fast, and easy to configure, eliminating the need for external tools like Elasticsearch in many scenarios. In fact, I had never implemented this functionality before and initially thought about using ES and even asked colleagues about it. But the problem with ES is that it can be "too much" and expensive for what you need!&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Creating the table and populating with data
&lt;/h2&gt;

&lt;p&gt;First, I create a simple table and add data to perform our searches: Articles with columns id, title, and description!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE articles (
  id SERIAL PRIMARY KEY,
  title VARCHAR(80) NOT NULL,
  description VARCHAR(200) NOT NULL
);

INSERT INTO articles (title, description) VALUES
  ('Learn PostgreSQL', 'A complete guide on PostgreSQL'),
  ('Introduction to SQL', 'Understand the fundamentals of SQL'),
  ('Text search in Postgres', 'How to implement text search in PostgreSQL'),
  ('Programming in Java', 'A basic Java course'),
  ('Web development with Rails', 'Learn to create applications with Rails'),
  ('Advanced SQL Course', 'Master SQL with practical examples'),
  ('Machine Learning', 'Introduction to Machine Learning');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Running the query to verify:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM articles;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 2: Implementing a simple search with LIKE
&lt;/h2&gt;

&lt;p&gt;A simple search in SQL uses LIKE or ILIKE (case-insensitive):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT id, title, description
FROM articles
WHERE title ILIKE '%PostgreSQL%' OR description ILIKE '%PostgreSQL%';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Problems with LIKE:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It is case-sensitive (except ILIKE).&lt;/li&gt;
&lt;li&gt;It does not offer relevance or ranking.&lt;/li&gt;
&lt;li&gt;It becomes slow with large volumes of data since there is no index.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Step 3: Configuring full-text search (FTS)
&lt;/h2&gt;

&lt;p&gt;As I mentioned earlier about ES, PostgreSQL has native features for full-text search. Here are the two main ones:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;to_tsvector: Converts a text into a search vector (searchable document).&lt;/li&gt;
&lt;li&gt;to_tsquery: Converts a text into a search query.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * 
FROM articles
WHERE to_tsvector('english', title || ' ' || description) 
      @@ to_tsquery('english', 'Rails');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we search for the word "Rails" in titles and descriptions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Adding relevance with ts_rank
&lt;/h2&gt;

&lt;p&gt;ts_rank calculates the search relevance, allowing us to rank the results.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT id, title, description,
       ts_rank(to_tsvector('english', title), to_tsquery('english', 'PostgreSQL')) AS rank_title,
       ts_rank(to_tsvector('english', description), to_tsquery('english', 'PostgreSQL')) AS rank_description
FROM articles
WHERE to_tsvector('english', title || ' ' || description) 
      @@ to_tsquery('english', 'PostgreSQL')
ORDER BY rank_title DESC, rank_description DESC;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 5: Using GIN and being happy with its performance!
&lt;/h2&gt;

&lt;p&gt;Adding a GIN index significantly speeds up the search:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE INDEX idx_articles_fts 
ON articles USING GIN (to_tsvector('english', title || ' ' || description));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the search uses the GIN index for better performance!&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 6: Adding approximate search with pg_trgm
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;To allow searches with typos (reann), we use the pg_trgm extension and the SIMILARITY function:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Enable the extension:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE EXTENSION IF NOT EXISTS pg_trgm;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Combine FTS with approximate search:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT id, title, description,
       ts_rank(to_tsvector('english', title), to_tsquery('english', 'PostgreSQL')) AS rank_title,
       similarity('PostgreSQL', title || ' ' || description) AS similarity
FROM articles
WHERE to_tsvector('english', title || ' ' || description) 
      @@ to_tsquery('english', 'PostgreSQL')
   OR similarity('PostgreSQL', title || ' ' || description) &amp;gt; 0.2
ORDER BY rank_title DESC, similarity DESC;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Result: Full-text search with ranking and similarity&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT id, title, description,
       ts_rank(to_tsvector('english', title), to_tsquery('english', 'SQL')) AS rank_title,
       ts_rank(to_tsvector('english', description), to_tsquery('english', 'SQL')) AS rank_description,
       similarity('SQL', title || ' ' || description) AS similarity
FROM articles
WHERE to_tsvector('english', title || ' ' || description) 
      @@ to_tsquery('english', 'SQL')
   OR similarity('SQL', title || ' ' || description) &amp;gt; 0.2
ORDER BY rank_title DESC, rank_description DESC, similarity DESC;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Before introducing countless tools to solve the full-text search problem, try using this in your application, as in the overwhelming majority of cases, it will solve the issues. With the use of GIN indexes and the pg_trgm extension, we achieve a fast and efficient search system. 🚀&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Sources&lt;/em&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://leandronsp.com/a-powerful-full-text-search-in-postgresql-in-less-than-20-lines" rel="noopener noreferrer"&gt;Article&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pganalyze.com/blog/gin-index" rel="noopener noreferrer"&gt;Understanding GIN Index&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pganalyze.com/blog/similarity-in-postgres-and-ruby-on-rails-using-trigrams#:~:text=The%20similarity%20function%20compares%20the,whether%20two%20strings%20are%20similar." rel="noopener noreferrer"&gt;Similarity in Postgres and Usign Rails migrations&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>postgres</category>
      <category>fulltex</category>
      <category>similarity</category>
      <category>sql</category>
    </item>
    <item>
      <title>Adding Redis easily with Docker</title>
      <dc:creator>Renan Maringolo</dc:creator>
      <pubDate>Thu, 15 Jul 2021 19:44:18 +0000</pubDate>
      <link>https://forem.com/renanmaringolo/adding-redis-easily-with-docker-158f</link>
      <guid>https://forem.com/renanmaringolo/adding-redis-easily-with-docker-158f</guid>
      <description>&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;In this chapter, i'll walk you through the installation, configuration and how to use Redis in a simple way. In this process, i'll use Docker to create a container with the Redis image.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Requirements
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Docker&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Installation &amp;amp; Configuration
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;In this first step, you need to install Docker on your machine by following the steps below.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;1.Update Software Repositories:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sudo apt-get update&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;2.Install some prerequisite packages that let &lt;code&gt;apt&lt;/code&gt; use packages over HTTPS:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sudo apt install apt-transport-https ca-certificates curl software-properties-common&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;3.So, add GPG Key to repository Docker on your SO:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;4.Add the repository Docker to fonts of the &lt;code&gt;APT&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;5.Again:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sudo apt update&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;6.Finally, install Docker:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sudo apt-get install docker-ce docker-ce-cli containerd.io&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Check if the Docker was installed correctly:&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;code&gt;sudo docker run hello-world&lt;/code&gt;
&lt;/h3&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Executing the command Docker without sudo:
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;sudo usermod -aG docker ${USER}&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Working with images from Docker
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Docker containers are built with Docker images. By default, it searches from Docker Hub, where the image is registered.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;code&gt;docker run --name redis-tutorial -p 6379:6379 -d redis&lt;/code&gt;
&lt;/h3&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;Explanation of this command. For the creation of a container, we need an image, if there is no image already created, the &lt;code&gt;run&lt;/code&gt; command will search the internet and download it to your host (which is your local machine) and automatically create a container. The &lt;code&gt;--name&lt;/code&gt; command allows you to name the container. The &lt;code&gt;-p&lt;/code&gt; command maps port 6379 on the container (right side) to port 6379 on the host (left side). The &lt;code&gt;-d&lt;/code&gt; command runs the container in the background. The last command is the name of the image it should fetch from the remote repository &lt;code&gt;redis&lt;/code&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Okay, if all went well, you should find your container. To do this is very simple! The &lt;code&gt;docker ps&lt;/code&gt; command lists all containers that are active, and you should have found something like this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;CONTAINER ID| IMAGE| COMMAND| CREATED| STATUS| PORTS| NAMES&lt;br&gt;
&lt;strong&gt;763fa8c3e7ca| redis| "docker-entrypoint.s…"| 2 hours ago| Up 2 hours| 0.0.0.0:6379-&amp;gt;6379/tcp, :::6379-&amp;gt;6379/tcp| redis-tutorial&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  In the first column is the Container ID, in the second column is image (redis, postgres, ruby...), in the third the command it runs in the background, in the fourth and fifth, the date the container was created and respectively the status in which he finds himself. Right at the end, we have the port and the connection protocol that was made and lastly you must remember, it is the name that we defined in the previous command.
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;Well, now that we have a container running in the background and we know its name, we need to go in, after all, the container is a separate process from the operating system and if we want to run things in there, we need to tell the docker that we want it, right ?&lt;br&gt;
The &lt;code&gt;exec&lt;/code&gt; command executes something inside the container and there are several modes and types of commands for this, but for now, let's just go into the container! Run:&lt;/p&gt;
&lt;h6&gt;
  
  
  &lt;code&gt;docker exec -it redis-tutorial bash&lt;/code&gt;
&lt;/h6&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;explanation of this command line: &lt;code&gt;exec&lt;/code&gt; allows you to execute commands in the container without the need to be inside it. &lt;code&gt;-it&lt;/code&gt; is a way to associate your terminal and interact with the container. &lt;code&gt;bash&lt;/code&gt; will cause us to enter the container's BASH.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Now that we are inside the container with a Redis image, we can run the Redis CLI:
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;redis-cli&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Try Redis commands, for example: &lt;code&gt;hmset student:xxx id 1 college nnn address xn&lt;/code&gt;. If the return is &lt;code&gt;OK&lt;/code&gt;, it means everything is fine and you can now play with Redis :)&lt;/p&gt;

</description>
      <category>redis</category>
      <category>docker</category>
      <category>database</category>
      <category>cache</category>
    </item>
  </channel>
</rss>
