Learn Docker for beginner

If you are learning web development sooner or later you will need to learn docker, and in fact most of company these days is using docker to develop and deploy their web application.

In this article we will learn the basic docker and by the end of this article you should be able to build a docker image and run it on your local computer or your production server.

What is docker?

Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package.

Containers provide a way to run applications in an isolated environment, which can be very useful for ensuring that an application works consistently across different environments and for improving the reliability and scalability of an application.

Docker is also useful for running multiple applications on the same machine, as each application can be isolated in its own container. This can be especially useful to provide required application to run your application in one machine. For example an application might need mysql datababse, redis and elastic search engine. With docker you can provide all those dependency easily.

Docker vs Virtual Machine

Before docker people are deploying their application on Bare Metal server. But running application to bare metal server is inefficient at least you will fight this three issues.

  1. Resource utilization: Running an application on a bare metal server can be inefficient in terms of resource utilization, as the server might not be fully utilized. In contrast, using containers or virtual machines allows you to run multiple applications on the same physical server, making better use of the available resources.

  2. Isolation: Running an application on a bare metal server can also be risky in terms of isolation. If the application fails or is compromised, it can potentially affect the entire server and other applications running on it. Containers and virtual machines provide a way to isolate applications from each other, reducing the risk of failure or compromise affecting other applications.

  3. Portability: Deploying an application to a bare metal server can also be less portable than using containers or virtual machines. If you want to move the application to a different server, you would need to manually set up the server and install all of the dependencies. With containers or virtual machines, you can simply move the container or VM image to the new server, saving time and effort.

To improve resource utilization, isolation, and portability you can use Virtual Machine or Docker. But for choosing these option you need to consider this.

A virtual machine (VM) is a software implementation of a computer that executes programs like a physical machine.

Docker is a tool that allows you to package an application and its dependencies in a container, which can then be run on any machine that has Docker installed.

Here are some key differences between VMs and Docker:

  • VMs include a full operating system, while Docker containers share the host machine's operating system.
  • VMs are heavier weight and slower to start up than Docker containers.
  • VMs are designed to run a single application or a small set of closely related applications, while Docker is designed to run multiple isolated applications.
  • VMs provide isolation at the hardware level, while Docker provides isolation at the operating system level.

Both VMs and Docker have their own use cases and advantages, and it is often useful to use both technologies together. For example, you might use VMs to run different environments for testing and development, and use Docker to package and deploy your applications to production environments.

Install Docker on your machine

If you are using a Mac or Windows, you can download Docker Desktop from the official website: https://www.docker.com/products/docker-desktop

If you are using Linux, you can install Docker using your distribution's package manager. For example, on Ubuntu you can use the following command:

sudo apt-get install docker

Once Docker is installed, you can start using it by running the docker command in your terminal. You can check that it is working by running the following command:

docker --version

To start using Docker, you will need to create a Docker image. A Docker image is a lightweight, stand-alone, executable package that includes everything needed to run a piece of software, including the application code, libraries, dependencies, and runtime.

Docker image

A Docker image is a lightweight, stand-alone, executable package that includes everything needed to run a piece of software, including the application code, libraries, dependencies, and runtime.

Docker images are created using a Dockerfile, which is a text file that contains the instructions for building the image. The Dockerfile specifies the base image to use (such as an operating system), and then adds the necessary files and packages needed to run the application.

You can create a Docker image using a Dockerfile, which is a text file that contains the instructions for building the image. Here is an example of a simple Dockerfile that creates an image for a Python application:

FROM python:3.8

COPY . /app
WORKDIR /app

RUN pip install -r requirements.txt

CMD ["python", "app.py"]

To build the image, run the following command in the same directory as your Dockerfile:

docker build -t my-app .

This will create an image with the name "my-app".

Once the image is built, you can use it to create a Docker container. A Docker container is a running instance of a Docker image. To create a container, you can use the docker run command, like this:

docker run -p 8000:8000 my-app

This will create a container and run the app.py script inside it. The -p flag maps the container's port 5000 to the host machine's port 5000, so you can access the application from your host machine's web browser.

These are just the basics of using Docker. There are many more features and options available, such as networking, volumes, and composing multiple containers into a single application. You can find more information in the official Docker documentation: https://docs.docker.com.

Docker Cheatsheet

Here is a quick cheat sheet for Docker:

1. Install Docker:

sudo apt-get update
sudo apt-get install docker.io

For macs you can use brew.

brew install docker

2. Start and enable Docker service:

sudo systemctl start docker
sudo systemctl enable docker

On mac

brew services start docker

3. Verify that Docker is running:

sudo systemctl status docker

4. Pull an image from a Docker registry (e.g., Docker Hub):

docker pull ubuntu

5. List all images on your system:

docker images

6. Run a command in a new container:

docker run -it ubuntu bash

7. Run a command in an existing container:

docker exec -it <container_id> bash

8. Stop a running container:

docker stop <container_id>

9. Start a stopped container:

docker start <container_id>

10. Remove a container:

docker rm <container_id>

11. Remove an image:

docker rmi <image_id>

12. Build an image from a Dockerfile:

docker build -t <image_name> .

13. Push an image to a Docker registry:

docker push <image_name>