zaro

How to run docker container on server?

Published in Docker Container Deployment 5 mins read

Running a Docker container on a server involves a straightforward process of installing the Docker engine, preparing a container image, and then executing the image as a container. This enables you to deploy applications consistently and efficiently across various server environments.

Prerequisites for Running Docker Containers

Before you can run Docker containers, ensure your server meets the following fundamental requirements:

  • Supported Operating System: Docker runs on various Linux distributions (e.g., Ubuntu, CentOS, RHEL), Windows Server, and macOS.
  • System Resources: Sufficient CPU, memory, and disk space are crucial for the containers and the Docker daemon itself.
  • Internet Connectivity: Required for downloading Docker Engine, pulling images from Docker Hub, or other registries.

Step-by-Step Guide to Running a Docker Container

The general process involves three primary steps: installing Docker, obtaining a container image, and finally running the container.

1. Install Docker Engine on Your Server

The first crucial step is to install the Docker Engine, which is the core component that builds and runs containers.

  • On Linux Servers:
    Most Linux distributions offer official Docker packages. You can typically install Docker using your system's package manager. For example, on Ubuntu, you would use:
    sudo apt-get update
    sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

    After installation, you might need to add your user to the docker group to run Docker commands without sudo:

    sudo usermod -aG docker $USER
    newgrp docker
  • On Windows Server:
    For Windows Server environments, the installation typically involves using PowerShell with Administrator privileges. After installation, you'll manage Docker commands directly in PowerShell.

2. Obtain or Create a Docker Image

A Docker image is a lightweight, standalone, executable package that includes everything needed to run a piece of software, including the code, a runtime, libraries, environment variables, and config files.

  • Pulling an Image from Docker Hub:
    The easiest way to get an image is to pull one from a public registry like Docker Hub. For instance, to get the official Nginx web server image:
    docker pull nginx

    You can also specify a specific version (tag), like docker pull nginx:1.23.0.

  • Building a Custom Image with a Dockerfile:
    For custom applications, you'll need to create a Dockerfile. This text file contains instructions for building a Docker image. After creating your Dockerfile (and any necessary application files), you build the image using the docker build command.
    Example Dockerfile:
    # Use an official Node.js runtime as a parent image
    FROM node:18-alpine
    # Set the working directory in the container
    WORKDIR /app
    # Copy package.json and package-lock.json to the working directory
    COPY package*.json ./
    # Install app dependencies
    RUN npm install
    # Copy the rest of the application code
    COPY . .
    # Expose port 3000
    EXPOSE 3000
    # Define the command to run your app
    CMD ["node", "app.js"]

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

    docker build -t my-nodejs-app .

    The -t flag tags your image with a name (my-nodejs-app in this case), and . indicates the build context is the current directory.

3. Run the Docker Container

Once you have an image, you can run it as a container using the docker run command. This command creates a new container from a specified image and starts it.

Basic Syntax:

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Here are some commonly used options for docker run:

Option Description Example
-d Detached mode: Runs the container in the background. docker run -d ...
-p Port mapping: Maps a host port to a container port. docker run -p 80:8080 ... (Host port 80 to container port 8080)
-v Volume mounting: Mounts a host directory as a data volume. docker run -v /path/on/host:/path/in/container ...
--name Name the container: Assigns a custom name for easy reference. docker run --name my-web-server ...
--env / -e Environment variable: Sets environment variables inside the container. docker run -e MY_VAR=value ...
--rm Remove on exit: Automatically removes the container when it exits. docker run --rm ...
--network Network connection: Connects the container to a specific network. docker run --network my-custom-net ...

Practical Example: Running a Nginx Web Server

To run an Nginx web server and make it accessible on your server's port 80:

docker run -d -p 80:80 --name my-nginx-server nginx
  • -d: Runs the container in the background (detached mode).
  • -p 80:80: Maps host port 80 to container port 80. This means traffic to your server's port 80 will be routed to the Nginx container.
  • --name my-nginx-server: Assigns a readable name to the container, making it easier to manage.
  • nginx: Specifies the image to use (the Nginx image we pulled earlier).

After running this command, you can open a web browser and navigate to your server's IP address or domain name, and you should see the default Nginx welcome page.

Managing Running Containers

Once your containers are running, you'll need commands to manage them:

  • List running containers:
    docker ps

    Add -a to see all containers, including stopped ones: docker ps -a.

  • Stop a container:
    docker stop my-nginx-server

    (Replace my-nginx-server with your container's name or ID).

  • Start a stopped container:
    docker start my-nginx-server
  • Remove a container:
    You must stop a container before you can remove it.
    docker rm my-nginx-server
  • View container logs:
    docker logs my-nginx-server
  • Execute a command inside a running container:
    docker exec -it my-nginx-server /bin/bash

    This opens an interactive shell inside the container.

Conclusion

Running Docker containers on a server provides a powerful and flexible way to deploy and manage applications. By following these steps—installing Docker, preparing your image, and using the docker run command with appropriate options—you can effectively containerize your applications for robust server-side execution.