Docker Tutorial

Docker Installation

How to use Docker

Docker Instance

Docker Container Lifecycle Command

Docker Container Operation Command

Docker Container rootfs Command

Docker Mirror Repository Command

Docker Local Image Management Command

Docker info|version Command

Docker container usage

In this tutorial, we'll cover the basics of Docker container usage, including creating, managing, and interacting with containers.

  • Running a container:

To run a new container, use the docker run command followed by the image name. You can also specify options such as container name, port mappings, and environment variables.

docker run -d --name my-container -p 8080:80 -e SOME_ENV_VAR=value my-image

In this example, the -d option tells Docker to run the container in the background (detached mode), --name assigns a custom name to the container, -p maps port 8080 on the host to port 80 on the container, and -e sets an environment variable.

  • Listing containers:

To list running containers, use the docker ps command. To list all containers, including stopped ones, use docker ps -a.

docker ps
  • Inspecting a container:

To view detailed information about a container, use the docker inspect command followed by the container name or ID.

docker inspect my-container
  • Viewing container logs:

To view the logs of a container, use the docker logs command followed by the container name or ID. To stream the logs in real-time, use the -f option.

docker logs -f my-container
  • Executing commands in a container:

To execute a command inside a running container, use the docker exec command followed by the container name or ID and the command you want to execute.

docker exec my-container ls /app

In this example, we're running the ls command in the /app directory inside the container.

  • Stopping and starting containers:

To stop a running container, use the docker stop command followed by the container name or ID.

docker stop my-container

To start a stopped container, use the docker start command followed by the container name or ID.

docker start my-container
  • Restarting containers:

To restart a running container, use the docker restart command followed by the container name or ID.

docker restart my-container
  • Removing containers:

To remove a stopped container, use the docker rm command followed by the container name or ID. To remove a running container, use the -f option.

docker rm my-container
  • Attaching to a running container:

To attach your terminal to a running container and interact with its standard input, output, and error streams, use the docker attach command followed by the container name or ID.

docker attach my-container

Press Ctrl + p followed by Ctrl + q to detach from the container without stopping it.

This tutorial provides a basic overview of Docker container usage. These commands should help you manage and interact with your containers effectively.

  1. How to Run Docker Containers:

    • Description: Use the docker run command to start Docker containers from images.
    • Code: Example of running a Docker container:
      docker run -d -p 8080:80 my_web_app
      
  2. Managing Docker Containers:

    • Description: Use commands like docker ps, docker stop, docker start, and docker rm to manage containers.
    • Code: Examples of managing containers:
      docker ps           # List running containers
      docker stop <container_id>
      docker start <container_id>
      docker rm <container_id>
      
  3. Docker Container Start and Stop:

    • Description: Use docker start and docker stop to respectively start and stop containers.
    • Code: Examples of starting and stopping containers:
      docker start <container_id>
      docker stop <container_id>
      
  4. Creating Docker Containers from Images:

    • Description: Containers are created from images using the docker run command.
    • Code: Example of running a container from an image:
      docker run -d my_web_app
      
  5. Docker Container Commands:

    • Description: Docker CLI provides various commands for managing containers, including docker ps, docker exec, and docker logs.
    • Code: Examples of useful container commands:
      docker ps           # List running containers
      docker exec -it <container_id> /bin/bash   # Access container shell
      docker logs <container_id>
      
  6. Docker Container Options and Flags:

    • Description: Docker run command supports flags like -d for detached mode, -p for port mapping, and -e for environment variables.
    • Code: Examples of using flags with docker run:
      docker run -d -p 8080:80 -e MY_ENV_VAR=value my_web_app
      
  7. Inspecting Docker Containers:

    • Description: docker inspect provides detailed information about a container, including its configuration and networking.
    • Code: Example of inspecting a container:
      docker inspect <container_id>
      
  8. Copying Files to and from Docker Containers:

    • Description: Use docker cp to copy files between the host and containers.
    • Code: Examples of copying files:
      docker cp ./local_file.txt <container_id>:/app/container_file.txt
      docker cp <container_id>:/app/container_file.txt ./local_file.txt
      
  9. Docker Container Environment Variables:

    • Description: Pass environment variables to containers using the -e flag in docker run.
    • Code: Example of setting environment variables:
      docker run -e MY_ENV_VAR=value my_web_app
      
  10. Docker Container Data Persistence:

    • Description: Use volumes or bind mounts for data persistence in Docker containers.
    • Code: Example of using volumes:
      docker run -v /host/path:/container/path my_web_app
      
  11. Limiting Resources for Docker Containers:

    • Description: Docker allows limiting container resources, such as CPU and memory, using the --cpu and --memory flags.
    • Code: Example of limiting CPU and memory:
      docker run --cpu=2 --memory=512m my_web_app