Streamlining Local Docker Deployment to Cafe24 Virtual Server

0

Today’s fast-evolving development environment demands deployment automation. Especially for local Docker Compose environments, automating deployment to Cafe24 virtual servers can save time and effort. This method creates an environment that is easy to update and enhances both development and operations efficiency. In this article, we’ll explore the details of how to set up this automated deployment process.

1. SSH Access Setup: Start Securely from the Beginning

First, it is crucial to set up SSH access from your local environment to the Cafe24 server. This allows you to connect to the server without a password. Secure and reliable connections enable seamless interaction with the server.

How to Generate and Configure SSH Keys

Generate SSH keys locally and then copy them to the server for connection setup. If you don’t have an SSH key, create one using the following command:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

Once the SSH key is generated, copy it to the Cafe24 server:

ssh-copy-id your_username@cafe24_server_ip

This will allow you to securely connect to the server. From now on, you can connect without entering a password by using the SSH key.

2. Preparing Docker Compose File: The Heart of Deployment

The Docker Compose file plays a key role in setting up and deploying your application environment. You can copy it to the Cafe24 server using Git or Rsync.

Deployment Using Git (Recommended)

If you manage your local project using Git, clone the relevant Git repository on the Cafe24 server and apply changes easily. Using Git simplifies version control and deployment processes.

1. Install Git on Cafe24 Server
sudo apt update
sudo apt install git
2. Push Your Local Project to GitHub or GitLab
git add .
git commit -m "Initial commit"
git push origin main
3. Clone the Project on the Cafe24 Server
git clone https://github.com/yourusername/yourproject.git
cd yourproject
docker-compose up -d

Now, the deployment is complete with the `docker-compose up -d` command.

File Synchronization Using Rsync

If you are not using Git, you can directly synchronize your local project files to the Cafe24 server using Rsync. Rsync is a highly useful tool for file synchronization and transfer.

rsync -avz ./your-local-project-directory/ your_username@cafe24_server_ip:/home/your_username/your-server-directory/

After synchronization, execute Docker Compose on the Cafe24 server:

ssh your_username@cafe24_server_ip
cd /home/your_username/your-server-directory/
docker-compose up -d

3. Using CI/CD Tools for Deployment Automation

If you want to automate deployment, it’s a good idea to use CI/CD tools. Tools like GitHub Actions, GitLab CI/CD, and Jenkins can automatically build and deploy your project, bridging the gap between development and operations for smoother workflows.

Automation Example Using GitHub Actions

GitHub Actions is an excellent tool for automating deployment. You can set up automatic deployment with the following example:

name: Deploy to Cafe24 Server

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout code
      uses: actions/checkout@v2

    - name: Set up SSH
      uses: webfactory/ssh-agent@v0.5.3
      with:
        ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}

    - name: Rsync files to Cafe24 server
      run: |
        rsync -avz ./ your_username@cafe24_server_ip:/home/your_username/your-server-directory/

    - name: SSH into server and deploy
      run: |
        ssh your_username@cafe24_server_ip << 'EOF'
        cd /home/your_username/your-server-directory/
        docker-compose up -d
        EOF

This setup automatically deploys the project to the server whenever a push is made to the main branch.

4. Additional Considerations: Docker Installation and Opening Ports

Before deploying, make sure Docker and Docker Compose are installed on the server. If not installed, use the following commands to install them:

sudo apt update
sudo apt install docker.io
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

Additionally, open the necessary ports through firewall settings to allow external access. Use the following commands to open the ports:

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

This process will enable external access to your application.

Conclusion: Improve Development Speed with Simple Automated Deployment

We have explored how to automatically deploy a local Docker environment to a Cafe24 virtual server. Deployment methods using Git or Rsync are efficient, and CI/CD tools can automate the process. This will help improve development speed, making server management and updates much easier. We hope this automated deployment process makes your server operation more convenient.

Leave a Reply