Navigating Docker: A Beginner's Guide
Tags: docker, programming, webdev, beginners
Published: 2024-05-03 14:40:12

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:

```bash
docker run -it ubuntu
```

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

```bash
docker images
```

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

```bash
docker container ls
```

To view all running containers, use this command.

```bash
docker container ls -a
```

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

#### Starting and Stopping Containers

To start a container:

```bash
docker start [container name]
```

And to stop it:

```bash
docker stop [container name]
```

### Exploring Container Environments

#### Accessing Container Terminal

To access the terminal of a running container:

```bash
docker exec -it [container name] bash
```

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:

```bash
docker run -it -p [your local port]:[container port] [image name]
```

For instance:

```bash
docker run -it -p 1025:1025 nodejs
```

You can also pass environment variables:

```bash
docker run -it -p 1025:1025 -e key=value nodejs
```

### Containerizing an Image

Creating a Dockerfile facilitates containerization:

```Dockerfile
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"]
```

Build the image using:

```bash
docker build -t [project name] [project path]
```

### Docker Compose: Managing Multiple Containers

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

```yaml
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"
```

Use the following commands to manage your Docker Compose stack:

```bash
docker-compose up     # Run all configured containers
docker-compose down   # Remove the entire stack of containers
```

### 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:

```bash
docker build -t [container repo name] .
```

- Push the image to Docker Hub:

```bash
docker push [container repo name]
```

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](https://github.com/jps27cse) [Linkedin](https://www.linkedin.com/in/jps27cse/)