{"id":1010,"date":"2021-03-28T23:55:53","date_gmt":"2021-03-28T21:55:53","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=1010"},"modified":"2022-09-02T09:51:09","modified_gmt":"2022-09-02T07:51:09","slug":"first-steps-with-docker","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2021\/03\/28\/first-steps-with-docker\/","title":{"rendered":"First steps with Docker"},"content":{"rendered":"\n<p>A&nbsp;<strong>Docker Container<\/strong>&nbsp;is an environment that contains an application, or multiple applications, and all the libraries, other applications and tooling they need to run.<\/p>\n\n\n\n<p>The application is encapsulated with its dependencies in a container. You can take a <span class=\"has-inline-color has-vivid-cyan-blue-color\">Docker Image<\/span>&nbsp;and create a container from it on a newly bought machine with Docker installed, and your application will work without additional work or dependencies.<\/p>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-red-color\">How to install Docker ?<\/span><\/h5>\n\n\n\n<p>The exact installation instructions depend on your operating system. The Docker Website contains detailed instructions for<\/p>\n\n\n\n<ul><li><a href=\"https:\/\/docs.docker.com\/docker-for-windows\/install\/\">Windows<\/a><\/li><li><a href=\"https:\/\/docs.docker.com\/engine\/install\/\">Linux<\/a><\/li><li><a href=\"https:\/\/docs.docker.com\/docker-for-mac\/install\/\">macOS<\/a><\/li><\/ul>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-red-color\">Launch your first container<\/span><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>docker container run &lt;options&gt; &lt;image&gt;<\/code><\/pre>\n\n\n\n<h5><br><span class=\"has-inline-color has-vivid-red-color\">Port mapping<\/span><\/h5>\n\n\n\n<p>When I run a web server like nginx for example, it will render web pages on port 80, but only inside the container. I wouldn&#8217;t have access to it because it&#8217;s totally isolated, the container has its own network. In order to be able to access the web pages, I will use the <strong>-p<\/strong> option which will allow me to specify the port of my machine and tell it to which port of the container I want to link. This way, I will be able to access the web pages through my browser.<\/p>\n\n\n\n<p>This is an example that opens port 80 of the container to port 9001 of my machine.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker container run -d -p 9001:80 nginx<\/code><\/pre>\n\n\n\n<p>And now if I go to the URL http: \/\/ localhost: 9001 \/ in my browser, I can see the nginx home page.<\/p>\n\n\n\n<h6><span class=\"has-inline-color has-vivid-purple-color\">CONTR\u00d4LE DES PORTS<\/span><\/h6>\n\n\n\n<p>With the following command, I can see a &#8220;PORTS&#8221; column that tells me which port on my machine is exposed to a container.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker container ls\n\n\/\/or \n\ndocker ps<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES\n828bbc8ba775        nginx               \"nginx -g 'daemon of\u2026\"   2 minutes ago       Up 2 minutes        0.0.0.0:9001-&gt;80\/<\/code><\/pre>\n\n\n\n<p>There is also a command to get port data for a specific container.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker port &lt;container_ID&gt;<\/code><\/pre>\n\n\n\n<p>The notation is reversed (compared to an ls) .<em> Example: 80\/tcp -&gt; 0.0.0.0:9001<\/em><\/p>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-red-color\">What is Dockerfile ? <\/span><\/h5>\n\n\n\n<p>A Dockerfile is a text file with instructions on how to build an image.<\/p>\n\n\n\n<p>Those instructions are part of a configuration language, which includes keywords like&nbsp;<code>FROM<\/code>,&nbsp;<code>LABEL<\/code>,&nbsp;<code>RUN<\/code>,&nbsp;<code>COPY<\/code>,&nbsp;<code>ENTRYPOINT<\/code>,&nbsp;<code>CMD<\/code>,&nbsp;<code>EXPOSE<\/code>,&nbsp;<code>ENV<\/code>&nbsp;and more.<\/p>\n\n\n\n<p>This is the workflow: first you create a Dockefile, then you built a Docker image from it using&nbsp;<code>docker build<\/code>, and finally you run a container from the image.<\/p>\n\n\n\n<h6><span class=\"has-inline-color has-vivid-purple-color\">Example:<\/span> Let\u2019s create our first Dockerfile<\/h6>\n\n\n\n<p>Let\u2019s say you have a folder with a simple Node.js app composed by an&nbsp;<code>app.js<\/code>, a&nbsp;<code>package.json<\/code>&nbsp;file that lists a couple dependencies you need to install before running the app, and&nbsp;<code>package-lock.json<\/code>.<\/p>\n\n\n\n<p>Inside it, create a plain text file named&nbsp;<code>Dockerfile<\/code>, with no extension, with this content:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>FROM node:14\nWORKDIR \/usr\/src\/app\nCOPY package*.json app.js .\/\nRUN npm install\nEXPOSE 3000\nCMD &#91;\"node\", \"app.js\"]<\/code><\/pre>\n\n\n\n<p>n the first line we say which image we want to start from. This will be our <span class=\"has-inline-color has-vivid-cyan-blue-color\">base image<\/span>. In this case it will take the official Node.js image, based on Alpine Linux, using Node 14. When creating a container from the Dockerfile, Docker will get that image from Docker Hub.<\/p>\n\n\n\n<p>Next we set the working directory to&nbsp;<code>\/usr\/src\/app<\/code>, which means all our commands will be run in that folder until we change it again. That\u2019s a folder we know already exists in the Node image.<\/p>\n\n\n\n<p>We copy the&nbsp;<code>package.json<\/code>,&nbsp;<code>package-lock.json<\/code>&nbsp;(using the&nbsp;<code>*<\/code>&nbsp;wildcard) and&nbsp;<code>app.js<\/code>&nbsp;files that are present in the current folder, into the working directory.<\/p>\n\n\n\n<p>We run&nbsp;<code>npm install<\/code>&nbsp;to install the packages listed in the&nbsp;<code>package.json<\/code>&nbsp;file.<\/p>\n\n\n\n<p>Then we expose port 3000 to the outside, since that\u2019s what our app runs on. A container is 100% isolated from the network unless you expose one of its ports using the&nbsp;<code>EXPOSE<\/code>&nbsp;command. <\/p>\n\n\n\n<p>Finally we run&nbsp;<code>node app.js<\/code>&nbsp;to start the app<\/p>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-red-color\">&nbsp;How to create a container from a DockerFile ? <\/span><\/h5>\n\n\n\n<p>Open a terminal and go to the&nbsp;<code>app<\/code>&nbsp;directory with the&nbsp;<code>Dockerfile<\/code>. Now build the container image using the<strong>&nbsp;<code>docker build<\/code><\/strong>&nbsp;command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> docker build -t &lt;my-customize-container-name&gt; .<\/code><\/pre>\n\n\n\n<p>This command used the Dockerfile to build a new container image. <\/p>\n\n\n\n<p>&nbsp;the&nbsp;<code><span class=\"has-inline-color has-vivid-red-color\">-t<\/span><\/code>&nbsp;flag tags our image. Think of this simply as a human-readable name for the final image.<\/p>\n\n\n\n<p>The&nbsp;<code>.<\/code>&nbsp;at the end of the&nbsp;<code>docker build<\/code>&nbsp;command tells that Docker should look for the&nbsp;<code>Dockerfile<\/code>&nbsp;in the current directory.<\/p>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-red-color\">How to get Log-info about specific service in docker?<\/span><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>docker logs -f &lt;service&gt;<\/code><\/pre>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-red-color\">Restart a service in docker<\/span><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>docker restart &lt;service&gt;<\/code><\/pre>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-red-color\">Check Docker version<\/span><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>$ docker version <\/code><\/pre>\n\n\n\n<p>It shows docker version for both client and server. As given in the following image.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/04\/image-32.png\" alt=\"\" class=\"wp-image-1854\" width=\"592\" height=\"402\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/04\/image-32.png 749w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/04\/image-32-300x204.png 300w\" sizes=\"(max-width: 592px) 100vw, 592px\" \/><\/figure>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-red-color\">Build Docker Image from a Dockerfile<\/span><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>$ docker build -t image-name docker-file-location  <\/code><\/pre>\n\n\n\n<p><strong>-t&nbsp;<\/strong>: it is used to tag Docker image with the provided name.<\/p>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-red-color\">Run Docker Image<\/span><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>$ docker run -d image-name  <\/code><\/pre>\n\n\n\n<p><strong>-d<\/strong>&nbsp;: It is used to create a daemon process.<\/p>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-red-color\">Check available Docker images<\/span><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>$ docker images  <\/code><\/pre>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-red-color\">Remove All Docker Images<\/span><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>docker rmi $(docker images -q)<\/code><\/pre>\n\n\n\n<p><strong>How It Works ? <\/strong>:  <code>docker images -q<\/code>\u00a0will list all image IDs. We pass these IDs to\u00a0<code>docker rmi<\/code>\u00a0(which stands for remove images) and we therefore remove all the images.<\/p>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-red-color\">Check for latest running container<\/span><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>$ docker ps -l  <\/code><\/pre>\n\n\n\n<p><strong>-l<\/strong>&nbsp;: it is used to show latest available container.<\/p>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-red-color\">Check all running containers<\/span><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>$ docker ps -a  <\/code><\/pre>\n\n\n\n<p><strong>-a<\/strong>&nbsp;: It is used to show all available containers.<\/p>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-red-color\">Stop running container<\/span><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>$ docker stop container_id  <\/code><\/pre>\n\n\n\n<p><strong>container_id<\/strong>\u00a0: It is an Id assigned by the Docker to the container.<\/p>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-red-color\">Stop All Docker Containers<\/span><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>docker kill $(docker ps -q)<\/code><\/pre>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-red-color\">Remove All Docker Containers<\/span><\/h5>\n\n\n\n<p>If you don\u2019t just want to stop containers and you\u2019d like to go a step further and remove them, simply run the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker rm $(docker ps -a -q)<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-red-color\">Delete an image<\/span><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>$ docker rmi image-name  <\/code><\/pre>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-red-color\">Delete all images<\/span><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>$ docker rmi -f $(docker images -q)  <\/code><\/pre>\n\n\n\n<p><strong>&#8211;<\/strong>f&nbsp;: It is used to delete image forcefully.<\/p>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-red-color\">Delete all containers<\/span><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>$ docker rm $(docker ps -a -q)  <\/code><\/pre>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-red-color\">Enter into Docker container<\/span><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>$ docker exec -it container-id bash  <\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h5>Reference: <\/h5>\n\n\n\n<p><a href=\"https:\/\/flaviocopes.com\/\">https:\/\/flaviocopes.com\/<\/a><\/p>\n\n\n\n<p><span class=\"has-inline-color has-vivid-cyan-blue-color\"><a href=\"https:\/\/nouslesdevs.com\">https:\/\/nouslesdevs.com<\/a><\/span><\/p>\n\n\n\n<p><a href=\"\"><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A&nbsp;Docker Container&nbsp;is an environment that contains an application, or multiple applications, and all the libraries, other applications and tooling they need to run. The application is encapsulated with its dependencies in a container. You can take a Docker Image&nbsp;and create a container from it on a newly bought machine with Docker installed, and your application [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1019,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[208],"tags":[180,259,266],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1010"}],"collection":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=1010"}],"version-history":[{"count":11,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1010\/revisions"}],"predecessor-version":[{"id":2155,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1010\/revisions\/2155"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/1019"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=1010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=1010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=1010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}