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 runto start a container from an image. - For example,
docker run hello-worldruns a simple test container. - Use flags like
-dfor detached mode and-pfor port mapping.
- Use
- Manage containers:
docker pslists running containers.docker ps -ashows all containers, including stopped ones.docker startrestarts a stopped container.docker stophalts 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 imageslists local images.docker rmi <image-id>remove an image.- Create a Dockerfile to build custom images.
- Build images:
- Use
docker buildto create an image from a Dockerfile. - Optimize images with multi-stage builds.
- Use
- Use Docker Compose:
- Create a
compose.yamlfile to define multi-container applications. - Use
docker compose upto 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.