Docker For Beginners
What is Docker?
You might have heard this word before and might sound pretty intimidating at first especially when introduced to it.
Docker is a containerization platform that packages your app and all its dependencies together in the form called a docker container to ensure that your application works seamlessly in any environment. This environment might be a production or staging server. Docker pulls the dependencies needed for your application to run from the cloud and configures them automatically. You don’t need to do any extra work. Cool Right?
Docker can also be used when we want to deploy an application to an environment which might have a conflict with the present environment settings.
Imagine we already have an application running PHP 5.3 on a server and want to deploy a new application which requires PHP 7.2 on that same server. This will cause some version conflict on that server and also might cause some features in the existing application to fail……
In situations like this, we might have to use Docker to sandbox or containerise the new application to run without affecting the old application. This brings us to Docker containers.
What is a Docker Container
Think of a Docker container as above image. There are multiple applications running on the same machine. These applications are put into docker containers and any changes made on these containers does not affect the other container. Each container has different Os running on the same physical machine. Docker helps you to create, deploy and run applications using containers.
A container packages up the code and all its dependencies so the application runs quickly and reliably from one computing environment to another.
A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings.
Creating Our First Docker Application
Let say we have a PHP application and want to deploy it to our staging or production server. First, we make sure we have docker configuration script included in the root directory of the application.
- Create a Dockerfile in your application
Create a file with name Dockerfile
at the root of your application and include the code below to tell docker what to do when running in the production or staging environment
FROM PHP:7.2-Apache
COPY src/ /var/www/html/
EXPOSE 80
Above is a sample docker script which configures PHP version 7.2 on a staging or production server, copy the PHP files from /src
directory to /var/www/html/
and expose the port 80 to be reached on.
2. Installing Docker on Staging Or Production Server
For Mac get docker here.
For Windows go here.
3. Running Docker
After docker is installed on the staging or production server, click on the whale icon to run docker
4. Deploying Your Application
Copy the application to the staging or production server and do the following
- Navigate to the project directory on the terminal and create a docker image.
Run the following command in the terminal and it will create a docker image of the application and download all the necessary dependencies needed for the application to run successfully
docker build -t <name to give to your image>
- Convert Docker image of the Application into a Running container.
Run the following command in terminal and it will use create a running container with all the needed dependencies and start the application.
docker run -p 9090:80 <name to give to your container>
The 9090
is the port we want to access our application on. 80
is the port the container is exposing for the host to access.
Below are some useful Docker commands
Stopping a running image
docker stop <id-of-image>
Starting an image which is not running
docker start <id-of-image>
Removing an image from docker
docker rmi <id-of-image>
Removing a container from docker
docker rm <id-of-container>
Do you need to hire top developers? Talk to Andela to help you scale.
Are you looking to accelerate your career as a developer? Andela is currently hiring senior developers. Apply now.