Common Docker Commands
Docker is a powerful tool for containerizing applications. Here’s how to work with Docker:
-
Install Docker: Download and install Docker Desktop for your operating system (Windows, Mac, or Linux).
- Run containers:
- Use
docker run
to start a container from an image. - For example,
docker run hello-world
runs a simple test container. - Use flags like
-d
for detached mode and-p
for port mapping.
- Use
- Manage containers:
docker ps
lists running containers.docker ps -a
shows all containers, including stopped ones.docker start
restarts a stopped container.docker stop
halts a running container.docker rm <container-id-or-name>
removes a stopped container.
- Work with images:
docker search <image_name>
search for images.docker pull <image_name>
downloads an image from a registry.docker images
lists local images.docker rmi <image-id>
remove an image.- Create a Dockerfile to build custom images.
- Build images:
- Use
docker build
to create an image from a Dockerfile. - Optimize images with multi-stage builds.
- Use
- Use Docker Compose:
- Create a
compose.yaml
file to define multi-container applications. - Use
docker compose up
to start all services defined in the Compose file.
- Create a
- Share images:
- Push your images to Docker Hub or other registries.
- Persist data:
- Use volumes or bind mounts for data that needs to persist beyond the container’s lifecycle.
Remember to use docker --help
or docker <command> --help
for more information on specific commands.