Table of Contents
1. Understand What Docker Is (and Isn’t)
2. Install Docker
3. Learn Basic Docker Commands
4. Build Your Own Image with a Dockerfile
5. Use Docker Compose for Multi-Container Apps
6. Tips for Beginners
Home Operation and Maintenance Docker How to get started with docker

How to get started with docker

Aug 16, 2025 pm 01:46 PM
getting Started docker

Docker is a platform for packaging, shipping, and running applications in lightweight, isolated containers that share the host OS kernel, unlike virtual machines. 2. Install Docker Desktop on Windows or macOS, or use the curl command on Linux, then test with docker --version and docker run hello-world. 3. Learn core commands: docker run to start containers, docker ps to list them, docker stop and docker rm to manage lifecycle, and docker images and rmi for image management; run nginx with docker run -d -p 8080:80 nginx to test locally. 4. Build a custom image using a Dockerfile: create a Node.js app with index.js, write a Dockerfile specifying node:18, COPY, EXPOSE, and CMD, then build with docker build -t my-node-app . and run it on port 3000. 5. Use Docker Compose for multi-container setups: define services like web and Redis in docker-compose.yml with version, services, build, ports, and depends_on, then launch with docker-compose up -d. 6. Best practices include naming containers and images clearly, using .dockerignore, choosing slim base images, avoiding hardcoded secrets, and debugging with docker logs; the key to mastering Docker is hands-on practice by building, breaking, and fixing containers to gain confidence quickly.

How to get started with docker

Getting started with Docker doesn’t have to be overwhelming. If you're new to containerization, the key is to understand the core concepts and get hands-on quickly. Here’s a practical guide to help you begin using Docker effectively.

How to get started with docker

1. Understand What Docker Is (and Isn’t)

Docker is a platform that allows you to package, ship, and run applications in isolated environments called containers. Unlike virtual machines, containers share the host OS kernel, making them lightweight and fast.

Key terms to know:

How to get started with docker
  • Image: A blueprint for a container (like a class in programming).
  • Container: A running instance of an image (like an object from a class).
  • Dockerfile: A script with instructions to build an image.
  • Docker Hub: A public registry where you can find or share images.

Think of it this way: if you’ve ever had the “it works on my machine” problem, Docker helps eliminate that by making your app run the same everywhere.


2. Install Docker

Before doing anything, install Docker on your system.

How to get started with docker
  • Windows or macOS: Download Docker Desktop from the official site. It includes Docker Engine, CLI, and a GUI.
  • Linux (Ubuntu/Debian): Use the package manager:
    curl -fsSL https://get.docker.com | sh
    sudo usermod -aG docker $USER

    Log out and back in so your user gets Docker permissions.

After installation, test it:

docker --version
docker run hello-world

If you see a "Hello from Docker" message, you're good to go.


3. Learn Basic Docker Commands

Start playing with these essential commands:

  • docker run [image] – downloads and starts a container from an image.

    docker run nginx

    This runs an Nginx web server in a container.

  • docker ps – lists running containers.

  • docker ps -a – lists all containers (including stopped ones).

  • docker stop [container] – stops a running container.

  • docker rm [container] – removes a container.

  • docker images – shows downloaded images.

  • docker rmi [image] – removes an image.

Try running a simple web app:

docker run -d -p 8080:80 nginx
  • -d runs it in the background (detached mode).
  • -p 8080:80 maps port 8080 on your machine to port 80 in the container.

Now open http://localhost:8080 in your browser—you’ll see the Nginx welcome page.


4. Build Your Own Image with a Dockerfile

Let’s create a simple Node.js app container.

  1. Make a directory:

    mkdir myapp && cd myapp
  2. Create a simple index.js:

    const http = require('http');
    const server = http.createServer((req, res) => {
      res.end('Hello from Docker!');
    });
    server.listen(3000);
  3. Create a Dockerfile (no extension):

    FROM node:18
    WORKDIR /app
    COPY . .
    EXPOSE 3000
    CMD ["node", "index.js"]
  4. Build the image:

    docker build -t my-node-app .
  5. Run it:

    docker run -d -p 3000:3000 my-node-app

Visit http://localhost:3000 and see your message.

This process teaches you how to turn any app into a portable container.


5. Use Docker Compose for Multi-Container Apps

When your app needs multiple services (like a web server and a database), use docker-compose.yml.

Example: Run a Node.js app with Redis.

  1. Create docker-compose.yml:

    version: '3.8'
    services:
      web:
        build: .
        ports:
          - "3000:3000"
        depends_on:
          - redis
      redis:
        image: redis
  2. Run it:

    docker-compose up -d
  3. Now your app and Redis are running together. This is how real-world apps scale with Docker.


    6. Tips for Beginners

    • Always name your containers and images clearly.
    • Use .dockerignore to exclude files (like node_modules) from being copied.
    • Keep images small—use official slim/base images when possible.
    • Never store sensitive data directly in images (use environment variables or secrets).
    • Read logs with docker logs [container] when things go wrong.

    Getting started with Docker is mostly about doing. Build a few images, break things, fix them. The learning curve flattens fast once you’ve run your first container. Basically, just start small and iterate.

    The above is the detailed content of How to get started with docker. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

ArtGPT

ArtGPT

AI image generator for creative art from text prompts.

Stock Market GPT

Stock Market GPT

AI powered investment research for smarter decisions

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

How to set environment variables in PHP environment Description of adding PHP running environment variables How to set environment variables in PHP environment Description of adding PHP running environment variables Jul 25, 2025 pm 08:33 PM

There are three main ways to set environment variables in PHP: 1. Global configuration through php.ini; 2. Passed through a web server (such as SetEnv of Apache or fastcgi_param of Nginx); 3. Use putenv() function in PHP scripts. Among them, php.ini is suitable for global and infrequently changing configurations, web server configuration is suitable for scenarios that need to be isolated, and putenv() is suitable for temporary variables. Persistence policies include configuration files (such as php.ini or web server configuration), .env files are loaded with dotenv library, and dynamic injection of variables in CI/CD processes. Security management sensitive information should be avoided hard-coded, and it is recommended to use.en

Creating Production-Ready Docker Environments for PHP Creating Production-Ready Docker Environments for PHP Jul 27, 2025 am 04:32 AM

Using the correct PHP basic image and configuring a secure, performance-optimized Docker environment is the key to achieving production ready. 1. Select php:8.3-fpm-alpine as the basic image to reduce the attack surface and improve performance; 2. Disable dangerous functions through custom php.ini, turn off error display, and enable Opcache and JIT to enhance security and performance; 3. Use Nginx as the reverse proxy to restrict access to sensitive files and correctly forward PHP requests to PHP-FPM; 4. Use multi-stage optimization images to remove development dependencies, and set up non-root users to run containers; 5. Optional Supervisord to manage multiple processes such as cron; 6. Verify that no sensitive information leakage before deployment

How to make PHP container support automatic construction? Continuously integrated CI configuration method of PHP environment How to make PHP container support automatic construction? Continuously integrated CI configuration method of PHP environment Jul 25, 2025 pm 08:54 PM

To enable PHP containers to support automatic construction, the core lies in configuring the continuous integration (CI) process. 1. Use Dockerfile to define the PHP environment, including basic image, extension installation, dependency management and permission settings; 2. Configure CI/CD tools such as GitLabCI, and define the build, test and deployment stages through the .gitlab-ci.yml file to achieve automatic construction, testing and deployment; 3. Integrate test frameworks such as PHPUnit to ensure that tests are automatically run after code changes; 4. Use automated deployment strategies such as Kubernetes to define deployment configuration through the deployment.yaml file; 5. Optimize Dockerfile and adopt multi-stage construction

How to build an independent PHP task container environment. How to configure the container for running PHP timed scripts How to build an independent PHP task container environment. How to configure the container for running PHP timed scripts Jul 25, 2025 pm 07:27 PM

Building an independent PHP task container environment can be implemented through Docker. The specific steps are as follows: 1. Install Docker and DockerCompose as the basis; 2. Create an independent directory to store Dockerfile and crontab files; 3. Write Dockerfile to define the PHPCLI environment and install cron and necessary extensions; 4. Write a crontab file to define timing tasks; 5. Write a docker-compose.yml mount script directory and configure environment variables; 6. Start the container and verify the log. Compared with performing timing tasks in web containers, independent containers have the advantages of resource isolation, pure environment, strong stability, and easy expansion. To ensure logging and error capture

How to use Kubernetes to keep PHP environment consistent Production and local container configuration standards How to use Kubernetes to keep PHP environment consistent Production and local container configuration standards Jul 25, 2025 pm 06:21 PM

To solve the problem of inconsistency between PHP environment and production, the core is to use Kubernetes' containerization and orchestration capabilities to achieve environmental consistency. The specific steps are as follows: 1. Build a unified Docker image, including all PHP versions, extensions, dependencies and web server configurations to ensure that the same image is used in development and production; 2. Use Kubernetes' ConfigMap and Secret to manage non-sensitive and sensitive configurations, and achieve flexible switching of different environment configurations through volume mounts or environment variable injection; 3. Ensure application behavior consistency through unified Kubernetes deployment definition files (such as Deployment and Service) and include in version control; 4.

How to install Docker on CentOS How to install Docker on CentOS Sep 23, 2025 am 02:02 AM

Uninstall the old version of Docker to avoid conflicts, 2. Install yum-utils and add the official Docker repository, 3. Install DockerCE, CLI and containerd, 4. Start and enable Docker services, 5. Run hello-world image to verify that the installation is successful, 6. Optionally configure non-root users to run Docker.

Essential HTML Tags for Beginners Essential HTML Tags for Beginners Jul 27, 2025 am 03:45 AM

To get started with HTML quickly, you only need to master a few basic tags to build a web skeleton. 1. The page structure is essential, and, which is the root element, contains meta information, and is the content display area. 2. Use the title. The higher the level, the smaller the number. Use tags to segment the text to avoid skipping the level. 3. The link uses tags and matches the href attributes, and the image uses tags and contains src and alt attributes. 4. The list is divided into unordered lists and ordered lists. Each entry is represented and must be nested in the list. 5. Beginners don’t have to force memorize all tags. It is more efficient to write and check them while you are writing. Master the structure, text, links, pictures and lists to create basic web pages.

How does Docker for Windows work? How does Docker for Windows work? Aug 29, 2025 am 09:34 AM

DockerforWindowsusesaLinuxVMorWSL2toruncontainersbecauseWindowslacksnativeLinuxkernelfeatures;1)itautomaticallymanagesalightweightLinuxVM(orusesWSL2)withHyper-VtohosttheDockerdaemonandcontainers;2)theDockerCLIandDesktopinterfaceforwardcommandstotheda

See all articles