#day20 of #90daysofDEVOPS
Are you just stepping into the world of Docker and feeling a bit overwhelmed by all the commands and options? Don’t worry; you’re not alone! Docker is a powerful tool for containerization, but it can take some time to get comfortable with its commands and functionalities. To help you along your journey, we’ve put together a comprehensive Docker cheat sheet tailored for beginners. Let’s dive in!
Getting Started
First things first, let’s familiarize ourselves with some basic Docker commands:
docker version
: Displays detailed information about your Docker CLI and daemon versions.docker system info
: Lists data about your Docker environment, including active plugins and container/image counts.docker help
: View the help index, a reference of all the supported commands.docker <command> --help
: View detailed information about a particular command.
Building Images
Building images from Dockerfiles is a fundamental aspect of Docker:
docker build .
: Build the Dockerfile in your working directory into a new image.docker build -t example-image:latest .
: Build the Dockerfile and tag the resulting image.docker build -f docker/app-dockerfile
: Build a Dockerfile at a specific path.docker build --build-arg foo=bar .
: Set build arguments during the build process.docker build --pull .
: Pull updated versions of images referenced in FROM instructions.docker build --quiet .
: Build an image without emitting output.
Running Containers
Once you have your images, you’ll want to run containers:
docker run example-image:latest
: Run a container using a specific image.docker run -d example-image:latest
: Detach terminal from the running container.docker run -it example-image:latest
: Attach terminal's input stream and TTY for interactive commands.docker run --name my-container example-image:latest
: Name the new container.docker run -p 8080:80 example-image:latest
: Bind ports between host and container.docker run -v /host-directory:/container-directory example-image:latest
: Bind mount directories between host and container.docker run --network my-network example-image:latest
: Connect container to a specific network.
Managing Containers
Managing containers is crucial for effective Docker usage:
docker ps
: List all running containers.docker ps -a
: List all containers, including stopped ones.docker attach <container>
: Attach terminal to a running container.docker stop <container>
: Stop a running container.docker rm <container>
: Delete a container.
Copying to and from Containers
Copying files to and from containers is made easy with docker cp
:
docker cp example.txt my-container:/data
: Copy file from host to container.docker cp my-container:/data/example.txt /demo/example.txt
: Copy file from container to host.
Executing Commands in Containers
Run commands inside containers with docker exec
:
docker exec my-container demo-command
: Run command inside container.docker exec -it my-container demo-command
: Run interactive command inside container.
Accessing Container Logs
Access logs from containers using docker logs
:
docker logs <container>
: Stream existing log output.docker logs <container> --follow
: Stream new logs continuously.docker logs <container> -n 10
: Get last 10 logs.
Managing Images
Interact with Docker images using these commands:
docker images
: List all stored images.docker rmi <image>
: Delete an image.docker tag <image> example-image:latest
: Add a new tag to an existing image.
Pulling and Pushing Images
Push and pull images to/from remote registries:
docker push
example.com/user/image:latest
: Push image to remote registry.docker pull
example.com/user/image:latest
: Pull image from remote registry.