DEV Community

Cover image for Navigating Docker: A Beginner's Guide
Jack Pritom Soren
Jack Pritom Soren

Posted on

Navigating Docker: A Beginner's Guide

Docker has revolutionized the way software is developed, deployed, and managed. With its lightweight containers and efficient workflow, Docker enables developers to package their applications and dependencies into a standardized unit, ensuring consistency across different environments. In this guide, we'll explore some fundamental Docker commands and concepts to help you kickstart your journey into containerization.

Getting Started with Docker

Creating and Managing Containers

To begin, let's delve into some basic Docker commands for managing containers:

docker run -it ubuntu
Enter fullscreen mode Exit fullscreen mode

This command creates a container image of Ubuntu and opens an interactive terminal session within it.

docker images
Enter fullscreen mode Exit fullscreen mode

To list all installed images in Docker, this command comes in handy.

docker container ls
Enter fullscreen mode Exit fullscreen mode

To view all running containers, use this command.

docker container ls -a
Enter fullscreen mode Exit fullscreen mode

Adding -a flag displays all containers, including those that are stopped.

Starting and Stopping Containers

To start a container:

docker start [container name]
Enter fullscreen mode Exit fullscreen mode

And to stop it:

docker stop [container name]
Enter fullscreen mode Exit fullscreen mode

Exploring Container Environments

Accessing Container Terminal

To access the terminal of a running container:

docker exec -it [container name] bash
Enter fullscreen mode Exit fullscreen mode

This command allows you to connect your local terminal to the terminal within the Docker container.

Port Mapping

Port mapping enables running containerized servers on your local machine:

docker run -it -p [your local port]:[container port] [image name]
Enter fullscreen mode Exit fullscreen mode

For instance:

docker run -it -p 1025:1025 nodejs
Enter fullscreen mode Exit fullscreen mode

You can also pass environment variables:

docker run -it -p 1025:1025 -e key=value nodejs
Enter fullscreen mode Exit fullscreen mode

Containerizing an Image

Creating a Dockerfile facilitates containerization:

FROM ubuntu

RUN apt-get update
RUN apt-get install -y curl
RUN curl -sL https://deb.nodesource.com/setup_18.x | bash -
RUN apt-get upgrade -y
RUN apt-get install -y nodejs

COPY package.json package.json
COPY package-lock.json package-lock.json
COPY main.js main.js

RUN npm install

ENTRYPOINT ["node", "main.js"]
Enter fullscreen mode Exit fullscreen mode

Build the image using:

docker build -t [project name] [project path]
Enter fullscreen mode Exit fullscreen mode

Docker Compose: Managing Multiple Containers

Docker Compose simplifies the management of multi-container applications. Consider the following docker-compose.yml file:

version: "3.8"

services:
  postgres:
    image: postgres
    ports:
      - "5432:5432"
    environment:
      POSTGRES_USER: postgres
      POSTGRES_DB: review
      POSTGRES_PASSWORD: password

  redis:
    image: redis
    ports:
      - "6379:6379"
Enter fullscreen mode Exit fullscreen mode

Use the following commands to manage your Docker Compose stack:

docker-compose up     # Run all configured containers
docker-compose down   # Remove the entire stack of containers
Enter fullscreen mode Exit fullscreen mode

Publishing Docker Images to Docker Hub

To share your Docker images with others, you can publish them to Docker Hub:

  • Create a repository on Docker Hub.
  • Build your local image using:
docker build -t [container repo name] .
Enter fullscreen mode Exit fullscreen mode
  • Push the image to Docker Hub:
docker push [container repo name]
Enter fullscreen mode Exit fullscreen mode

Docker makes it seamless to package, distribute, and run applications across different environments. By mastering these basic commands and concepts, you'll be well-equipped to leverage Docker's power for your development projects. Happy containerizing!

Follow me on : Github Linkedin

Top comments (0)