In this tutorial, we will focus on the basics of Docker and how it can be used for application containerization. Docker is a powerful platform that allows you to develop, ship, and run applications in a portable and self-sufficient container environment.
By the end of this tutorial, you will be able to:
1. Understand the basic concepts of Docker
2. Create a Dockerfile
3. Build a Docker image
4. Run a Docker container
Prerequisites:
- Basic knowledge of command line interface
- Docker installed on your machine. If not, you can download it from the official Docker website
Docker is a tool designed to make it easier to create, deploy, and run applications using containers. Containers allow developers to package up an application with all of its parts it needs, such as libraries and other dependencies, and ship it all out as one package.
A Dockerfile is a text file that contains all the commands a user could call on the command line to assemble an image.
Example Dockerfile:
# Use an official Python runtime as a parent image
FROM python:3.7-slim
# Set the working directory in the container
WORKDIR /app
# Copy the current directory contents into the container at /app
ADD . /app
# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 80
# Run app.py when the container launches
CMD ["python", "app.py"]
To build your image, go to the directory that has your Dockerfile and run the following command:
docker build -t friendly-hello .
In this command, -t
tags your image. Think of it as a human-friendly name for your final product. The .
tells Docker to use the current directory (i.e., the Dockerfile must be present in the current directory).
To run the image and create a container from it, use the following command:
docker run -p 4000:80 friendly-hello
# This is a comment
# We are using the alpine version of the official node image
FROM node:alpine
# Create a directory named app in the container
WORKDIR /app
# Copy package.json from current directory to the present working directory in the container
COPY package.json .
# Run npm install command to install the dependencies
RUN npm install
# Copy rest of the application to /app in container
COPY . .
# Expose the port 8080 for the application to be accessible
EXPOSE 8080
# Define the command to run the application
CMD ["npm", "start"]
Running the command docker build -t my-app .
will produce an image named my-app
.
Let's run the my-app
image we created in the previous example.
docker run -p 8080:8080 my-app
This command maps the port 8080 of the host to the port 8080 of the container where our application is running.
In this tutorial, we learned about Docker, Dockerfile, how to build a Docker image, and how to run a Docker container. The next steps would be to learn about Docker Compose, which allows you to run multi-container Docker applications.
Additional resources:
- Docker Documentation: https://docs.docker.com/
- Dockerfile reference: https://docs.docker.com/engine/reference/builder/
Solution:
1. Dockerfile:
FROM python:3.7
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
CMD ["python", "app.py"]
docker build -t my-python-app .
docker run -p 5000:5000 my-python-app
Keep practicing with different applications and different types of Dockerfiles to get better at using Docker!