So, what is Docker?
Docker Official Website: Link
As described on IBM’s website, “Docker is an open source containerization platform. It enables developers to package applications into containers—standardized executable components combining application source code with the operating system (OS) libraries and dependencies required to run that code in any environment”. To this action of packaging an app, we call it containerization.
Some advantages of a containerized application:
- Faster and more secure deployments;
- No need to setup a specific environment to run the application;
Ok, great! But how can I deploy my application as a container?
Let's take this website as an example.
Currently my website runs on a VPS alongside some other applications on a containerized infrastructure (check the infrastructure diagram below).
As you can see, a single VPS running nothing more than the OS, Docker and Apache Webserver (used to redirect each URL to a specific port), is able to host multiple applications built on totally different stacks.
Creating a Dockerfile
In order to deploy a Docker container to run your application, first you need to build a Docker image. The best way of doing this is through the creation of a Dockerfile.
As mentioned on the Docker website "A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.". Then, Docker can build images automatically by reading the instructions from this file.
The Dockerfile used to build the image of this website:
# Image to be installed
# Create app dir
RUN mkdir -p /usr/src/<APP_NAME>
# Copy the app content to the newly created dir
COPY . /usr/src/<APP_NAME>
# Start working from the new dir
# Setup dependencies
RUN apt update
RUN pip3 install --no-cache-dir -r requirements.txt
# App port
# Start app on boot
ENTRYPOINT [ "python3.8", "app.py" ]
In order to build the image, you just need to have Docker installed (check the install instructions here). After installed, through command line, run the following commands:
(On the folder where the Dockerfile is stored)
# Build image
docker build .
# List images
docker image ls
Uploading the image to Docker Hub
After the image is built, you can remotely store it on Docker Hub. In order to do this, first you need to create an account at Docket Hub and, when done, run the following commands through the command line:
# Login into Docker Hub
docker login -u <USERNAME>
# Add a tag to the image
docker tag <IMAGE_ID> <ACCOUNT>/<IMAGE>:<TAG>
# Upload it
docker push <ACCOUNT>/<IMAGE>:<TAG>
How to deploy image
When done, connect to your VPS, pull the Docker image and execute it. To do this, you just need to run these commands:
# Pull Docker image
docker pull <ACCOUNT>/<IMAGE>:<TAG>
# Start container in background
docker run -p <LOCAL_PORT>:<CONTAINER_PORT> -d --name <APP_NAME> <IMAGE_ID>
NOTE: In this case I use the flag "-p" to map the container port on the server. With this, I'm able to have each application running on different server ports and then I just need to redirect the URL to the matching application port.
And that's it! I hope you find this blog post useful and, if you find any issue, please send me an email and I'll get back to you asap.