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
In this tutorial, we'll cover the basics of Docker container usage, including creating, managing, and interacting with containers.
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.
To list running containers, use the docker ps
command. To list all containers, including stopped ones, use docker ps -a
.
docker ps
To view detailed information about a container, use the docker inspect
command followed by the container name or ID.
docker inspect my-container
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
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.
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
To restart a running container, use the docker restart
command followed by the container name or ID.
docker restart my-container
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
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.
How to Run Docker Containers:
docker run
command to start Docker containers from images.docker run -d -p 8080:80 my_web_app
Managing Docker Containers:
docker ps
, docker stop
, docker start
, and docker rm
to manage containers.docker ps # List running containers docker stop <container_id> docker start <container_id> docker rm <container_id>
Docker Container Start and Stop:
docker start
and docker stop
to respectively start and stop containers.docker start <container_id> docker stop <container_id>
Creating Docker Containers from Images:
docker run
command.docker run -d my_web_app
Docker Container Commands:
docker ps
, docker exec
, and docker logs
.docker ps # List running containers docker exec -it <container_id> /bin/bash # Access container shell docker logs <container_id>
Docker Container Options and Flags:
-d
for detached mode, -p
for port mapping, and -e
for environment variables.docker run
:docker run -d -p 8080:80 -e MY_ENV_VAR=value my_web_app
Inspecting Docker Containers:
docker inspect
provides detailed information about a container, including its configuration and networking.docker inspect <container_id>
Copying Files to and from Docker Containers:
docker cp
to copy files between the host and containers.docker cp ./local_file.txt <container_id>:/app/container_file.txt docker cp <container_id>:/app/container_file.txt ./local_file.txt
Docker Container Environment Variables:
-e
flag in docker run
.docker run -e MY_ENV_VAR=value my_web_app
Docker Container Data Persistence:
docker run -v /host/path:/container/path my_web_app
Limiting Resources for Docker Containers:
--cpu
and --memory
flags.docker run --cpu=2 --memory=512m my_web_app