A Complete Beginner’s Guide to Docker Environment Setup and Management

0

What is Docker?

Docker is an open-source platform that helps you containerize applications for deployment, management, and execution. With Docker, you can package an application and its dependencies into a single unit that can run consistently across any environment. In this article, we will explore the steps to set up a Docker environment in detail.

1. Installing Docker

Installing Docker varies slightly depending on the operating system. Here, we introduce the installation methods for each OS.

Windows

macOS

Linux

1. Update the package index
sudo apt-get update
2. Install required packages
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
3. Add Docker’s official GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
4. Set up the Docker repository
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
5. Install Docker
sudo apt-get update
sudo apt-get install docker-ce
6. Start and verify the Docker service
sudo systemctl start docker
sudo systemctl enable docker
sudo systemctl status docker

2. Basic Docker Commands

Once Docker is installed, you can start using Docker with basic commands.

Check Docker version

docker --version

View Docker images list

docker images

View Docker containers list

docker ps

3. Docker Images and Containers

Images act as templates for creating containers. Containers are runnable instances created from images.

Downloading and Running Images

1. Download and run an image

Download an image from Docker Hub and run a container. For example, to download and run an Nginx image, use the following command:

docker run -d -p 80:80 --name mynginx nginx

This command runs the Nginx container in the background and maps port 80 of the host to port 80 of the container.

Managing Containers

View running containers
docker ps
View all containers
docker ps -a
Stop a container
docker stop <container ID or name>
Start a container
docker start <container ID or name>
Remove a container
docker rm <container ID or name>

4. Writing a Dockerfile

A Dockerfile is a script used to create an image. For example, let’s write a Dockerfile for a simple Node.js application.

1. Create a project directory

Create a project directory and navigate to it:

mkdir mynodeapp
cd mynodeapp

2. Create `app.js` file

const http = require('http');

   const hostname = '0.0.0.0';
   const port = 3000;

   const server = http.createServer((req, res) => {
     res.statusCode = 200;
     res.setHeader('Content-Type', 'text/plain');
     res.end('Hello World\n');
   });

   server.listen(port, hostname, () => {
     console.log(`Server running at http://${hostname}:${port}/`);
   });

3. Create `package.json` file

{
     "name": "mynodeapp",
     "version": "1.0.0",
     "description": "My Node.js Docker App",
     "main": "app.js",
     "scripts": {
       "start": "node app.js"
     },
     "dependencies": {
       "express": "^4.17.1"
     }
   }

4. Write the Dockerfile

# Set the base image
   FROM node:14

   # Set the working directory
   WORKDIR /usr/src/app

   # Copy package files
   COPY package*.json ./

   # Install dependencies
   RUN npm install

   # Copy source code
   COPY . .

   # Set the application port
   EXPOSE 3000

   # Command to run the application
   CMD ["node", "app.js"]

5. Build the Docker image

docker build -t mynodeapp .

6. Run the Docker container

docker run -d -p 3000:3000 --name mynodeapp-container mynodeapp

Now, you can see the “Hello World” message at `http://localhost:3000`.

5. Docker Compose

Docker Compose is a tool for defining and running multi-container Docker applications. You can define multiple services in a `docker-compose.yml` file.

Installing Docker Compose

  • Docker Compose is included in Docker Desktop for Windows and macOS.
  • For Linux, you need to install it separately:
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

Example of `docker-compose.yml`

version: '3'
services:
  web:
    image: mynodeapp
    build: .
    ports:
      - "3000:3000"
    volumes:
      - .:/usr/src/app
    depends_on:
      - db
  db:
    image: mongo
    ports:
      - "27017:27017"

In this example, we define a Node.js application and a MongoDB database.

6. Summary

  • Installing Docker: Install Docker Desktop or Docker Engine according to your operating system.
  • Basic Docker commands: Use Docker commands to download images, and run and manage containers.
  • Writing a Dockerfile: Write scripts to create Docker images.
  • Using Docker Compose: Use a tool to define and run multi-container applications.

By following these steps, you can successfully set up a Docker environment and containerize your applications. Docker helps maintain consistency across development environments and simplifies the deployment process.

Leave a Reply