Getting Started With Docker On Your VPS (Tutorial)
Getting started with Docker on your VPS is pretty straightforward, and once you’re set up via this Docker tutorial, it’s like you’ve “leveled up” in DevOps.
Why get started with Docker on your VPS?
Docker is a platform for designing, building, and launching “containers,” which are isolated environments containing all the software and configuration files necessary to run a service or application.
Running Docker on your virtual server gives you more stability, more flexibility, and a lot more ways to recover quickly if (or when) you mess things up.
Get THE BEST DEALS IN CLOUD HOSTING from Los Angeles!Grab a huge 32GB RAM & 320GB of SSD storage for just $109/year!
Installing Docker on your machine brings significant benefits:
- You can develop locally using a specific environment.
- You can ensure that local development environment is replicated exactly when you’re ready to deploy code, ensuring 100% compatibility.
- Build Dockerfiles/Docker images to make your site/app portable to multiple VPSs for redundancy or fail-over.
- Host multiple applications on a single VPS without them interacting—or conflict—with one another. For example, run two WordPress installations with separate Apache/Nginx web servers and separate MySQL databases.
- One container can crash without affecting other containers or the health of your VPS.
- Capability to automatically restart containers upon reboot of the VPS itself.
- A certain degree of improved security by separating different apps into different containers.
- Easy backups!
It’s clear that Docker should probably have a role on your VPS.Let’s get started on installing Docker and taking the first steps toward a container-powered VPS.
Get a $50 Bonus for 72 hours only…
To celebrate our newest datacenters in Tokyo & London, we’re offering a $50 hosting credit with any 3-year hosting plan. And a $20 Bonus with a 1-year plan. Use your bonus to add backups, discount your renewal or add a new server, super cheap. We’re shutting this one down soon, though. So get there now…
Score a 24GB Performance VPS with 240GB of SSD storage for just $99/year…PLUS a $50 Credit!)
Prerequisites for this getting started with Docker tutorial
- A virtual private server (VPS) running any of our available OS options. See our pricing for details.
- A non-root user account (commands requiring escalated privileges will use
Step 1. Installing Docker
You can get started with Docker by installing it with a single command. Type the following into your VPS and the script will take care of the rest:
$ sudo curl -sS https://get.docker.com/ | sh
The script checks your operating system, downloads and installs package repositories, installs Docker itself alongside any dependencies, and starts the Docker service.
Note: Running scripts in this way can be risky. You’re trusting the developers and the script they wrote, with sudo access to your machine. It’s always best to check out the script before running it to make sure there’s no funny business.
Step 2. Testing your Docker installation
The people behind Docker recommend testing out your Docker installation with a basic
Hello world command to ensure everything is working the way it should. If so, you’ll see the following output:
$ sudo docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 78445dd45222: Pull complete Digest: sha256:c5515758d4c5e1e838e9cd307f6c6a0d620b5e07e6f927b07d05f6d12a1ac8d7 Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://cloud.docker.com/ For more examples and ideas, visit: https://docs.docker.com/engine/userguide/
Step 3. Some post-installation configurations
Now that Docker is installed, let’s take a moment to make a few quality-of-life improvements. These will help make Docker a little easier to use on a day-to-day basis:
- Enable Docker to start automatically after a reboot.
- Allow the non-root user to manage Docker.
In CentOS, Debian, and Ubuntu 16.04,
systemd is responsible for managing which services start when the system boots up. That means you can enable this with a single command.
$ sudo systemctl enable docker
Once the Docker service is set to start automatically upon rebooting your VPS, all Docker containers will also spin up automatically. All your apps running in Docker containers will come back online without any manual intervention.
Once you have enabled the
docker service itself, you do not need to take any further steps. This means that any application you run via Docker will gracefully restart after boot, potentially minimizing downtime (as long as the services inside the container are set up to start at boot themselves).
In order to give our non-root user access to the Docker management commands, we need to create a
docker group (it may already be created for you), and then add your primary user to that group.
$ sudo groupadd docker $ sudo usermod -aG docker $USER
Log out of your VPS by typing
Ctrl+D and log back in. Then, you can test whether or not you can use the
docker command without prepending
$ docker run hello-world
Compose is a tool that helps simplify the configuration and deployment of Docker containers and applications by using an easy-to-read
.yaml file. In some cases, this will be easier than writing out a lengthy command for the shell prompt.
As of April 3, 2019,
1.24.0 is the newest version of
docker-compose. You may want to check the releases page to see if there’s a new version, and then replace the
1.24.0 in the command below with the newer version number.
$ sudo -i # curl -L https://github.com/docker/compose/releases/download/1.24.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose # chmod +x /usr/local/bin/docker-compose
Step 4. Testing Docker with a basic LAMP stack
Now we can get to the exciting bit—getting started running some actual applications in Docker containers. No more
We’ll start by creating a very basic LAMP stack using the
php:apache container available from Docker. But, before that, let’s create a directory on the host to store our files, which we’ll link to the
/var/www/html directory within the container.
$ mkdir $HOME/apache && cd $HOME/apache
Then, we can create a small PHP file named
info.php that will display information about the PHP configuration. It’s a standard method of testing PHP-based installations.
$ printf '<?phpn ;phpinfo(); n?>' > info.php
Finally, we have our
docker command. But, before you run it, check out the information just beneath the command so that you can understand exactly what it’s accomplishing.
$ docker run -d --name=apache -p 8080:80 -v $HOME/apache:/var/www/html php:apache
docker run specifies that we are going to create and start a new container, and the
-d option means we will “detach” from it, much the way one detaches from a
tmux session or an
ssh session. In cases where you want to immediately run commands inside the newly-created container, you can omit the
--name=apache to give the container a specific name. This is recommended, because your chosen names will be easier to manage and remember than the randomized defaults—comes in handy when you want to stop or delete a container.
-p 8080:80 will expose port
8080 to traffic arriving on the VPS, and will route that traffic to port 80 on the container. This makes it possible to expose different containers to different ports, and enable more complex configurations with an
nginx reverse proxy.
-v $HOME/apache:/var/www/html is a virtual drive mapping. In this case, any files in the directory before the colon,
$HOME/apache, will be available in the
/var/www/html directory inside the container.
docker which image to use. More images can be found on the Docker store.
You should now be able to see that the container is running with the
docker ps command:
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d1fbdb7e0c5f php:apache "docker-php-entryp..." 3 seconds ago Up 3 seconds 0.0.0.0:8080->80/tcp apache
You can now also access your basic Apache web server by visiting
http://YOUR-SERVER-IP:8080/info.php in your favorite browser. If all has gone correctly, you’ll see something like the following:
Now, for the sake of showing some more core
docker commands, let’s gracefully shut down this container, delete the container, followed by the image itself.
$ docker stop apache $ docker rm apache $ docker rmi php:apache
Step 5. And WordPress, for good measure
Okay, we’ve gotten started with Docker and tested the LAMP stack.
So, now let’s take the LAMP stack a step further with a full-blown WordPress installation, and this time, let’s also use
docker-compose to make the process a little bit more human-readable.
The first step is creating a new directory for this project.
$ mkdir wp_test && cd wp_test
Then, create a
docker-compose.yml file that will specify the configuration. This will create two containers: one running Apache/Wordpress, and another running the
mysql instance, with data persisted between reboots and container shutdowns. Of course, for production use, you will want to change the passwords to be more secure.
version: '2' services: db: image: mysql:5.7 volumes: - db_data:/var/lib/mysql restart: always environment: MYSQL_ROOT_PASSWORD: somewordpress MYSQL_DATABASE: wordpress MYSQL_USER: wordpress MYSQL_PASSWORD: wordpress container_name: wp_test_db wordpress: depends_on: - db image: wordpress:latest ports: - "8080:80" restart: always environment: WORDPRESS_DB_HOST: db:3306 WORDPRESS_DB_USER: wordpress WORDPRESS_DB_PASSWORD: wordpress container_name: wp_test volumes: db_data:
To launch the container for the first time, use the
docker-compose up command.
$ docker-compose up -d
Now, you can check on these new containers using
$ docker ps 20570a5eb798 wordpress:latest "docker-entrypoint..." 3 seconds ago Up 2 seconds 0.0.0.0:8080->80/tcp wp_test c1872cb1443d mysql:5.7 "docker-entrypoint..." 3 seconds ago Up 3 seconds 3306/tcp wp_test_db
Of course, the WordPress installation is now available on
http://YOUR-SERVER-IP:8080, for you to begin the famous 5-minute installation. And, if for any reason, you need to shut down these containers while retaining the data, use
Getting started with Docker: Next Steps
We hope you’re excited to get started taking full advantage of Docker’s container technology on your VPS.
By offloading services to containers, you can keep your base OS cleaner, with fewer attack vectors, and with less risk of various applications conflicting with one another.
Plus, it’s much safer to make mistakes with containers! All you need to do is stop the container, remove it, and try again, without worrying that you’re cluttering up your system or potentially breaking it.
If you want to get started leveling up your Docker skills, check out a few of our Docker tutorials:
- Host multiple websites on one VPS with Docker and Nginx
- Hosting multiple SSL-enabled sites with Docker and Nginx
- Self-hosting with Docker: The definitive handbook
Enjoy your containers! And, while it’s definitely possible, we can’t recommend running Docker inside of Docker.