Deploying Applications with Docker on Your Linux server
Learn how to containerize and deploy your applications using Docker for easier management and scalability.
Deploying Applications with Docker on Your Linux server
Docker simplifies application deployment by packaging apps with all dependencies into containers. Learn how to deploy applications using Docker on your Linux server.
Why Use Docker?
Benefits of containerization:
- Consistency: Same environment everywhere (dev, staging, production)
- Isolation: Each app runs in its own container
- Portability: Run anywhere Docker is installed
- Efficiency: Lightweight compared to VMs
- Easy updates: Pull new image and restart
- Rollback: Quickly revert to previous version
Prerequisites
- Linux server with Ubuntu 20.04 or later
- Root or sudo access
- Basic command line knowledge
Step 1: Install Docker
Update packages:
sudo apt update
sudo apt upgrade -y
Install dependencies:
sudo apt install apt-transport-https ca-certificates curl software-properties-common
Add Docker GPG key:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
Add Docker repository:
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
Install Docker:
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin
Verify installation:
sudo docker --version
sudo docker run hello-world
Step 2: Configure Docker (Optional)
Add user to docker group (avoid using sudo):
sudo usermod -aG docker $USER
newgrp docker
Test without sudo:
docker ps
Step 3: Deploy a Simple Web App
Example: Node.js Application
Create Dockerfile:
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install --production
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]
Build image:
docker build -t myapp:latest .
Run container:
docker run -d -p 3000:3000 --name myapp myapp:latest
Verify it's running:
docker ps
curl localhost:3000
Step 4: Docker Compose for Multi-Container Apps
Create docker-compose.yml
:
version: '3.8'
services:
web:
build: .
ports:
- "3000:3000"
environment:
- NODE_ENV=production
- DATABASE_URL=postgresql://db:5432/myapp
depends_on:
- db
restart: unless-stopped
db:
image: postgres:15-alpine
environment:
- POSTGRES_USER=myuser
- POSTGRES_PASSWORD=mypassword
- POSTGRES_DB=myapp
volumes:
- postgres_data:/var/lib/postgresql/data
restart: unless-stopped
volumes:
postgres_data:
Start all services:
docker compose up -d
Check status:
docker compose ps
View logs:
docker compose logs -f
Step 5: Manage Containers
List running containers:
docker ps
List all containers:
docker ps -a
Stop container:
docker stop myapp
Start container:
docker start myapp
Restart container:
docker restart myapp
Remove container:
docker rm myapp
View logs:
docker logs -f myapp
Execute command in container:
docker exec -it myapp /bin/sh
Step 6: Manage Images
List images:
docker images
Remove image:
docker rmi myapp:latest
Pull image from Docker Hub:
docker pull nginx:latest
Tag image:
docker tag myapp:latest myapp:v1.0.0
Step 7: Persistent Data with Volumes
Create named volume:
docker volume create myapp_data
Use volume in container:
docker run -d -v myapp_data:/app/data myapp:latest
List volumes:
docker volume ls
Inspect volume:
docker volume inspect myapp_data
Step 8: Networking
Create custom network:
docker network create myapp_network
Run containers on network:
docker run -d --network myapp_network --name db postgres:15
docker run -d --network myapp_network --name web -p 3000:3000 myapp:latest
Containers on same network can communicate using container names.
Step 9: Update Deployed Application
Pull new code and rebuild:
git pull
docker build -t myapp:latest .
docker compose down
docker compose up -d
Or with zero-downtime using blue-green deployment.
Step 10: Best Practices
1. Use .dockerignore: Exclude unnecessary files
2. Multi-stage builds: Smaller images
3. Health checks: Monitor container health
4. Resource limits: Prevent containers from consuming all resources
5. Don't run as root: Create non-root user in Dockerfile
6. Use specific tags: Avoid latest
in production
7. Scan for vulnerabilities: docker scan myapp:latest
Monitoring and Maintenance
Check resource usage:
docker stats
Clean up unused resources:
docker system prune -a
Backup volumes:
docker run --rm -v myapp_data:/data -v $(pwd):/backup alpine tar czf /backup/backup.tar.gz /data
Conclusion
Docker simplifies deployment and makes your applications portable and scalable. Combined with Gumpbox's container management features, deploying and monitoring Docker applications becomes effortless.
Next Steps
Ready to simplify your Linux server management?
Gumpbox makes server administration effortless with an intuitive interface designed for developers.
Get Started