In this tutorial, we'll delve into the world of Docker, a popular platform used to simplify the process of deploying applications. Specifically, we'll focus on building and running Docker images.
By the end of this tutorial, you'll be able to:
To get the most out of this tutorial, you should have:
A Docker image is a lightweight, standalone, executable package that includes everything needed to run a piece of software. A Dockerfile is a text file that contains a series of instructions used to create a Docker image.
To build a Docker image, navigate to the directory that contains your Dockerfile and run the docker build
command:
docker build -t my-app .
Here, -t my-app
assigns the name my-app
to your image, and .
specifies that Docker should look for the Dockerfile in the current directory.
Once you've built your image, run it as a container using the docker run
command:
docker run -d -p 8080:8080 my-app
Here, -d
runs the container in detached mode (in the background), and -p 8080:8080
maps port 8080 of the container to port 8080 on the host machine.
Here's a simple Dockerfile for a Node.js application:
# Use an official Node.js runtime as the base image
FROM node:14
# Set the working directory in the container to /app
WORKDIR /app
# Copy package.json and package-lock.json to the working directory
COPY package*.json ./
# Install the app dependencies
RUN npm install
# Copy the rest of the application code to the working directory
COPY . .
# Expose port 8080
EXPOSE 8080
# Define the command to run the app
CMD [ "node", "app.js" ]
After building this image with docker build -t my-node-app .
and running it with docker run -d -p 8080:8080 my-node-app
, your app will be accessible at http://localhost:8080
.
# Use an official Python runtime as a base image
FROM python:3.8
# Set the working directory
WORKDIR /app
# Install the 'requests' package using pip
RUN pip install requests
# Copy a Python script into the container
COPY script.py .
# Run the script when the container launches
CMD [ "python", "./script.py" ]
In this tutorial, we've covered the basics of Docker images and Dockerfiles, including how to build and run Docker images. The next steps in your Docker journey could include learning how to use Docker Compose to manage multi-container applications, or how to use Docker with continuous integration/continuous deployment (CI/CD) systems.
Solution:
# Use an official Python runtime as a base image
FROM python:3.8
# Set the working directory
WORKDIR /app
# Copy a Python script into the container
COPY script.py .
# Run the script when the container launches
CMD [ "python", "./script.py" ]
Where script.py
contains:
print("Hello, Docker!")
Then build and run the image:
docker build -t my-python-app .
docker run -it --rm my-python-app
script.py
.The solution follows a similar pattern to Example 2 in the "Code Examples" section.
Remember, practice is key when learning new concepts, so don't be afraid to experiment with different Dockerfiles and applications. Happy coding!