<?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>DEV Community: David Barenco</title>
    <description>The latest articles on DEV Community by David Barenco (@daveloper).</description>
    <link>https://dev.to/daveloper</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%2F501802%2Ff85ef298-5e7c-4738-bd8d-9b1d9162b6e6.jpeg</url>
      <title>DEV Community: David Barenco</title>
      <link>https://dev.to/daveloper</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/daveloper"/>
    <language>en</language>
    <item>
      <title>Docker do Zero</title>
      <dc:creator>David Barenco</dc:creator>
      <pubDate>Tue, 23 Jul 2024 20:11:10 +0000</pubDate>
      <link>https://dev.to/daveloper/docker-do-zero-2180</link>
      <guid>https://dev.to/daveloper/docker-do-zero-2180</guid>
      <description>&lt;p&gt;Quem nunca ouviu a frase: "Na minha máquina funciona...". Esse é (ou era) um problema comum aos desenvolvedores, pois cada máquina tem configurações diferentes e na hora de rodar a aplicação, muitas variáveis podiam levar ela a falhar. Dessa forma, o Docker surge como uma forma de padronizar o ambiente de desenvolvimento para, com poucos comandos, subirmos a aplicação inteira, com suas dependências e conexões.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3uyp736s3qk0hv7g7l6a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3uyp736s3qk0hv7g7l6a.png" alt="Meme about docker" width="422" height="585"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;De início pode parecer complicado, mas entendendo alguns conceitos e comandos, rapidamente conseguimos extrair o melhor que o Docker tem a oferecer e facilitar muito nossa vida. &lt;/p&gt;

&lt;p&gt;O docker roda uma máquina linux virtualmente , então é recomendado conhecer um pouco de Linux e comandos do terminal.&lt;/p&gt;

&lt;p&gt;Mas antes de aprender a criar ou executar os comandos, vamos entender alguns conceitos iniciais:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faqfyudy7d5w4o8twqv71.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faqfyudy7d5w4o8twqv71.gif" alt="Gif basic stuff" width="220" height="194"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Construindo uma casa
&lt;/h2&gt;

&lt;p&gt;Pra entender o funcionamento do Docker na nossa máquina e seu uso, eu gosto de usar a analogia de quando construímos uma casa. No caso, vamos pensar que temos um terreno que é um condomínio de casas, e lá vamos construir várias casas. As casa terão um padrão pra estrutura principal, e depois poderemos personalizar cada uma com móveis e etc. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxd208l8wce9fzfz3y2d4.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxd208l8wce9fzfz3y2d4.gif" alt="tom and jerry house build" width="220" height="164"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Primeiramente precisamos pensar no tamanho da nossa casa, as paredes, portas e janelas. Onde ficarão a parte hidráulica e elétrica. &lt;/li&gt;
&lt;li&gt;Após isso definido, passamos pro arquiteto ou engenheiro construir a planta das casas. &lt;/li&gt;
&lt;li&gt;Com isso, podemos construir uma ou várias casa baseadas na planta da casa.&lt;/li&gt;
&lt;li&gt;E depois podemos entrar em cada casa e colocar o que quisermos dentro. Até modificar uma porta ou janela. Enfim, trabalhar cada casa individualmente. Podemos também demolir uma casa e construir outra no mesmo local, com base na planta inicial.&lt;/li&gt;
&lt;li&gt;Uma questão além disso são as contas (luz, água). Vamos supor que queremos sempre ter o total de quanto estamos gastando no condomínio. Se demolirmos uma casa, não podemos perder esse valor. Então precisamos ter um local para armazenar as contas fora de cada casa, além de ter uma cópia na própria casa.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Agora que temos uma ideia dos passos hipotéticos pra construir nosso condomínio de casas, vamos relacioná-los a subir uma aplicação com Docker.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Primeiro, definimos os passos pra construir uma &lt;em&gt;image&lt;/em&gt;, ou seja, a planta da casa. Esses passos ficam salvos num &lt;em&gt;Dockerfile&lt;/em&gt;, um arquivo onde fica definido o que vamos precisar subir na(s) máquina(s) que vamos subir.&lt;/li&gt;
&lt;li&gt;Desse arquivo, buildamos a &lt;em&gt;image&lt;/em&gt;, a planta da nossa casa. Uma &lt;em&gt;image&lt;/em&gt; é basicamente um template pra criação de &lt;em&gt;containers&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Já os &lt;em&gt;containers&lt;/em&gt; são nossas casas. Eles são instâncias de uma máquina virtual rodando na nossa máquina, com as configurações especificadas na nossa &lt;em&gt;image&lt;/em&gt;. Podemos subir um &lt;em&gt;container&lt;/em&gt; e acessá-lo através do terminal. Lá, podemos modificá-lo como quisermos, instalando novas aplicações ou bibliotecas, executar aplicações, criar banco de dados, etc.&lt;/li&gt;
&lt;li&gt;Por fim, podemos remover um &lt;em&gt;container&lt;/em&gt;. Isso irá apagar qualquer arquivo ou instalação que tivermos feito dentro dele. Ao subirmos um novo &lt;em&gt;container&lt;/em&gt;, teremos as especificações iniciais definidas na nossa &lt;em&gt;image&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;E se quisermos manter dados do nosso &lt;em&gt;container&lt;/em&gt; pro próximo? Podemos utilizar os &lt;em&gt;volumes&lt;/em&gt;, para isso. Como a contas da casa, precisamos salvar as informações fora do container para persistí-los. Dessa forma, fazemos uma "ponte" entre nossa máquina e o &lt;em&gt;container&lt;/em&gt;, copiando tudo de um pro outro. Ao removermos os &lt;em&gt;containers&lt;/em&gt;, mantemos os dados na nossa máquina e podemos usá-los novamente.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Fluxo de trabalho
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz99zrykexkqobbx73qf0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz99zrykexkqobbx73qf0.png" alt="docker workflow" width="800" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Depois de entender esses conceitos, fica mais fácil entender um fluxo de trabalho utilizando Docker. Os comandos abaixo estão melhor descritos no fim desse post.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Primeiramente, precisamos de uma image pra trabalhar. Podemos criar um Dockerfile para buildar (&lt;code&gt;docker build&lt;/code&gt;) uma ou baixar uma do Docker Hub (&lt;code&gt;docker pull&lt;/code&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Após termos a image (&lt;code&gt;docker images&lt;/code&gt;), podemos executá-la (&lt;code&gt;docker run&lt;/code&gt;) e gerar um container, uma máquina virtual com as configurações definidas na image.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Container pode executar uma vez e parar, ou ficar rodando na nossa máquina (&lt;code&gt;docker ps&lt;/code&gt;). Podemos acessar o container (&lt;code&gt;docker exec&lt;/code&gt;) e rodar comandos dentro dele (lembrando que qualquer alteração será perdida quando matarmos o container).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Após utilizar, podemos matar o container (&lt;code&gt;docker stop&lt;/code&gt;) (&lt;code&gt;docker rm&lt;/code&gt;). E caso desejamos, também remover a image (&lt;code&gt;docker rmi&lt;/code&gt;), se não formos mais utilizá-la para criar novos containers.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Criando um Dockerfile
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3y79v9kuxpdf2752u02b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3y79v9kuxpdf2752u02b.png" alt="Código básico de um Dockerfile" width="800" height="206"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ao criamos um arquivo, como o exemplo acima, podemos buildar o &lt;em&gt;Dockerfile&lt;/em&gt; numa image, rodando &lt;code&gt;docker build -t &amp;lt;NOME-DA-IMAGEM&amp;gt;:&amp;lt;VERSÃO&amp;gt; &amp;lt;DIRETÓRIO-DO-DOCKERFILE&amp;gt;&lt;/code&gt;. Isso cria a image com os comandos e instalações especificadas no Dockerfile já executados. &lt;/p&gt;

&lt;p&gt;A partir daí, podemos definir os passos para configurar a image com as configurações iniciais. Sempre usamos uma image como base com o comando &lt;code&gt;FROM&lt;/code&gt;. O &lt;code&gt;RUN&lt;/code&gt;, por exemplo, executa os comandos. &lt;/p&gt;

&lt;h3&gt;
  
  
  Principais instruções do Dockerfile
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;FROM&lt;/code&gt;: image que vamos utilizar de base.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;WORKDIR&lt;/code&gt;: Define a pasta que vamos executar os comandos no container.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;RUN&lt;/code&gt;: Roda os comandos especificados.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;COPY&lt;/code&gt;: Copia arquivos da nossa máquina pro image.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ENTRYPOINT&lt;/code&gt;: Comando a ser executado quando inicializamos o container.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Algumas outras podem ser conferidas &lt;a href="https://docs.docker.com/reference/dockerfile/" rel="noopener noreferrer"&gt;aqui&lt;/a&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Principais comandos Docker que utilizo
&lt;/h2&gt;

&lt;p&gt;Abaixo, separei algumas das principais funcionalidades que utilizo no dia-a-dia.&lt;/p&gt;

&lt;h3&gt;
  
  
  Utilizando Imagens
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;docker pull &amp;lt;NOME-DA-IMAGEM&amp;gt;:&amp;lt;VERSÃO&amp;gt;&lt;/code&gt;: Puxa a image pra nossa máquina local. Se a versão não for passada, se utiliza a &lt;em&gt;latest&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;docker build &amp;lt;CAMINHO-DO-DOCKERFILE&amp;gt;&lt;/code&gt;: Buildamos em uma image o Dockerfile

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-t &amp;lt;NOME-DA-IMAGEM&amp;gt;:&amp;lt;VERSÃO&amp;gt;&lt;/code&gt;: Criamos um nome da image. É interessante utilizar o nome do nosso usuário no Docker Hub seguido de / e um nome da image: &lt;code&gt;user/project-abc:v1&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;
&lt;code&gt;docker images&lt;/code&gt;: Lista as imagens baixadas na máquina.&lt;/li&gt;

&lt;li&gt;
&lt;code&gt;docker rmi &amp;lt;IMAGE-ID&amp;gt;&lt;/code&gt;: Remove a image.&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Listagem e remoção de containers.
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;docker ps&lt;/code&gt;: Lista os containers que estão rodando.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-a&lt;/code&gt;: Mostra os containers ativos ou que já foram executados.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-q&lt;/code&gt;: Lista os ids dos containers.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;code&gt;docker stop &amp;lt;ID-DO-CONTAINER&amp;gt;&lt;/code&gt;: Para o container que esteja rodando.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;code&gt;docker rm &amp;lt;ID-DO-CONTAINER&amp;gt;&lt;/code&gt;: Remove o container que está parado.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-f&lt;/code&gt;: Força a remoção, mesmo se o container estiver rodando.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Para remover todos os containers ativos e não ativos:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;docker rm -f $(docker ps -aq)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Manipulando containers
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;docker run &amp;lt;IMAGE&amp;gt; &amp;lt;COMANDO&amp;gt;&lt;/code&gt;: Roda a image em nosso terminal com o comando informado. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-i&lt;/code&gt;: Modulo interativo, mantendo o processo rodando.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-t&lt;/code&gt;: Permite digitar coisas no terminal.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-p&lt;/code&gt;: Aponta para porta a ser mapeada.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-d&lt;/code&gt;: Desatacha o terminal do processo.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-v&lt;/code&gt;: Volume para persistência de dados. Passamos o caminho local, com ':' separando o caminho no container. As mudanças na pasta do container persistirão na máquina local.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--rm&lt;/code&gt;: Remove o container após terminar de executá-lo.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--name&lt;/code&gt;: Define um nome para o container.&lt;/li&gt;
&lt;li&gt;Exemplo: &lt;code&gt;docker run -it ubuntu bash&lt;/code&gt; rodamos uma image do Ubuntu e executamos o bash, permitindo-nos executar códigos no terminal dentro do container.&lt;/li&gt;
&lt;li&gt;Exemplo: &lt;code&gt;docker run -d -p 8080:80 nginx&lt;/code&gt; rodamos o nginx e mapeamos a porta 8080 da nossa máquina pra porta 80 do container.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;code&gt;docker exec &amp;lt;NOME-DO-CONTAINER&amp;gt; &amp;lt;COMANDO&amp;gt;&lt;/code&gt;: Executa um comando no nosso container já ativo. (Podemos usar as mesmas flags do &lt;code&gt;docker run&lt;/code&gt;, como por exemplo, a &lt;code&gt;-it&lt;/code&gt;)&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Trabalhando com volumes
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;docker volume ls&lt;/code&gt;: Lista volumes.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;docker volume create &amp;lt;NOME-DO-VOLUME&amp;gt;&lt;/code&gt;: Cria um volume.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;docker volume inspect &amp;lt;NOME-DO-VOLUME&amp;gt;&lt;/code&gt;: Mostra dados do volume.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;docker volume prune&lt;/code&gt;: Remove os volumes e libera espaço.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Com isso, podemos criar um volume passando a flag &lt;code&gt;--mount&lt;/code&gt; com o nome do volume criado. Exemplo:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;docker run -d -p 8080:80 --mount type=volume,source=&amp;lt;NOME-DO-VOLUME&amp;gt;,target=/app nginx&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Ou com a flag &lt;code&gt;-v&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;docker run -d -p 8080:80 -v &amp;lt;NOME-DO-VOLUME&amp;gt;:/app nginx&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Considerações
&lt;/h2&gt;

&lt;p&gt;Tentei resumir bem os conceitos e principais comandos que uso do Docker. Vale a pena se aprofundar e praticar subindo imagens para uso pessoal. Outro ponto é estudar sobre &lt;em&gt;Docker Compose&lt;/em&gt;, que orquestra múltiplos &lt;em&gt;containers&lt;/em&gt; Docker para funcionarem juntos.&lt;br&gt;
Qualquer dúvida ou comentário, escreve aqui em baixo que ficarei feliz em responder.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Docker from Zero</title>
      <dc:creator>David Barenco</dc:creator>
      <pubDate>Tue, 23 Jul 2024 20:11:05 +0000</pubDate>
      <link>https://dev.to/daveloper/docker-from-zero-4fci</link>
      <guid>https://dev.to/daveloper/docker-from-zero-4fci</guid>
      <description>&lt;p&gt;Who has never heard the phrase: "It works on my machine...". This is (or was) a common problem for developers, as each machine has different configurations and when running the application, many variables could cause its failure. In this way, Docker appears as a way of standardizing the development environment so that, with just a few commands, we can run the entire application, with its dependencies and connections.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3uyp736s3qk0hv7g7l6a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3uyp736s3qk0hv7g7l6a.png" alt="Meme about docker" width="422" height="585"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It may seem complicated at first, but understanding some concepts and commands, we can quickly extract the best that Docker has to offer and make our lives much easier.&lt;/p&gt;

&lt;p&gt;Docker runs a virtual Linux machine, so it's recommended to know a little about Linux and terminal commands.&lt;/p&gt;

&lt;p&gt;But before learning how to create or execute commands, let's understand some initial concepts:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faqfyudy7d5w4o8twqv71.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faqfyudy7d5w4o8twqv71.gif" alt="Gif basic stuff" width="220" height="194"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Building a house
&lt;/h2&gt;

&lt;p&gt;To understand how Docker works on our machine and its use, I like to use the analogy of when we're building a house. In this case, let's think that we have a piece of land that is a condominium of houses, and we are going to build several houses there. The houses will have a standard for the main structure, and then we can customize each one with furniture, etc. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxd208l8wce9fzfz3y2d4.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxd208l8wce9fzfz3y2d4.gif" alt="tom and jerry house build" width="220" height="164"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Firstly we need to think about the size of our house, the walls, doors and windows. Where the hydraulic and electrical parts will be located. &lt;/li&gt;
&lt;li&gt;After this is defined, we pass it on to the architect or engineer to build the house blueprint. &lt;/li&gt;
&lt;li&gt;With this, we can build one or several houses based on the house blueprint.&lt;/li&gt;
&lt;li&gt;And then we can enter each house and put whatever we want inside. Even modify a door or window, work on each house individually. We can also demolish a house and build another in the same location, based on the initial blueprint.&lt;/li&gt;
&lt;li&gt;Another issue is the bills (electricity, water). Let's assume that we always want to have a total of how much we are spending on the condominium. If we demolish a house, we cannot lose that value. So we need to have a place to store the bills outside each house, in addition to having a copy in the house itself.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now that we have an idea of ​​the hypothetical steps to build our condominium of houses, let's relate them to running an application with Docker.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First, we define the steps to build an &lt;em&gt;image&lt;/em&gt;, that is, the house blueprint. These steps are saved in a &lt;em&gt;Dockerfile&lt;/em&gt;, a file where is defined what we will need to have on the machine(s) we are going to run .&lt;/li&gt;
&lt;li&gt;From this file, we built the &lt;em&gt;image&lt;/em&gt;, the blueprint of our house. An &lt;em&gt;image&lt;/em&gt; is basically a template for creating &lt;em&gt;containers&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;The &lt;em&gt;containers&lt;/em&gt; are our homes. They are instances of a virtual machine running on our machine, with the settings specified in our &lt;em&gt;image&lt;/em&gt;. We can upload a &lt;em&gt;container&lt;/em&gt; and access it through the terminal. There, we can modify it as we want, installing new applications or libraries, running applications, creating a database, etc.&lt;/li&gt;
&lt;li&gt;Finally, we can remove a &lt;em&gt;container&lt;/em&gt;. This will erase any files or installations we have made inside it. When we upload a new &lt;em&gt;container&lt;/em&gt;, we will have the initial specifications defined in our &lt;em&gt;image&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;What if we want to keep data from our &lt;em&gt;container&lt;/em&gt; to the next one? We can use &lt;em&gt;volumes&lt;/em&gt; for this. Like house bills, we need to save the information outside the container to persist it. This way, we create a "bridge" between our machine and the &lt;em&gt;container&lt;/em&gt;, copying everything from one to the other. By removing the &lt;em&gt;containers&lt;/em&gt;, we keep the data on our machine and can use it again.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Workflow
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz99zrykexkqobbx73qf0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz99zrykexkqobbx73qf0.png" alt="docker workflow" width="800" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once you understand these concepts, it becomes easier to understand a workflow using Docker. The commands below are better described at the end of this post.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Firstly, we need an image to work with. We can create a Dockerfile to build (&lt;code&gt;docker build&lt;/code&gt;) one or download one from Docker Hub (&lt;code&gt;docker pull&lt;/code&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After we have the image (&lt;code&gt;docker images&lt;/code&gt;), we can run it (&lt;code&gt;docker run&lt;/code&gt;) and generate a container, a virtual machine with the settings defined in the image.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Container can run once and stop, or keep running on our machine (&lt;code&gt;docker ps&lt;/code&gt;). We can access the container (&lt;code&gt;docker exec&lt;/code&gt;) and run commands inside it (remembering that any changes will be lost when we kill the container).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After using the container, we can kill it (&lt;code&gt;docker stop&lt;/code&gt;) (&lt;code&gt;docker rm&lt;/code&gt;). And if we wish, we can also remove the image (&lt;code&gt;docker rmi&lt;/code&gt;), if we are no longer going to use it to create new containers.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Creating a Dockerfile
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3y79v9kuxpdf2752u02b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3y79v9kuxpdf2752u02b.png" alt="Código básico de um Dockerfile" width="800" height="206"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When creating a file, like the example above, we can build the &lt;em&gt;Dockerfile&lt;/em&gt; into an image, running &lt;code&gt;docker build -t &amp;lt;IMAGE-NAME&amp;gt;:&amp;lt;VERSION&amp;gt; &amp;lt;DOCKERFILE-DIRECTORY&amp;gt;&lt;/code&gt;. This creates the image with the commands and installations specified in the Dockerfile already executed. &lt;/p&gt;

&lt;p&gt;From there, we can define the steps to configure the image with the initial settings. We always use an image as a base with the &lt;code&gt;FROM&lt;/code&gt; command. &lt;code&gt;RUN&lt;/code&gt;, for example, executes the commands.&lt;/p&gt;

&lt;h3&gt;
  
  
  Dockerfile main instructions
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;FROM&lt;/code&gt;: image that we will use as base.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;WORKDIR&lt;/code&gt;: Defines the folder where we will execute the commands in the container.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;RUN&lt;/code&gt;: Runs the specified commands.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;COPY&lt;/code&gt;: Copies files from our pro image machine.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ENTRYPOINT&lt;/code&gt;: Command to be executed when we initialize the container.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Some others can be seen &lt;a href="https://docs.docker.com/reference/dockerfile/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Main Docker commands I use
&lt;/h2&gt;

&lt;p&gt;Below, I have separated some of the main features that I use on a daily basis.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using Images
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;docker pull &amp;lt;IMAGE-NAME&amp;gt;:&amp;lt;VERSION&amp;gt;&lt;/code&gt;: Pulls the image to our local machine. If the version is not passed, &lt;em&gt;latest&lt;/em&gt; is used.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;docker build &amp;lt;DOCKERFILE-PATH&amp;gt;&lt;/code&gt;: We build the Dockerfile in an image

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-t &amp;lt;IMAGE-NAME&amp;gt;:&amp;lt;VERSION&amp;gt;&lt;/code&gt;: We create an image name. It is interesting to use our Docker Hub user name followed by / and an image name: &lt;code&gt;user/project-abc:v1&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;
&lt;code&gt;docker images&lt;/code&gt;: Lists the images downloaded to the machine.&lt;/li&gt;

&lt;li&gt;
&lt;code&gt;docker rmi &amp;lt;IMAGE-ID&amp;gt;&lt;/code&gt;: Removes the image.&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Listing and removal of containers.
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;docker ps&lt;/code&gt;: Lists the containers that are running.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-a&lt;/code&gt;: Shows active containers or those that have already been run.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-q&lt;/code&gt;: Lists container ids.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;code&gt;docker stop &amp;lt;CONTAINER-ID&amp;gt;&lt;/code&gt;: Stops the container that is running.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;code&gt;docker rm &amp;lt;CONTAINER-ID&amp;gt;&lt;/code&gt;: Removes the stopped container.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-f&lt;/code&gt;: Forces removal, even if the container is running.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;To remove all active and non-active containers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;docker rm -f $(docker ps -aq)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Manipulating containers
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;docker run &amp;lt;IMAGE&amp;gt; &amp;lt;COMMAND&amp;gt;&lt;/code&gt;: Run the image in our terminal with the given command. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-i&lt;/code&gt;: Interactive module, keeping the process running.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-t&lt;/code&gt;: Allows you to type things in the terminal.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-p&lt;/code&gt;: Points to the port to be mapped.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-d&lt;/code&gt;: Detaches the terminal from the process.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-v&lt;/code&gt;: Volume for data persistence. We pass the local path, with ':' separating the path in the container. Changes to the container folder will persist on the local machine.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--rm&lt;/code&gt;: Removes the container after finishing running it.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--name&lt;/code&gt;: Defines a name for the container.&lt;/li&gt;
&lt;li&gt;Example: &lt;code&gt;docker run -it ubuntu bash&lt;/code&gt; we run an Ubuntu image and run bash, allowing us to execute code in the terminal inside the container.&lt;/li&gt;
&lt;li&gt;Example: &lt;code&gt;docker run -d -p 8080:80 nginx&lt;/code&gt; we run nginx and map port 8080 on our machine to port 80 on the container.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;code&gt;docker exec &amp;lt;CONTAINER-NAME&amp;gt; &amp;lt;COMMAND&amp;gt;&lt;/code&gt;: Executes a command in our already active container. (We can use the same flags as &lt;code&gt;docker run&lt;/code&gt;, such as &lt;code&gt;-it&lt;/code&gt;)&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Working with volumes
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;docker volume ls&lt;/code&gt;: List volumes.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;docker volume create &amp;lt;VOLUME-NAME&amp;gt;&lt;/code&gt;: Creates a volume.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;docker volume inspect &amp;lt;VOLUME-NAME&amp;gt;&lt;/code&gt;: Shows volume data.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;docker volume prune&lt;/code&gt;: Removes volumes and frees up space.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With this, we can create a volume by passing the &lt;code&gt;--mount&lt;/code&gt; flag with the name of the volume created. Example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;docker run -d -p 8080:80 --mount type=volume,source=&amp;lt;VOLUME-NAME&amp;gt;,target=/app nginx&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Or with the &lt;code&gt;-v&lt;/code&gt; flag:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;docker run -d -p 8080:80 -v &amp;lt;VOLUME-NAME&amp;gt;:/app nginx&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Considerations
&lt;/h2&gt;

&lt;p&gt;I tried to summarize the concepts and main commands I use in Docker. It's worth delving deeper and practicing uploading images for personal use. Another point is to study &lt;em&gt;Docker Compose&lt;/em&gt;, which orchestrates multiple Docker &lt;em&gt;containers&lt;/em&gt; to work together.&lt;br&gt;
If you have any questions or comments, write them below and I will be happy to answer them.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
