When working with docker, there are a lot of commands to manage the container, images, troubleshoot issues or access useful information on runtime. The following are some commands to help with that kind of tasks.
Below you can find the most common commands to interact with Docker containers.
# Show all running docker containers docker ps # Show all docker containers docker ps -a # Run a container docker run <image>:<tag> # Run a container and connect to it docker run -it <image>:<tag> # Run a container and clean it up after exit docker run --rm <image>:<tag> # Run a container in the background docker run -d <image>:<tag> # Stop a container docker stop <container> # Delete a container docker rm <container> # Kill a container docker kill <container>
Below you can find the most common commands to manage Docker images.
# List available local images docker images # Build an image with a dockerfile docker build -t <image>:<tag> <run_directory> -f <dockerfile> # Remove a local docker image docker rmi <image>:<tag> # Show metadata for an image docker image inspect <image> # Remove all unused docker images docker image prune
Interaction among Host and Container
Even though Docker provides isolation into the container, sometimes we might need to perform son interaction between the Host machine and a container, for example: copying information, accessing a port in the container, or sharing a folder between the host and the container.
# Map a local port to a docker instance docker run -d -p 127.0.0.1:<local_port>:<docker_port> <image> # List the ports a docker container is running on docker port <container>
If we need to copying data from docker container to the host machine or vice-versa
# Copy file or folder from a docker container to host machine docker cp <container>:<container_dir> <local_dir> # Copy file or folder from local machine onto a container docker cp <local_dir> <container>:<container_dir>
# List volumes docker volume ls # Create a volume docker volume create <volume> # Delete a volume docker volume rm <volume> # Show volume metadata docker volume inspect <volume> # Delete all volumes not attached to a container docker volume prune # Mount a local directory to your container docker run -v <local_dir>:<container_dir> <image>
Registry and Repository
These command help to connect a docker Repository of images, pull and push images to it.
# Login to a remote repository docker login <repository> # Search for docker images docker search <image> # Pull a docker image from a repository docker pull <image> # Push an image to your remote repository docker push <image>:<tag> #Retag a local image with a new image name and tag docker tag <image>:<tag> <repository>/<image>:<tag>
Below you can find the most common commands to interact with Docker Compose.
# Start your docker-compose defined resources in detached mode docker-compose up -d -f <docker_compose_yaml> # Stop all docker-compose resources docker-compose stop # Destroy all docker-compose resources docker-compose down # Show docker-compose processes docker-compose ps # Show docker-compose logs docker-compose logs # Show docker-compose resource consumption docker-compose top # Pull latest docker-compose images docker-compose pull
When an issue happens, we might need to access the logs or the internal of the container. For that purpose, we can use the following commands.
# Show the logs of a container docker logs <container> # Follow/tail the logs of a container docker logs -f <container> # Show timestamps on docker logs docker logs -t <container> # Execute a command on a container docker exec -it <container_id> /bin/bash
There are some other useful commands to get information to troubleshoot an issue.
# Show details/metadata of a container docker inspect <container> # Show a 'top' view of processes running on a container docker top <container> # Show a 'top' view of all docker containers docker stats # Show any files that have changed since startup docker diff <container> # Connect to an already running container docker attach <container> # Show docker system wide information docker system info # Show docker disk space used docker system df