The Power of Containers: An Introduction to Docker and its Applications

Tejaksha K
6 min readMar 4, 2023

--

Docker is a popular platform that allows developers to create, deploy, and run applications in isolated containers. Containers are lightweight, portable, and self-sufficient units that package all the dependencies and configurations needed to run an application.

In this blog, we will cover the basics of Docker and explain how to create a simple Docker file with a sample application. We will also discuss the differences between Docker files and Docker Compose files.

Docker basics

Docker is built on top of a technology called containerization, which allows applications to be isolated from the underlying system and run consistently across different environments. Containers are similar to virtual machines, but they are much lighter and faster since they don’t need to emulate an entire operating system.

Docker uses a client-server architecture, where the Docker client communicates with the Docker daemon running on the host machine. The Docker daemon is responsible for managing the containers, images, and other Docker resources.

To start using Docker, you first need to install it on your machine. You can download the Docker Desktop application for Windows and Mac from the official Docker website. For Linux users, Docker can be installed from the command line using the package manager.

Once Docker is installed, you can start using it to create and manage containers. The Docker command-line interface (CLI) provides a set of commands for interacting with Docker. Here are some of the most commonly used Docker commands:

  • docker run: creates a new container from an image
  • docker build: builds a new image from a Dockerfile
  • docker push: uploads an image to a Docker registry
  • docker pull: downloads an image from a Docker registry
  • docker ps: lists all running containers
  • docker stop: stops a running container
  • docker rm: removes a container
  • docker images: lists all available images
  • docker rmi: removes an image

Creating a Docker file

To create a Docker image, you need to create a Dockerfile. A Dockerfile is a text file that contains a set of instructions for building an image. Here is an example of a simple dockerfile.

FROM node:14-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD [ "npm", "start" ]

Let’s go through each line of this Dockerfile:

  • FROM node:14-alpine: This line specifies the base image that our image will be built upon. In this case, we are using the node:14-alpine image, which is a lightweight version of Node.js 14 based on Alpine Linux.
  • WORKDIR /app: This line sets the working directory to /app.
  • COPY package*.json ./: This line copies the package.json and package-lock.json files to the current directory (./) in the container.
  • RUN npm install: This line installs the dependencies specified in the package.json file.
  • COPY . .: This line copies all the files from the current directory on the host machine to the current directory in the container.
  • EXPOSE 3000: This line exposes port 3000 on the container.
  • CMD [ "npm", "start" ]: This line specifies the command that will be run when the container starts. In this case, we are running npm start.

To build the Docker image, navigate to the directory where the Dockerfile is located and run the following command:

docker build -t myapp .

This command tells Docker to build an image with the tag myapp using the Dockerfile in the current directory (.). The -t flag specifies the tag name.

Once the image is built, you can run it using the docker run command:

docker run -p 3000:3000 myapp

This command tells Docker to start a container using the myapp image and map port 3000 in the container to port 3000 on the host machine (-p 3000:3000).

Docker Compose

While Dockerfiles are great for building images, they can be limited when it comes to deploying complex applications with multiple containers. Docker Compose is a tool that allows you to define and run multi-container Docker applications using a YAML file.

A Docker Compose file defines the services that make up your application, along with their configurations and dependencies. Here is an example of a simple Docker Compose file:

version: '3'
services:
app:
build: .
ports:
- "3000:3000"
depends_on:
- db
environment:
DB_HOST: db
DB_USER: myuser
DB_PASSWORD: mypassword
db:
image: mysql:latest
restart: always
environment:
MYSQL_DATABASE: mydb
MYSQL_USER: myuser
MYSQL_PASSWORD: mypassword
MYSQL_ROOT_PASSWORD: rootpassword
volumes:
- dbdata:/var/lib/mysql
volumes:
dbdata:

This Compose file defines two services: app and db.

The app service is built using the Dockerfile in the current directory (.) and maps port 3000 in the container to port 3000 on the host machine. It also depends on the db service, meaning that the db service will be started before the app service.

The db service uses the mysql:latest image and sets some environment variables for configuring the MySQL database. It also sets up a volume called dbdata to persist the data stored in the database.

Both the app and db services have environment variables set for the database host, user, and password. These variables are used by the application to connect to the database.

To start the application using Docker Compose, navigate to the directory where the Compose file is located and run the following command:

docker-compose up

This command will start both the app and db services, and the application will be accessible at http://localhost:3000.

If you need to make changes to the application or the database, you can stop the containers using the docker-compose down command, make your changes, and then start the containers again using docker-compose up.

The data stored in the database will be persisted in the dbdata volume, so it will not be lost when the containers are stopped and started again.

In summary, Docker Compose allows you to define and manage multi-container applications with ease. By including environment variables and volumes in your Compose file, you can configure your services and persist data across container restarts.

Dockerfile vs. Docker Compose

While Dockerfiles and Docker Compose files are both used for building and deploying Docker applications, they serve different purposes.

A Dockerfile is used to define the image that a container is based on. It contains instructions for installing dependencies, configuring the environment, and setting up the application. Dockerfiles are typically used to build a single container image.

On the other hand, a Docker Compose file is used to define a multi-container application. It allows you to define multiple services and their configurations, and manage the dependencies between them. Docker Compose files are typically used for deploying complex applications with multiple containers.

In conclusion, Docker is a powerful tool that can simplify the process of building and deploying applications. By using Dockerfiles and Docker Compose files, you can easily create and manage containers and services, and ensure that your applications are consistent and portable across different environments.

Conclusion

In conclusion, Docker is a powerful tool that simplifies the process of building and deploying applications. With Dockerfiles, you can easily define the image that a container is based on and automate the build process. With Docker Compose, you can define and manage multi-container applications with ease, including their configurations and dependencies.

In this blog, we covered the basics of Docker and how to create a simple Dockerfile to build a container image. We also explained how Docker Compose works and provided an example of a Compose file that includes a database service with credentials and a volume for persistent data.

By using Docker and Docker Compose, you can create and deploy applications that are consistent and portable across different environments. Whether you’re a developer or a system administrator, Docker is a valuable tool that can save you time and simplify your workflow.

You’re welcome! I’m glad I could help you with our blog post. Don’t hesitate to reach out if you have any other questions or need further assistance. Keep up the great work! ❤️

--

--

Tejaksha K

Reach me at https://tinyurl.com/56svjz36 I'm a Full Stack Developer & Cloud Expert with experience in Google Cloud Platform & AWS.