How to install docker on google cloud

  • This topic is empty.
  • Post
    Weekend Wiki
    Keymaster
    To install Docker on a Google Cloud virtual machine (VM) running a Linux-based OS (e.g., Ubuntu), follow these steps:

    Step 1: Create a Google Cloud VM Instance

    1. Log in to Google Cloud Console:
    2. Create a New Project (optional):
      • Navigate to the top-left corner and click Select a project.
      • Click New Project to create a new project, and give it a name.
    3. Create a VM Instance:
      • Go to Compute Engine -> VM instances from the Google Cloud Console.
      • Click Create Instance.
      • Choose the Machine type and Region that suits your requirements.
      • Choose an OS for your VM (Ubuntu or other Linux distributions like Debian or CentOS).
      • Under Firewall, check Allow HTTP traffic and Allow HTTPS traffic if you plan to expose web services.
      • Choose Create to launch the instance.
    4. Access the VM:
      • Once the VM instance is created, click SSH next to your instance to access the terminal.

    Step 2: Install Docker on Google Cloud VM

    The following steps will install Docker on your Google Cloud VM running Ubuntu (the process is similar for other Linux distributions).

    Step 1: Update the Package List

    Run the following commands to update the package list and upgrade the installed packages on your VM:

    sudo apt-get update
    sudo apt-get upgrade -y
    

    Step 2: Install Required Dependencies

    To install Docker, you need to ensure that you have the required dependencies:

    sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common
    

    Step 3: Add Docker’s Official GPG Key

    Docker provides an official GPG key to verify the integrity of the Docker installation package. Add it using the following command:

    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
    

    Step 4: Add Docker Repository

    Next, you’ll need to add Docker’s repository to your APT sources:

    sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
    

    Step 5: Install Docker

    Update the APT package list again to include Docker’s repository and then install Docker:

    sudo apt-get update
    sudo apt-get install -y docker-ce
    

    Step 6: Start Docker Service

    Once Docker is installed, start the Docker service:

    sudo systemctl start docker
    

    Step 7: Enable Docker to Start on Boot

    To ensure Docker starts automatically when the VM starts:

    sudo systemctl enable docker
    

    Step 3: Verify Docker Installation

    Run the following command to check if Docker is installed correctly and running:

    sudo docker --version
    

    You should see the version of Docker installed.

    To test Docker further, you can run the hello-world container:

    sudo docker run hello-world
    

    If everything is working, Docker will pull the hello-world image and run it, printing a confirmation message.


    Step 4: Manage Docker as a Non-root User (Optional)

    By default, Docker commands require root privileges. To manage Docker without using sudo every time, add your user to the Docker group:

    1. Add your user to the Docker group:
      sudo usermod -aG docker $USER
      
    2. Log out and log back in, or run the following command to apply the group changes:
      newgrp docker
      

    Now you can run Docker commands without sudo.


    Step 5: Configure Firewall and Expose Docker Containers (Optional)

    If you want to expose your Docker containers to the internet, you can open the required ports using Google Cloud’s firewall rules.

    1. Go to the Firewall rules section under VPC Network in the Google Cloud Console.
    2. Create a new firewall rule to allow traffic on the required ports (e.g., port 80 for HTTP, port 443 for HTTPS).

    For example, to allow HTTP traffic:

    • Set Source IP ranges to 0.0.0.0/0 (to allow all IP addresses).
    • Set Protocols and ports to tcp:80.

    This will expose any Docker containers running a web service (on port 80) to the internet.


    Step 6: Install Docker Compose (Optional)

    If you plan to manage multi-container Docker applications, you can install Docker Compose to define and run multi-container Docker applications.

    1. Install Docker Compose:
      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
      
    2. Make the Docker Compose Binary Executable:
      sudo chmod +x /usr/local/bin/docker-compose
      
    3. Verify Installation:
      docker-compose --version
      

    Step 7: (Optional) Secure Docker

    While the basic Docker setup is functional, consider securing your Docker installation by following best practices such as:

    1. Use HTTPS for Docker Remote API: For remote Docker access, set up a TLS/SSL connection.
    2. Use Docker Content Trust to ensure that only trusted images are used:
      export DOCKER_CONTENT_TRUST=1
      
    3. Regularly update Docker: Make sure your Docker installation is up-to-date with the latest security patches.

    Conclusion

    By following these steps, you should now have Docker installed and running on your Google Cloud VM. You can manage containers, expose applications to the internet, and optionally use Docker Compose to manage multi-container applications.

    Let me know if you need further assistance or additional details!

  • You must be logged in to reply to this topic.
en_USEnglish