Guide For Docker
1. Most useful Commands
- Command 1: build an image
The command:
docker build . -f path/to/Dockerfile -t talkohavy/repository:0.0.1 --no-cache
- Command 2: upload an image
The command:
docker build . -f path/to/Dockerfile -t talkohavy/repository:0.0.1 --no-cache
- Command 3: Just run an image locally
docker run -t --rm -e PORT=3000 IMAGE_ID
- Command 4: Run & SSH into image locally
docker run -it --rm -e PORT=3000 IMAGE_ID sh
- Command 5: Run an image & expose port to host
docker run -t --rm -p 8888:8888 imageName
- Command 6: Show logs of a running container
docker logs IMAGE_NAME_OR_ID
- Command 7: SSH into a running container
docker exec -it CONTAINER_NAME_ID sh
- Command 8: Copy a file into a container
When on your desktop, run the following:
docker cp ~/Desktop/my-file.txt CONTAINER_NAME_ID:/path/in/container/my-file.txt
2. Docker Commands
Table of contents• Command 1: docker system prune
docker system prune [OPTIONS] NAME[:TAG|@DIGEST]
Description:
Remove all unused containers, networks, images (both dangling and unreferenced), and optionally, volumes.
• Command 2: docker login
docker login
You can authenticate to any public or private registry for which you have credentials to, in order to push your images, and pull them for later use.
When you don't specify a registry, the default one is used, which is https://index.docker.io/v1/
.
You'll need to provide 2 credentials to login with: username
& password
.
Avoid using the --password
flag! Use --password-stdin
instead.
Example usage:
echo -n $DOCKER_ACCESS_TOKEN | docker login https://index.docker.io/v1/ -u talkohavy --password-stdin
• Command 2: docker push
docker image push [OPTIONS] NAME[:TAG]
Upload an image to a registry.
When the image gets pushed to is dependant on the registry you provided when you did the docker login
. In docker login, if the registry is omitted, dockerhub is chosen by default.
In case of pushing to dockerhub, you have you personal namespace there, which you created and named talkohavy
. This is also your username.
Inside talkohavy
namespace, you have repositories
.
Each repository
is an image, and all its versions. For example, "api-gateway@0.0.1" & "api-gateway@0.0.2".
Tp push an image to dockerhub, you mush first build it with proper naming convention:
docker build . -t username/repository@0.0.1