Основы Docker. Создание образа, запуск контейнера
3 min read
2 hours ago
Published on Sep 18, 2025
This response is partially generated with the help of AI. It may contain inaccuracies.
Table of Contents
Introduction
This tutorial will guide you through the basics of Docker, focusing on creating Docker images and running containers. By the end of this guide, you'll have a working understanding of Docker concepts and the ability to deploy a simple web application.
Step 1: Understand Docker
-
What is Docker?
- Docker is a platform that allows developers to automate the deployment of applications inside software containers.
- Containers package an application and its dependencies, ensuring consistent environments across development and production.
-
Why Use Docker?
- Simplifies application deployment.
- Provides portability and scalability.
- Facilitates resource efficiency and isolation.
Step 2: Learn About Images and Containers
-
Docker Images
- An image is a lightweight, standalone, executable package that includes everything needed to run a piece of software.
-
Docker Containers
- A container is a running instance of an image. It can be started, stopped, moved, and deleted independently.
Step 3: Set Up Your Project
-
Clone the Repository
- Use the following command to clone the sample web application repository:
git clone https://github.com/eugene-okulik/cool_web_app
- Use the following command to clone the sample web application repository:
-
Navigate to the Project Directory
- Change to the project directory:
cd cool_web_app
- Change to the project directory:
Step 4: Create a Dockerfile
-
What is a Dockerfile?
- A Dockerfile is a text document that contains all the commands to assemble an image.
-
Basic Structure of a Dockerfile
- Create a file named
Dockerfile
in the project directory and add the following commands:FROM node:14 WORKDIR /app COPY package*.json ./ RUN npm install COPY . . CMD ["npm", "start"]
- This Dockerfile uses the Node.js base image, sets the working directory, installs dependencies, and specifies the command to run the app.
- Create a file named
Step 5: Build the Docker Image
- Build the Image
- Use the following command to build your Docker image:
docker build -t cool_web_app .
- The
-t
flag tags the image with a name for easy reference.
- Use the following command to build your Docker image:
Step 6: Run the Docker Container
- Start the Container
- Run your newly created Docker image with this command:
docker run -d -p 3000:3000 cool_web_app
- The
-d
flag runs the container in detached mode, and-p
maps port 3000 of your machine to port 3000 of the container.
- Run your newly created Docker image with this command:
Step 7: Stop the Docker Container
- Stop the Running Container
- To stop the container, first find its container ID with:
docker ps
- Then use the following command to stop it:
docker stop <container_id>
- To stop the container, first find its container ID with:
Step 8: Map Ports
- Understanding Port Mapping
- Port mapping allows you to access applications running in a Docker container through ports on your host machine.
- You can change the host port if 3000 is already in use by modifying the
-p
flag in the run command to something like-p 8080:3000
.
Conclusion
In this tutorial, you learned the basics of Docker, including how to create a Dockerfile, build an image, run a container, and manage ports. You can now deploy simple applications using Docker. For further learning, explore the Docker documentation and experiment with more complex applications. Happy Dockering!