developer’s

DOCKER I

An introductory guide to docker for developers

Jayvishaalj
FAUN — Developer Community 🐾
8 min readFeb 2, 2021

--

docker

What is Docker and Why Should you use it?

memegenerator.net

If you are a developer or a DevOps person then you may have spent a lot of time and may have come across various errors while setting up a development or production environment. This becomes a more tedious job when it has to be set up across various systems running different operating systems as you may run into different kinds of errors in each OS. The next possible solution that most of you come up with is to use VMs(Virtual Machines), yes it is one of the possible solutions but what if each of your apps requires a different OS, again you will have to set up more number of VMs which will require you to install the respective OS on each of those instances. So, again even this way becomes a tiresome one.

Virtual Machine’s Architecture

So, What’s the solution?

Here comes the Docker into play. Docker is a set of platform as a service (PaaS) products that use OS-level virtualization to deliver software in packages called Containers.

Containers are isolated from one another and bundle their software, libraries, and configuration files, they communicate with each other through well-defined channels.

Docker’s Architecture

Docker container image is a lightweight, stand-alone, executable package of software that includes everything needed to run an application, ie., code, runtime, system tools, system libraries, and settings.

Docker is a platform for developers, sysadmins, and DevOps person to develop, deploy, and run applications with containers. This is often described as Containerization. Here in docker, you can develop many applications without the need to set up the VMs. By using docker you need to set up the environment only once. You can then convert the Setting of a container to an image and use it on any OS having a docker installed in it.

Now let’s get into action and get our hands dirty with docker

Now that we have an idea about what docker does, let’s dive deeper! To get started we need to install docker-ce in our system. Docker CE is available for all major Operating Systems including macOS, Windows, and Linux. The specific steps needed to install Docker CE on your system can be found at https://docs.docker.com/install/.

Once the docker is installed you can easily start a docker container. If you need more information about any command discussed below, just jump down to the bottom of the page to see the cheat sheet and links to the official Docker documentation.

Once Docker is installed and running on your system, we’re able to start by entering the following command on the terminal:

$ docker

This command will display a list with all options available for the docker command along with a description. As output you should be able to see something similar to the following:

docker options

Next, use the following commands to check for the installed Docker version. The below command shows just the version and build of the docker ce installed.

$ docker -version

The next command shows all the info of the docker ie., both the client and the docker Engine version.

$ docker version

Running First Container

Now that the docker is up and running we’re ready to select an image so that we can run our first container. To run a container instance of a pre-existing image, select an image from the list of Docker images in the Docker Hub.

Let’s say that we’d first like to run a Nodejs Express web server. Type in the search term “node”. A result list will be presented:

Click on the node image to be able to see the details page, there we can see the “docker pull node” command, we will be using this command to pull the docker image:

Let’s pull the Node image into our local docker engine. To pull the image run the below command:

$ docker pull node

pulling node image to the local docker host

This will pull the node image from the docker hub and store it in our docker engine. Now if you enter the “docker images” it will list the node image.

$ docker images

Let’s then run a container with a node image that we just pulled.

$ docker run -it -p 3000:3000 node bash

The docker run command lets you run any Docker image as a container. In this example we’re using the following options :

  • -it : executes the container in interactive mode (give us access to its terminal).
  • -p 3000:3000: by using the -p option we’re connecting the internal port 3000 of the container to the external port 3000. We’re then able to send an HTTP request to the server from our local machine by opening up URL http://dockerIP:3000. It’s also possible to connect an internal port to any other external port, e.g. connecting internal port 3000 to external port 8000 (-p 8000:3000). In this case, we need to access http://localhost:8000.
  • node it is the name of the image that our container should use.
  • bash whatever we specify right next to the image name is considered as a command that needs to be executed once the container starts, here we are asking the container to run the bash command, so that we can access the terminal of the container.

Now since we have got access to bash inside the node container, let’s check for the version of the node and npm which comes pre-installed in this node image that we pulled. Type in the following command:

# node -v

# npm -v

This returns an output of something similar to the below image:

node and npm version

Now let’s install a code editor for us to enter the code into the container. Let’s install the Vim editor. Execute the following commands for installing vim.

# apt-get update

# apt-get install vim

You should see vim getting installed as in the following screen:

Vim is installed

Let’s cd into /usr/src/app directory by using the following command:

# cd /usr/src/app

now let’s initialize npm and install express, to run an express node server.

# npm init -y

# npm install express

Express installed

Now create a server.js file using vim and enter the code as mentioned below. To create a file enter the bellow command in the shell.

# vim server.js

Once vim gets opened, press I to enter into insert mode. Then enter the following code into the editor:

const express = require('express');const app = express();app.use('/',(req,res,next) => {return res.status(200).send("WELCOME TO NODE JS EXPRESS SERVER");});app.listen(3000,'0.0.0.0',() => {console.log("SERVER IS UP AND RUNNING ON PORT 3000");});

Now press the escape button to exit from editing mode and type:wq to save and exit the Vim editor. To check if our server.js file has been created type in the following command and you should see your code.

# cat server.js

server.js file

Let’s now run this server code in our container. Type in the following command to run the code.

# node server.js

The response which is displayed in the browser when accessing the container by URL http://localhost:3000 should look like what you can see in the following screenshot:

The output of our server

If it doesn’t show up in the localhost don’t worry it will be running in the host machine’s IP address. The IP address of the latest machines will be either 192.168.99.100 or 192.168.99.101.

Stopping the Container

To stop the server press ctrl + c and then enter exit to exit the container. To see all the containers enter the following command.

# docker ps -a

The output of the above command will be similar to the below image:

List of all docker containers

# docker rm [CONTAINER_ID]

This command is used to delete the stopped container. If you try to delete an unstopped container it will return an error.

docker container removed

Running Containers In Detached Mode

So far, we’ve started our container in interactive mode. Containers can also be started in the detached mode which means that the containers can run in the background (in not blocking the terminal instance). To start a container in the detached mode we need to use the command line option -d instead of option -it as you can see in the following:

$ docker run -d -p 80:80 ngnix

In the above command, we are using the Nginx image instead of the node image, which is also available in the docker hub. Again, you’re able to check if the container is running by using the command docker ps. To stop the detached container you need to use the following command:

$ docker stop [CONTAINER_ID]

You can also get an overview of containers that have already been stopped by using the command:

# docker ps -a

remove docker

Jay Vishaal J

LinkedIn | Github

Thank you :)

👋 Join FAUN today and receive similar stories each week in your inbox! Get your weekly dose of the must-read tech stories, news, and tutorials.

Follow us on Twitter 🐦 and Facebook 👥 and Instagram 📷 and join our Facebook and Linkedin Groups 💬

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author! ⬇

--

--

A Budding Blogger, full time full stack developer, devops practioner. For more details about me checkout : https://jayvishaalj.github.io