{"id":1952,"date":"2025-01-01T11:25:37","date_gmt":"2025-01-01T11:25:37","guid":{"rendered":"https:\/\/havari.me\/?p=1952"},"modified":"2025-05-02T06:56:05","modified_gmt":"2025-05-02T06:56:05","slug":"how-to-install-and-configure-docker-and-docker-compose-2","status":"publish","type":"post","link":"https:\/\/havari.me\/en\/how-to\/how-to-install-and-configure-docker-and-docker-compose-2\/","title":{"rendered":"How to install and configure Docker and Docker-compose"},"content":{"rendered":"<div dir=\"ltr\">\n<style>\n\/* WordPress-style code block styling with syntax highlighting *\/\n.wp-block-code {\n    background: #1e1e1e;\n    color: #d4d4d4;\n    padding: 16px;\n    border-radius: 8px;\n    font-family: 'Consolas', 'Monaco', monospace;\n    font-size: 14px;\n    overflow-x: auto;\n    margin: 1em 0;\n    border: 1px solid #3c3c3c;\n}<\/p>\n<p>\/* Syntax highlighting for code blocks *\/\n.wp-block-code code {\n    display: block;\n}<\/p>\n<p>\/* Bash syntax *\/\n.wp-block-code.bash code {\n    color: #d4d4d4;\n}\n.wp-block-code.bash .keyword { color: #569cd6; } \/* e.g., apt, curl *\/\n.wp-block-code.bash .string { color: #ce9178; } \/* e.g., quoted strings *\/\n.wp-block-code.bash .comment { color: #6a9955; } \/* e.g., # comments *\/\n.wp-block-code.bash .variable { color: #9cdcfe; } \/* e.g., $USER *\/<\/p>\n<p>\/* YAML syntax *\/\n.wp-block-code.yaml code {\n    color: #d4d4d4;\n}\n.wp-block-code.yaml .key { color: #9cdcfe; } \/* e.g., service names, keys *\/\n.wp-block-code.yaml .string { color: #ce9178; } \/* e.g., quoted values *\/\n.wp-block-code.yaml .number { color: #b5cea8; } \/* e.g., version numbers *\/<\/p>\n<p>\/* JSON syntax *\/\n.wp-block-code.json code {\n    color: #d4d4d4;\n}\n.wp-block-code.json .key { color: #9cdcfe; } \/* e.g., JSON keys *\/\n.wp-block-code.json .string { color: #ce9178; } \/* e.g., JSON strings *\/<\/p>\n<p>\/* General styling for content *\/\n.wp-block-group {\n    margin-bottom: 2em;\n}\n.wp-block-group h1 {\n    color: #333;\n    font-size: 2.2em;\n    margin-bottom: 0.5em;\n}\n.wp-block-group h2 {\n    color: #333;\n    font-size: 1.8em;\n    margin-bottom: 0.5em;\n}\n.wp-block-group h3 {\n    color: #444;\n    font-size: 1.4em;\n    margin-bottom: 0.5em;\n}\n.wp-block-group p, .wp-block-group ul, .wp-block-group ol {\n    color: #555;\n    line-height: 1.6;\n}\n.wp-block-group ul li, .wp-block-group ol li {\n    margin-bottom: 0.5em;\n}\n.wp-block-group a {\n    color: #0073aa;\n    text-decoration: none;\n}\n.wp-block-group a:hover {\n    text-decoration: underline;\n}\nhr {\n    border: 0;\n    border-top: 1px solid #ccc;\n    margin: 2em 0;\n}\n<\/style>\n<div class=\"wp-block-group\">\n<h1>Step-by-Step Guide to Installing Docker and Docker Compose on Your VPS or Root Server<\/h1>\n<p>Docker and Docker Compose are powerful tools for running and managing containerized applications. Whether you're deploying web apps, databases, or microservices, setting them up on a VPS or root server is straightforward. In this guide, I'll walk you through connecting to your server via SSH using PuTTY, installing Docker and Docker Compose, and configuring them on an Ubuntu or Debian system. Let's get started!<\/p>\n<hr>\n<\/div>\n<div class=\"wp-block-group\">\n<h2>Prerequisites<\/h2>\n<p>Before diving in, ensure you have:<\/p>\n<ul>\n<li>A VPS or root server running Ubuntu or Debian.<\/li>\n<li>Root or <code>sudo<\/code> access to the server.<\/li>\n<li>An internet connection for downloading packages.<\/li>\n<li>The IP address or domain name of your server.<\/li>\n<\/ul>\n<hr>\n<\/div>\n<div class=\"wp-block-group\">\n<h2>Step 1: Download and Install PuTTY<\/h2>\n<p>If you haven't already, download <strong>PuTTY<\/strong>a free SSH client for connecting to your server:<\/p>\n<ul>\n<li>Visit the <a href=\"https:\/\/www.chiark.greenend.org.uk\/~sgtatham\/putty\/latest.html\">official PuTTY website<\/a>.<\/li>\n<li>Download the installer or standalone <code>putty.exe<\/code> for your Windows system.<\/li>\n<li>Install PuTTY (or run the executable directly if using the standalone version).<\/li>\n<\/ul>\n<hr>\n<\/div>\n<div class=\"wp-block-group\">\n<h2>Step 2: Connect to Your Server via SSH<\/h2>\n<ol>\n<li><strong>Open PuTTY<\/strong>:\n<ul>\n<li>Launch PuTTY from your Start menu or by double-clicking <code>putty.exe<\/code>.<\/li>\n<\/ul>\n<\/li>\n<li><strong>Enter Server Details<\/strong>:\n<ul>\n<li>In the <strong>\"Host Name (or IP address)\"<\/strong> field, type your server's IP address or domain name (e.g, <code>192.168.1.1<\/code> or <code>yourdomain.com<\/code>).<\/li>\n<li>Ensure the <strong>Port<\/strong> is set to <code>22<\/code> (default for SSH).<\/li>\n<li>Select <strong>SSH<\/strong> as the connection type (default).<\/li>\n<\/ul>\n<\/li>\n<li><strong>Connect<\/strong>:\n<ul>\n<li>Click the <strong>\"Open\"<\/strong> button (not \"OK\", as PuTTY uses \"Open\" to initiate the connection).<\/li>\n<li>If prompted with a security alert about the server's key, click <strong>\"Accept\"<\/strong> to trust the server.<\/li>\n<\/ul>\n<\/li>\n<li><strong>Log In<\/strong>:\n<ul>\n<li>Enter your username (e.g., <code>root<\/code> or another user with <code>sudo<\/code> privileges).<\/li>\n<li>Type your password when prompted (note: passwords don't display as you type for security).<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>Once logged in, you'll see a terminal prompt (e.g., <code>root@yourserver:~#<\/code>).<\/p>\n<hr>\n<\/div>\n<div class=\"wp-block-group\">\n<h2>Step 3: Update Your Server's Package Lists<\/h2>\n<p>To ensure your server has the latest package information:<\/p>\n<pre class=\"wp-block-code bash\"><code><span class=\"keyword\">apt<\/span> update<\/code><\/pre>\n<p>This command refreshes the list of available packages.<\/p>\n<hr>\n<\/div>\n<div class=\"wp-block-group\">\n<h2>Step 4: Upgrade Installed Packages<\/h2>\n<p>Install any available updates for existing packages to keep your system secure:<\/p>\n<pre class=\"wp-block-code bash\"><code><span class=\"keyword\">apt<\/span> upgrade -y<\/code><\/pre>\n<p>The <code>-y<\/code> flag automatically confirms the installation, saving you from manual prompts.<\/p>\n<hr>\n<\/div>\n<div class=\"wp-block-group\">\n<h2>Step 5: Install Required Dependency Packages<\/h2>\n<p>Install tools and dependencies needed for Docker and future installations:<\/p>\n<pre class=\"wp-block-code bash\"><code><span class=\"keyword\">apt<\/span> install curl nano git apt-transport-https ca-certificates gnupg2 software-properties-common -y<\/code><\/pre>\n<p>Here's what these packages do:<\/p>\n<ul>\n<li><code>curl<\/code>: Downloads files from the web.<\/li>\n<li><code>nano<\/code>: A simple text editor.<\/li>\n<li><code>git<\/code>: Version control system.<\/li>\n<li><code>apt-transport-https<\/code>, <code>ca-certificates<\/code>, <code>gnupg2<\/code>, <code>software-properties-common<\/code>: Enable secure repository access.<\/li>\n<\/ul>\n<hr>\n<\/div>\n<div class=\"wp-block-group\">\n<h2>Step 6: Install Docker<\/h2>\n<p>Follow these steps to install Docker on your server:<\/p>\n<ol>\n<li><strong>Add Docker's GPG Key<\/strong>:\n<ul>\n<li>For <strong>Debian<\/strong>:\n<pre class=\"wp-block-code bash\"><code><span class=\"keyword\">curl<\/span> -fsSL https:\/\/download.docker.com\/linux\/debian\/gpg || <span class=\"keyword\">gpg<\/span> --dearmor -o \/usr\/share\/keyrings\/docker-archive-keyring.gpg<\/code><\/pre>\n<\/li>\n<li>For <strong>Ubuntu<\/strong>:\n<pre class=\"wp-block-code bash\"><code><span class=\"keyword\">curl<\/span> -fsSL https:\/\/download.docker.com\/linux\/ubuntu\/gpg || <span class=\"keyword\">gpg<\/span> --dearmor -o \/usr\/share\/keyrings\/docker-archive-keyring.gpg<\/code><\/pre>\n<\/li>\n<\/ul>\n<p>This key ensures the Docker repository is trusted.<\/p>\n<\/li>\n<li><strong>Add Docker Repository<\/strong>:\n<ul>\n<li>For <strong>Debian<\/strong>:\n<pre class=\"wp-block-code bash\"><code><span class=\"keyword\">echo<\/span> <span class=\"string\">\"deb [arch=$(dpkg --print-architecture) signed-by=\/usr\/share\/keyrings\/docker-archive-keyring.gpg] https:\/\/download.docker.com\/linux\/debian $(lsb_release -cs) stable\"<\/span> | <span class=\"keyword\">tee<\/span> \/etc\/apt\/sources.list.d\/docker.list<\/code><\/pre>\n<\/li>\n<li>For <strong>Ubuntu<\/strong>:\n<pre class=\"wp-block-code bash\"><code><span class=\"keyword\">echo<\/span> <span class=\"string\">\"deb [arch=$(dpkg --print-architecture) signed-by=\/usr\/share\/keyrings\/docker-archive-keyring.gpg] https:\/\/download.docker.com\/linux\/ubuntu $(lsb_release -cs) stable\"<\/span> | <span class=\"keyword\">tee<\/span> \/etc\/apt\/sources.list.d\/docker.list<\/code><\/pre>\n<\/li>\n<\/ul>\n<p>This adds Docker's official repository to your system.<\/p>\n<\/li>\n<li><strong>Update Package Lists Again<\/strong>:\n<pre class=\"wp-block-code bash\"><code><span class=\"keyword\">apt<\/span> update<\/code><\/pre>\n<\/li>\n<li><strong>Install Docker<\/strong>:\n<pre class=\"wp-block-code bash\"><code><span class=\"keyword\">apt<\/span> install docker-ce docker-ce-cli -y<\/code><\/pre>\n<\/li>\n<li><strong>Verify Docker Installation<\/strong>:\n<ul>\n<li>Start and enable Docker:\n<pre class=\"wp-block-code bash\"><code><span class=\"keyword\">systemctl<\/span> start docker\n<span class=\"keyword\">systemctl<\/span> enable docker<\/code><\/pre>\n<\/li>\n<li>Check the version:\n<pre class=\"wp-block-code bash\"><code><span class=\"keyword\">docker<\/span> --version<\/code><\/pre>\n<p>You should see output like <code>Docker version 27.3.1, build abc123<\/code>.<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li><strong>Test Docker<\/strong>:\n<pre class=\"wp-block-code bash\"><code><span class=\"keyword\">docker<\/span> run hello-world<\/code><\/pre>\n<p>This pulls and runs a test container, confirming that Docker is working.<\/p>\n<\/li>\n<\/ol>\n<hr>\n<\/div>\n<div class=\"wp-block-group\">\n<h2>Step 7: Install Docker Compose<\/h2>\n<p>Docker Compose simplifies managing multi-container applications. Install it with these steps:<\/p>\n<ol>\n<li><strong>Download Docker Compose<\/strong>:\n<pre class=\"wp-block-code bash\"><code><span class=\"keyword\">curl<\/span> -L https:\/\/github.com\/docker\/compose\/releases\/download\/v$(<span class=\"keyword\">curl<\/span> -Ls https:\/\/www.servercow.de\/docker-compose\/latest.php)\/docker-compose-$<span class=\"keyword\">uname<\/span> -s)-$(<span class=\"keyword\">uname<\/span> -m) &gt; \/usr\/local\/bin\/docker-compose<\/code><\/pre>\n<p>This command fetches the latest Docker Compose version dynamically.<\/p>\n<\/li>\n<li><strong>Set Execution Permissions<\/strong>:\n<pre class=\"wp-block-code bash\"><code><span class=\"keyword\">chmod<\/span> +x \/usr\/local\/bin\/docker-compose<\/code><\/pre>\n<p>This makes the Docker Compose binary executable.<\/p>\n<\/li>\n<li><strong>Verify Installation<\/strong>:\n<pre class=\"wp-block-code bash\"><code><span class=\"keyword\">docker-compose<\/span> --version<\/code><\/pre>\n<p>You should see output like <code>Docker Compose version v2.24.5<\/code>.<\/p>\n<\/li>\n<\/ol>\n<hr>\n<\/div>\n<div class=\"wp-block-group\">\n<h2>Step 8: Navigate to the \/opt Directory<\/h2>\n<p>To prepare for future projects (e.g., storing Docker Compose files), switch to the <code>\/opt<\/code> directory:<\/p>\n<pre class=\"wp-block-code bash\"><code><span class=\"keyword\">cd<\/span> \/opt<\/code><\/pre>\n<p>The <code>\/opt<\/code> directory is commonly used for optional software and configurations.<\/p>\n<hr>\n<\/div>\n<div class=\"wp-block-group\">\n<h2>Step 9: Optional Configuration and Testing<\/h2>\n<ol>\n<li><strong>Run Docker Without Sudo<\/strong> (Recommended):\n<p>Add your user to the <code>docker<\/code> group to avoid typing <code>sudo<\/code> for Docker commands:<\/p>\n<pre class=\"wp-block-code bash\"><code><span class=\"keyword\">usermod<\/span> -aG docker <span class=\"variable\">$USER<\/span><\/code><\/pre>\n<p>Log out and back in (or reconnect via PuTTY) for this to take effect.<\/p>\n<\/li>\n<li><strong>Test Docker Compose<\/strong>:\n<ul>\n<li>Create a sample <code>docker-compose.yml<\/code> file:\n<pre class=\"wp-block-code bash\"><code><span class=\"keyword\">nano<\/span> \/opt\/docker-compose.yml<\/code><\/pre>\n<\/li>\n<li>Add this content:\n<pre class=\"wp-block-code yaml\"><code><span class=\"key\">version<\/span>: <span class=\"string\">'3.8'<\/span>\n<span class=\"key\">services<\/span>:\n  <span class=\"key\">web<\/span>:\n    <span class=\"key\">image<\/span>: <span class=\"string\">nginx:latest<\/span>\n    <span class=\"key\">ports<\/span>:\n      - <span class=\"string\">\"8080:80\"<\/span><\/code><\/pre>\n<\/li>\n<li>Save and exit (<code>Ctrl+O<\/code>, <code>Enter<\/code>, <code>Ctrl+X<\/code>).<\/li>\n<li>Run the Compose file:\n<pre class=\"wp-block-code bash\"><code><span class=\"keyword\">cd<\/span> \/opt\n<span class=\"keyword\">docker-compose<\/span> up -d<\/code><\/pre>\n<\/li>\n<li>Verify the container is running:\n<pre class=\"wp-block-code bash\"><code><span class=\"keyword\">docker<\/span> ps<\/code><\/pre>\n<\/li>\n<li>Open a browser and visit <code>http:\/\/your-server-ip:8080<\/code> to see the Nginx welcome page.<\/li>\n<li>Stop the containers:\n<pre class=\"wp-block-code bash\"><code><span class=\"keyword\">docker-compose<\/span> down<\/code><\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li><strong>Configure Docker Daemon<\/strong> (Optional):\n<ul>\n<li>Edit Docker's settings (e.g., for logging limits):\n<pre class=\"wp-block-code bash\"><code><span class=\"keyword\">nano<\/span> \/etc\/docker\/daemon.json<\/code><\/pre>\n<\/li>\n<li>Add:\n<pre class=\"wp-block-code json\"><code>{\n  <span class=\"key\">\"log-driver\"<\/span>: <span class=\"string\">\"json-file\"<\/span>,\n  <span class=\"key\">\"log-opts\"<\/span>: {\n    <span class=\"key\">\"max-size\"<\/span>: <span class=\"string\">\"10m\"<\/span>,\n    <span class=\"key\">\"max-file\"<\/span>: <span class=\"string\">\"3\"<\/span>\n  }\n}<\/code><\/pre>\n<\/li>\n<li>Restart Docker:\n<pre class=\"wp-block-code bash\"><code><span class=\"keyword\">systemctl<\/span> restart docker<\/code><\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<hr>\n<\/div>\n<div class=\"wp-block-group\">\n<h2>Step 10: Maintenance Tips<\/h2>\n<ul>\n<li><strong>Update Docker<\/strong>:\n<pre class=\"wp-block-code bash\"><code><span class=\"keyword\">apt<\/span> update &amp;&amp;&amp; <span class=\"keyword\">apt<\/span> upgrade docker-ce -y<\/code><\/pre>\n<\/li>\n<li><strong>Update Docker Compose<\/strong>: Repeat Step 7.1 with the latest version.<\/li>\n<li><strong>Clean Up Unused Resources<\/strong>:\n<pre class=\"wp-block-code bash\"><code><span class=\"keyword\">docker<\/span> system prune -a<\/code><\/pre>\n<\/li>\n<li><strong>Check Logs for Issues<\/strong>:\n<pre class=\"wp-block-code bash\"><code><span class=\"keyword\">journalctl<\/span> -u docker<\/code><\/pre>\n<\/li>\n<\/ul>\n<hr>\n<\/div>\n<div class=\"wp-block-group gezegd<h2>Troubleshooting<\/h2><ul><li><strong>PuTTY Connection Fails<\/strong>:<ul><li>Verify your server\u2019s IP, port (22), and credentials.<\/li><li>Ensure SSH is enabled on your server (<code>sudo systemctl status ssh<\/code>).<\/li><\/ul><\/li><li><strong>Docker Not Starting<\/strong>:<ul><li>Check status: <code>systemctl status docker<\/code>.<\/li><li>View logs: <code>journalctl -u docker<\/code>.<\/li><\/ul><\/li><li><strong>Docker Compose Errors<\/strong>:<ul><li>Validate YAML: <code>docker-compose config<\/code>.<\/li><li>Ensure the binary is executable: <code>ls -l \/usr\/local\/bin\/docker-compose<\/code>.<\/li><\/ul><\/li><\/ul><hr><\/div><div class=\"wp-block-group\">\n<h2>Conclusion<\/h2>\n<p>Congratulations! You've connected to your VPS or root server using PuTTY, installed Docker and Docker Compose, and set up a basic configuration. You're now ready to deploy containerized applications. Try experimenting with Docker Compose files to run databases, web servers, or full-stack apps. If you run into issues or want to explore advanced setups, leave a comment below or reach out!<\/p>\n<p>Happy containerizing!<\/p>\n<hr>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Step-by-Step Guide to Installing Docker and Docker Compose on Your VPS or Root Server Docker and Docker Compose are powerful tools for running and managing containerized applications. Whether you&#8217;re deploying web apps, databases, or microservices, setting them up on a VPS or root server is straightforward. In this guide, I\u2019ll walk you through connecting to [&hellip;]<\/p>","protected":false},"author":1,"featured_media":1950,"comment_status":"open","ping_status":"closed","sticky":true,"template":"","format":"standard","meta":{"_acf_changed":false,"jetpack_post_was_ever_published":false,"footnotes":""},"categories":[48],"tags":[5,43,42],"class_list":["post-1952","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-how-to","tag-docker","tag-how-to","tag-install"],"acf":[],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/havari.me\/wp-content\/uploads\/2024\/09\/i-have-wordpress-personal-blog-i-need-to-create-fe-3.png?fit=2048%2C1024&ssl=1","_links":{"self":[{"href":"https:\/\/havari.me\/en\/wp-json\/wp\/v2\/posts\/1952","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/havari.me\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/havari.me\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/havari.me\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/havari.me\/en\/wp-json\/wp\/v2\/comments?post=1952"}],"version-history":[{"count":4,"href":"https:\/\/havari.me\/en\/wp-json\/wp\/v2\/posts\/1952\/revisions"}],"predecessor-version":[{"id":2192,"href":"https:\/\/havari.me\/en\/wp-json\/wp\/v2\/posts\/1952\/revisions\/2192"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/havari.me\/en\/wp-json\/wp\/v2\/media\/1950"}],"wp:attachment":[{"href":"https:\/\/havari.me\/en\/wp-json\/wp\/v2\/media?parent=1952"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/havari.me\/en\/wp-json\/wp\/v2\/categories?post=1952"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/havari.me\/en\/wp-json\/wp\/v2\/tags?post=1952"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}