Docker's Complete Guide: Learn to Create and Manage Containers for Scalable Applications

Docker has become an essential tool in the world of system development and administration, allowing the creation, distribution and execution of applications in isolated and consistent environments. In this comprehensive guide, we'll explore everything from the basics of Docker to practical examples of different use cases, including content from Docker Hub.

Introduction to Docker

Docker is a lightweight virtualization platform that allows you to package and distribute applications along with all their dependencies in containers. A container is a unit of software that includes everything needed to run an application, including code, runtime, libraries, and configurations

Advantages of using Docker

  • Portability: Containers can run on any machine that has Docker installed.
  • Isolation: Each container operates in an isolated environment, avoiding conflicts between applications.
  • Scalability: Containers can be easily replicated to handle variable workloads.
  • Consistency: Ensures that the application works the same way in different environments.

Key concepts: images and containers.

  • Image: Template that contains a file system and settings needed to run an application.
  • Container: Running instance of an image. It's an isolated environment.

Docker installation.

To install Docker, it depends on the operating system, it is available for Linux with any distribution such as Ubuntu, CentOS, Kali, etc., also available for Windows and MacOS. To install it, just follow the steps depending on the operating system you have in this link: https://docs.docker.com/engine/install/

Docker Basic Commands

Docker Run

The 'docker run' command is used to run a container from an image. For example:

docker run -d -p 80:8080 nginx

This command runs a container based on the “nginx” image in the background and maps port 8080 of the container to port 80 of the host.

💡 Note: The detach '-d' flag is usually used to be able to continue using our terminal, since if it's not used, it's time to stop the container to continue using the command line.

Docker ps

Shows a list of running containers.

docker ps

Docker ps -a

Shows the list of containers that are running and stopped over time.

docker ps -a

Docker images

Lists the images available in the system.

docker images

Docker build

Build an image from a Dockerfile in the current directory.

docker build -t "my-app"

Docker stop and Docker Start

Stops and starts a running container.


docker stop "container-name"
docker start " container-name"


Creating and Managing Images

A Dockerfile is a text file that defines how an image will be constructed. Dockerfile example for a Node.js application



FROM node:18.17-alpine

WORKDIR /app

COPY package.json yarn.lock ./

RUN npm install

COPY . .

EXPOSE 3000

CMD [ "npm", "start" ]

Here we can see an example showing a series of instructions that our DockerFile will have

  • FROM: provides us with a base image. In this case we took the 18.17-alpine image, which is much lighter than an 18.17 node
  • WORKDIR: this is where you will host our application in the container
  • COPY: add the files from our local directory, all the dependencies so that you don't have any errors when doing the build
  • RUN: create the application with node:18.17-alpine
  • EXPOSE: this is where our application will be displayed, in this case on port 3000
  • CMD: Specify which command we want to execute in the container

💡 Note: there is another instruction that is the ENTRYPOINT, which has been the entry to execute in the container as an example it would look like this


FROM node:18.17-alpine

WORKDIR /app

COPY package.json yarn.lock ./

RUN npm install

COPY . .

EXPOSE 3000

ENTRYPOINT [ "npm" ]

CMD [ "start" ]


Docker networks

Types of Networks in Docker

  • Bridge: internal private network for containers on the same host.
  • Host: Share the host's network with the container
  • Overlay: Multi-host network for communication between containers on different hosts.

Create and Manage Networks

docker network create "my-network"

Connecting Containers to Networks

docker run -d --network="my-network" --name="my-container-nginx"

Data Persistence

Assembling Volumes in Containers

It allows data to persist even after stopping or deleting a container.

docker run -d -v "my-volume":/ruta/.../.../"my-container-nginx"

Data in the Container vs Data in Volume

The data in the container is volatile and is lost when the container is deleted. The data in a volume persists

Copying data to/from Containers


docker cp archivo.txt "container-name":/ruta/.../.../container/
docker cp "container-name":/ruta/.../.../container/archivo.txt .


Practical Use Cases

Deploying a Web Application with Nginx


version: '3'
services:
  web:
    image: nginx
    ports:
      - "80:80"


Container Databases: MySQL and MongoDB


version: '3'
services:
  mysql:
    image: mysql:latest
    environment:
      MYSQL_ROOT_PASSWORD: mysecretpassword
  mongodb:
    image: mongo:latest
    volumes:
      - mongo-data:/data/db
volumes:
  mongo-data:


Isolated Development Environments

Create consistent development environments for different projects


FROM node:14

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

CMD ["npm", "start"]


Container Orchestration with Kubernetes

Kubernetes is a powerful tool for orchestrating and managing containers in a production environment

Exploring Docker Hub

Docker Hub is a public repository for container images, where individuals and companies can publish solution images.

Steps to Upload an Image to Docker-Hub

The first thing you need to have to upload an image to Docker-Hub is an account,

  1. Create your Docker Hub account:

If you don't have your Docker Hub account yet, go to https://hub.docker.com/ and sign up

  1. Install Docker on your local machine:

To install Docker on your systems, download and install it from the official website of docker https://www.docker.com/get-started/

  1. Sign in to Docker Hub from the command line

Open a terminal and use the following command to log in to Docker Hub with your username and password


docker login


You'll be asked to enter your Docker Hub username and password

  1. Label your image that you are going to upload

Make sure that the image you want to upload has an appropriate label. You can label the image using the following command


docker tag "name_image":etiqueta "user_docker_hub"/"name_image":etiqueta


Example:



docker tag my_app:v1 robpalacios1/my_app:v1

  1. Upload the image to Docker Hub

Use the following command to upload your tagged image to Docker Hub:



docker push "user_docker_hub"/"name_image":etiqueta 

Example


docker push robpalacios1/my_app:v1


  1. Verify the image in Docker Hub

After the image is pushed and you see that it has been successfully completed, you can review the repository in Docker Hub, in the section repositories to make sure that the image is available


💡 Note: As you can see, I have in my Docker Hub a React repository with different versions that I have done in the code, and just by doing a pull to your place they can contribute to the code, GitHub style with the docker pull robpalacios1/react-docker command: <Tag>

Where Tag can be the versions I have from 1.0.0 to the last one that is being the latest

Conclusion

Docker is a technology that has evolved the way we develop, deploy and manage applications. From creating images to managing networks and volumes, Docker provides the tools needed to build consistent and efficient environments.

Ready to take your applications to the next level with Docker?

At Kranio, we have experts in containerization and DevOps who will help you implement efficient solutions using Docker, optimizing your development processes and ensuring the scalability of your applications. Contact us and discover how we can promote the digital transformation of your company.

Roberto Palacios

September 16, 2024