Docker: A Practical Guide with Arch Linux Setup: Part-1
Published: 2023-10-26 13:41:07

## Docker Intro:

Docker has revolutionized the way we develop, deploy, and run applications. It provides a lightweight and efficient solution for packaging, distributing, and running software in isolated environments called containers. This blog post aims to guide you through the basics of Docker, including its setup on an Arch-based Linux system.

## Docker Basics

### Installation

To install Docker on Arch Linux, you can use the following commands:

```bash
sudo pacman -S docker
sudo systemctl start docker
sudo systemctl enable docker
```
This will install Docker and start the Docker daemon.

### Adding User to Docker group
To add the user to the docker group, use the following command:

```
sudo usermod -aG docker $USER
```

This will add the user to the docker group without requiring sudo privileges.
`Note:` It requires to restart your device.

### Running a Container

Let's start with a simple example. To run an Ubuntu container interactively, use:

```bash
docker run -it ubuntu
```
### Find Images in Docker Hub
```bash
docker search jenkins
```
### Download Docker Image from Docker Hub to Local
```bash
docker pull jenkins
```
### Delete Local image
```bash
docker rmi image_name
```

This command pulls the Ubuntu image from the Docker Hub and starts an interactive shell within the container.

### Managing Containers

- **Start/Stop a Container:**

```bash
docker start container_name
docker stop container_name
```

- **List Containers:**

```bash
docker ps -a
```
- **Go Inside Docker Container:**
```bash
docker attach container_name
```

- **Execute a Command in a Running Container:**

```bash
docker exec container_name command
```

- **Attach to a Running Container:**

```bash
docker exec -it container_name bash
```

- **Remove a Container:**

```bash
docker rm container_name
```

- **Inspect Container Details:**

```bash
docker inspect container_name
```
- **Diff Containers**
```bash
docker diff container_name
```
- **Commit changes into container and create updated image**
```bash 
docker commit container_name new_image_name
```

### Listing Images

```bash
docker images
```

### Dockerization with Dockerfile

A Dockerfile is a script that contains a set of instructions for building a Docker image. Here's a basic example for a Node.js application:

```Dockerfile
FROM ubuntu

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

WORKDIR /app

COPY package.json package-lock.json ./

RUN npm install

COPY . .

CMD ["node", "main.js"]
```

To build the image, navigate to the directory containing the Dockerfile and run:

```bash
docker build -t your_image_name .
```

### Container Networking

To expose a port from the container to the host system, use the `-p` flag. For example:

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

This maps port 1025 on the host to port 1025 on the container.

## Conclusion

Docker simplifies the process of packaging, distributing, and running applications. In this guide, we covered basic Docker commands, the creation of Dockerfiles, and container networking. This should serve as a solid foundation for further exploration into the world of containerization.
Happy Dockering!